Does WordPress Cron API Run in the Background?

I’m reading the code of cron.php in wp-includes and spawn_cron() seems to be the one which actually executes registered tasks.

The last two lines of the function:

Read More
$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 ) ) );

It simply opens wp-cron.php passing a task an as a query argument.

The description of the API at the top of cron.php:

* 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.`

My question is that let’s say the visitor opens one of the pages of the site and then the registered task is fired by the cron API. And if the task is heavy and takes several minutes to complete, does the visitor gets a page that is not completely loaded until the task finishes?

[Edit]
To clarify what I’m asking, the question is, does the WP Cron API run tasks after the page loading completes?

Related posts

Leave a Reply

2 comments

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

    With the below sample plugin, I confirmed that the above code (I quoted in the question as well) is the one actually calls scheduled tasks. It sets 0.0.1 timeout and accesses to wp-cron.php. This means if there are 100 tasks it takes 1 seconds to load all tasks. So it has a slight impact on the page loading speed. But it seems to be something not to worry about too much.

    <?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);
    }