2

I am developing a plugin and want to add a cron task(let's say cron_task_test) but have a doubt. In case 15 tasks(they may be scheduled by multiple plugins or the user himself) are scheduled to run at a time along with my task(cron_task_test), will wp-cron pick them one by one i.e. pick another task after first has finished or if the first task has taken more than a minute? Or does all will be fired one after another

In short, how does cron_lock works? In case it executes one after another, will not the main cron thread running all tasks one by one itself exceed maximum execution time limit of the server(generally 30 seconds)?

What will happen if any other cron task before my task(cron_task_test) has thrown an error or exceeded time-limit?

1 Answer 1

0

For example about 12:00 it should run 3 tasks.

If You use wp-cron it will start when someone visit Your page about 12:00. ( Here is small complication because when two users visit Your page exactly in the same moment cron can start twice and do everything twice ).

However someone opens our page round 12:00 and cron starts. It pulls all the scheduled events in array. Set transient that cron is busy and start all process inside foreach statement using call_user_func_array ( it is almost the same as do_action but for array ).

Procedure in foreach:

  • reschedule if necessary;
  • unschedule before start of job;
  • call_user_func_array ( do the job );
  • check cron lock because if the hook ran too long and another cron process stole the lock, cron quits loop;

My small tips:

  • crontab can be better solution;
  • don't forget about some logging to test your cron task;
  • if cron's one of jobs fails it has no impact on other tasks ( if i am wrong please correct me );

Here You have some extra information wrote in article by my friend: Cron solutions for wordpress

2
  • It does not clearly answer questions: 1. Does the scheduler runs all tasks one after another or fires Async HTTP requests for all schedules tasks? 2. If it schedules one after another, doesn't server stops it due to exceeding maximum time allowed for a script? Mar 3, 2018 at 15:48
  • It runs all tasks one after another in foreach loop. After every task, it checks off another task already started or not. Timelimit can be reached by one task that's why there is a lock. No matter what next task should run. Visit wp-crone.php it is easy to understand. Mar 5, 2018 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.