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
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