Domanda

I'm programming the integration with external API (real estate). I have one CRON job which is planned for at 1 o'clock am. It's running very well because I'm using server CRON initialize instead WP CRON standard so It's running at the right time.

It's my scheduled job function:

function wcs_cron_system_update() {

    wcs_cron_start_update();

    $wcs_cron_actions = new WCS_Cron_Actions();
    $wcs_cron_helpers = new WCS_Cron_Helpers();

    $wcs_cron_actions->real_estate_cleaning();
    $wcs_cron_helpers->remove_unused_images();

    $wcs_cron_actions->investments_insert_sql();
    $wcs_cron_actions->apartments_insert_sql();
    $wcs_cron_actions->commerce_insert_sql();

    $wcs_cron_actions->investments_insert();
    $wcs_cron_actions->real_estate_insert();

    $wcs_cron_actions->investment_update_by_single_real_easte();
    $wcs_cron_actions->real_estate_update_by_single_investment();
    $wcs_cron_actions->investment_update_by_all_real_easte();
    $wcs_cron_actions->search_index_update();


}

I'm doing a few functions one by one because I need some data before I'm doing the next function. I can't schedule (I don't know how I should do it) separately CRON jobs because I had a few situations when some function was run before I have necessary data (because some functions were run before it should be)

My function contains a sequence of tasks (functions).

Question: is it the correct way to do it? maybe I should do it another way? how?

Thanks!

È stato utile?

Soluzione

I can't schedule (I don't know how I should do it) separately CRON jobs because I had a few situations when some function was run before I have necessary data

The key to doing this is scheduling the first job to run daily at the required time, and have that event create all of the other ones after it is done.

function wcs_cron_system_update($step = 0) {
    switch ($step) {
        case 0:
            // do what has to be done first
            break;
        case 1:
            // second step
            break;
        case 2:
            // etc
            break;
    }
}

If you start like this, you can pass the $step as an argument to it. So on the daily cron it executes step 0.

After the first step is done, what you can do is call wp_schedule_single_event() like so

\wp_schedule_single_event(
    // start in 5min
    \current_time('timestamp') + (5 * 60),
    'same_hook_as_daily_cron',
    [1]
);

This will create an event for your second step. When that is done, just create a single event for your third step. Make sure your add_action code allows for arguments.

Altri suggerimenti

Question: is it the correct way to do it? maybe I should do it another way? how?

There is no correct way to do it, it depends what your cron job is meant to do. If your cron job runs then it is correct. If it does not, then it isn't.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top