admin管理员组

文章数量:1332873

I am trying to learn how to use the action scheduler so I am hoping somehow can give me some good pointers. I've got this code:

<?php
add_action( 'init', array( $this, 'wootomation_schedule' ) );
add_action( 'wootomation_import', array( $this, 'wootomation_do_action' ) );

function wootomation_schedule() {
    // if ( false === as_next_scheduled_action( 'wootomation_import' ) ) {
    //  as_schedule_recurring_action( time(), 30, 'wootomation_import' );
    // }
    if ( false === as_next_scheduled_action( 'wootomation_import' ) ) {
        as_enqueue_async_action( 'wootomation_import' );
    }
}

function wootomation_do_action() {
    var_dump('test');
    wp_mail( '[email protected]', 'test AS', 'Action schedule completed' );
}

Which appears to create the action but it never runs. I tried refreshing the front end of the website but didn't help.

Can anyone tell me how to get it to run?

And the 2nd question would be, is this the right approach for breaking up some heavy indexing, maybe with args such as page: "0-100", page: "101-200", etc.

Thanks, Dragos

I am trying to learn how to use the action scheduler so I am hoping somehow can give me some good pointers. I've got this code:

<?php
add_action( 'init', array( $this, 'wootomation_schedule' ) );
add_action( 'wootomation_import', array( $this, 'wootomation_do_action' ) );

function wootomation_schedule() {
    // if ( false === as_next_scheduled_action( 'wootomation_import' ) ) {
    //  as_schedule_recurring_action( time(), 30, 'wootomation_import' );
    // }
    if ( false === as_next_scheduled_action( 'wootomation_import' ) ) {
        as_enqueue_async_action( 'wootomation_import' );
    }
}

function wootomation_do_action() {
    var_dump('test');
    wp_mail( '[email protected]', 'test AS', 'Action schedule completed' );
}

Which appears to create the action but it never runs. I tried refreshing the front end of the website but didn't help.

Can anyone tell me how to get it to run?

And the 2nd question would be, is this the right approach for breaking up some heavy indexing, maybe with args such as page: "0-100", page: "101-200", etc.

Thanks, Dragos

Share Improve this question asked Jun 5, 2020 at 13:40 Dragos MicuDragos Micu 2132 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1
require_once( plugin_dir_path( __FILE__ ) . '/libraries/action-scheduler/action-scheduler.php' );

/**
 * Schedule an action with the hook 'eg_midnight_log' to run at midnight each day
 * so that our callback is run then.
 */
function eg_log_action_data() {
    if ( false === as_next_scheduled_action( 'eg_midnight_log' ) ) {
        as_schedule_recurring_action( strtotime( 'midnight tonight' ), DAY_IN_SECONDS, 'eg_midnight_log' );
    }
}
add_action( 'init', 'eg_log_action_data' );

/**
 * A callback to run when the 'eg_midnight_log' scheduled action is run.
 */
function eg_log_action_data() {
    error_log( 'It is just after midnight on ' . date( 'Y-m-d' ) );
}
add_action( 'eg_midnight_log', 'eg_log_action_data' );

Docs Link: https://actionscheduler/usage/#scheduling-an-action

本文标签: wp cronAction Scheduler not running