使用AJAX触发后台作业

时间:2019-10-10 作者:dingo_d

I need a second opinion and a sanity check if the solution I\'m trying to implement will work.

I need to implement a way to run a script that would otherwise be blocking if I would run it using just ajax (and would probably timeout). So my idea is to do the following:

Have a button in the admin that, when clicked, would trigger an ajax call, which would schedule the event immediately. This scheduled event would be done using wp_schedule_event() (https://developer.wordpress.org/plugins/cron/scheduling-wp-cron-events/). In this event, on the first call, I\'d add a flag in the database that will signal that the event is running. This is used as a way to check if the event is running in the background so that I can do a check, in ajax, if the job is running in the background - if it is, don\'t run it again.

Now, because it\'s a scheduled, recurring job, I\'d need a way to not spawn it again all the time, even if it finished. What I was thinking is adding a check in the place where I add my add_action( \'bl_cron_hook\', \'bl_cron_exec\' ); hook for

if ( ! wp_next_scheduled( \'bl_cron_hook\' ) ) {
    wp_schedule_event( time(), \'five_seconds\', \'bl_cron_hook\' );
}

The add_action call and the function to execute bl_cron_exec would be in a separate class, and the wp_schedule_event would be in the ajax callback.

And also, if the flag in the database is finished I would unschedule the job (in the class where the bl_cron_exec is).

What concerns me is, if my job is running in the background, and I unschedule the job, will this terminate the already running job, or am I safe and this job will continue working (or fail, in which case I will write to the db that the call failed). Because a running job is started in a separate process if I\'m not mistaken. So a job that is running cannot be stopped, or can it?

Is there a smarter way of implementing background jobs in WordPress? I don\'t want a plugin, I need my own implementation (business reasons).

Basically, some kind of a flow would be:


ajax:

NO JOB RUNNING:

check if job is running - no
schedules event (in 1 sec or less)
triggers flag in the database - job running
exit with code - event started (scheduled)

JOB RUNNING:

check if job is running - yes
unschedule job
exit with code - event running

event:

If the database trigger says: running (the wp_next_scheduled should take care of that) - don\'t start new schedule (return? or unschedule).
If the database trigger says: finished, continue running the event.
2 个回复
SO网友:Fernando Claussen

我不认为你需要担心这个运行标志或计划外。由于您正在计划一个将立即运行的一次性作业,因此没有理由取消计划。

您只需检查具有该名称的作业是否已计划,以及是否已计划(即使此时正在运行),不要执行任何操作。

是的,这些作业运行在不同的线程上,据我所知,您不能中途取消它们。

因此,流程是:

单击按钮,在JS中运行Ajax,PHP回调检查是否已安排,如果未安排,则安排并返回wp\\u send\\u json\\u success()<让它运行,如果是,用wp\\u send\\u json\\u error()杀死ajax

SO网友:John Dee

我这样做的方式是有三种不同的方法。一个启动,一个停止,一个执行流程并检查是否仍应继续。

interface WPCronBackgroundProcessManagerInterface {
    /**
     * @return boolean
     * determines if the condition to continue the process is still active
     * used to initiate the process, and determine if the process should continue
     */
    public function isProcessConditionActive();

    /**
     * @return bool; false for failure
     * @return bool; true for success
     */
    public function initiateProcess();

    /**
     * @return bool; false for failure
     * @return bool; true for success
     * This method should do the process, and check if it wants to keep going
     */
    public function continueProcess();
}

相关推荐

How to use wp-ajax in wp-cron

我已经创建了一个自定义wordpress插件,其中我使用的是wp ajax。在类的\\uu构造中,我有一个操作(admin\\u footer),以便在wp之后使用wp\\u enqueue\\u脚本和wp\\u localize\\u脚本instructions因此,流程类似于php->js->和php我为什么这么做?避免内存限制错误。Here 说得很好:“上面的解决方案通过将单个大型任务分解为多个小型任务来绕过PHP的限制”一切都像一个后台的魅力!我的问题是如何管理此过程以与wp cron