WordPress.org

Ready to get started?Download WordPress

Forums

End-to-End Cron setup. (3 posts)

  1. tonyparera
    Member
    Posted 1 year ago #

    Hi,

    I am looking for a short guide on End-to-End Cron setup, with following steps I already did and some still waiting.

    Purpose : I have some big SQL which I cached using set_transient with timeout, however I want to run this SQL and use set_transient in cron so my end user never experience the slowless.

    Steps I thought so far,

    1. Disable the Cron job for every page load.
    A: define('DISABLE_WP_CRON', true) in wp-config.php file. (DONE)

    2. Run cron at regular interval from your system.
    A: Setup cron entry with "wget -q -o - -O - -t 1 http://example.com/wp-cron.php > /dev/null 2>&1. This sets output and log goes to null, quite mode and retry once only. (DONE)

    3. setup the cron schedule for every 20 mins.
    A : to add this filter
    add_filter( 'cron_schedules', 'cron_add_20mins' );
    function cron_add_20mins( $schedules ) {
    // Adds every 20mis to the existing schedules.
    $schedules['every20mins'] = array(
    'interval' => 1200,
    'display' => __( 'Once Every20mins' )
    );
    return $schedules;
    }
    (BUT I don't know where to add this, so it execute only one time).

    4: Schedule an Once Every20mins job.
    A: to add this two filters.
    function my_activation() {
    if ( !wp_next_scheduled( 'my_hourly_event' ) ) {
    wp_schedule_event( time(), 'hourly', 'my_hourly_event');
    }
    }
    function do_this_20mins() {
    //check and cache all SQL object with set_transient.
    }
    add_action('wp', 'my_activation');
    add_action('my_hourly_event', 'do_this_20mins');

    TWO QUESTIONS :
    1. I guess action 'my_hourly_event' is not right if I want to make it every 20mins, so what it could be ?
    2. I don't know where to add this, so it execute only one time.

  2. tonyparera
    Member
    Posted 1 year ago #

    Hi Christine,

    Thanks. It helps but I have two distinct question on the same ?

    1. Where can I put my add_filter( 'cron_schedules', 'cron_add_20mins' ); so It execute one time only ? I guess if I put in functions.php than it will call every time page loads.

    2. Which hook i should use to bind "add_action('my_hourly_event', 'do_this_20mins');" , will "my_hourly_event" sufficient ?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags