admin管理员组

文章数量:1297120

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!

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!

Share Improve this question edited Mar 31, 2021 at 22:08 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Mar 31, 2021 at 22:04 Daniel KozinaDaniel Kozina 31 bronze badge 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 Commented Mar 31, 2021 at 22:10
Add a comment  | 

2 Answers 2

Reset to default 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.

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.

本文标签: phpLarge Wordpress CRON job