【问题标题】:Does WordPress Cron API Run in the Background?WordPress Cron API 是否在后台运行?
【发布时间】:2012-09-06 02:02:57
【问题描述】:

我正在阅读 wp-includes 中 cron.php 的代码,spawn_cron() 似乎是实际执行注册任务的代码。

函数的最后两行:

$cron_url = site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron );

wp_remote_post( $cron_url, array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) ) );

它只是打开 wp-cron.php,将任务作为查询参数传递。

cron.php 顶部的 API 说明:

* Schedules a hook which will be executed once by the WordPress actions core at 
* a time which you specify. The action will fire off when someone visits your
* WordPress site, if the schedule time has passed.`

我的问题是,假设访问者打开站点的一个页面,然后注册的任务被 cron API 触发。如果任务很繁重并且需要几分钟才能完成,访问者是否会在任务完成之前获得一个尚未完全加载的页面?

[编辑] 为了澄清我在问什么,问题是 WP Cron API 会在页面加载完成后运行任务吗?

【问题讨论】:

    标签: php cron wordpress


    【解决方案1】:
    $cron_url = site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron );
    wp_remote_post( $cron_url, array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) ) );
    

    通过下面的示例插件,我确认上面的代码(我也在问题中引用)是实际调用计划任务的代码。它设置 0.0.1 超时并访问 wp-cron.php。这意味着如果有 100 个任务,加载所有任务需要 1 秒。所以它对页面加载速度有轻微的影响。不过这似乎是不用太担心的事情。

    <?php
    /* Plugin Name: Sample Cron Task */ 
    
        // I used `heavy` because this code was initially written to test how it affects the server response if a heavy task is registered as a cron job. So forget about the naming.
    add_action('admin_menu', 'sample_cron_heavy_task');
    function sample_cron_heavy_task() {
        add_options_page(
            'Sample Cron Heavy Task', 
            'Sample Cron Heavy Task', 
            'manage_options',
            'sample_cron_heavy_task', 
            'sample_cron_heavy_task_admin');
    }
    function sample_cron_heavy_task_admin() {
        ?>
        <div class="wrap">
        <?php
            wp_schedule_single_event(time(), 'my_action_name');
            $cron_url = site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron );
            // executes the registered task immediately 
            wp_remote_post( $cron_url, array( 'timeout' => 0.01, 'blocking' => false, 'sslverify' => apply_filters( 'https_local_ssl_verify', true ) ) );
            echo get_option('sample_cron_heavy_task');
        ?>
        </div>
        <?php
    }
    
    add_action('my_action_name', 'myevent'); 
    function myevent() {
        $msg = date('Y m d h:i:s A') . ': the cron task is called.<br />';
        update_option('sample_cron_heavy_task', $msg);
    }
    

    【讨论】:

    • 访问者是否会在任务完成之前获得未完全加载的页面?
    【解决方案2】:

    cron 作业根本不应该影响您的查看器。如果它确实找到了新的托管公司。

    【讨论】:

    • 只要您的功能设置正确,它们仍应接收创建页面所需的数据。从数据库中检索永远不应该有停顿。
    • 我仍然很难理解你所说的。 if it does find a new hosting companyto create the pageretrieval from the database 听起来与问题无关。我错过了什么吗?
    • 我只是说这绝不会影响您网站的浏览者。
    • 我担心的是,在有人访问某个页面后打开隐藏页面会触发任务;那我怎么知道页面已经完全加载了。
    • 我说的是 WP Cron,它的工作方式与常规的 linux/unix cron 作业不同。这就是为什么我在最初的帖子中引用了 API 的最后两行。
    猜你喜欢
    • 1970-01-01
    • 2010-12-23
    • 2012-01-13
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多