0

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!

1
  • 1
    When you say you are using a server triggered cron, how are you doing that? I ask because some people use curl or wget command to trigger Cron via the URL, which would put an upper limit on the time it can run. Otherwise I do not believe this question can be answered, as there is no right/wrong way. You need to write a Q that can be answered with facts, if it runs and works for you then I do not see the problem. If you're looking to discuss architecture, this isn't a discussion forum – Tom J Nowell Mar 31 at 22:10
1

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.

2
  • It's a very good answer for me. I tried to do it but I have a problem with "same_hook_as_daily_cron". If I'm scheduling single cron event with the same name as the parent (first action) I have duplicated actions in "wp cron event list". I have two actions with the same name but the time is different. I think it's good because my second and third action will be run one by one and the main action will be running at a specific time. In this way, I can test it by WP CLI command line because when I init this action I have duplicates in response. Thank you I think it's very helpful :) – Daniel Kozina Apr 3 at 22:56
  • @DanielKozina Yes you'll have same action, but with different arguments. – kero Apr 5 at 12:13
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.