WP schedule with intervals

Viewed 45

I have Wordpress website with the Woocommerce plugin.

I developed plugin with function that runs while loop to go through my 3,000 products. The function changes the price of the products by external data (it compares prices of competitors).

Everything works great, i succeed, the function runs and the scraper is great.. but, now i understand that the external website allowed me to scrape only once per 10 seconds. Also, i need this function to run once per 4 hours.

What is the best way to achieve this goal?

Summary:

  • I need the function to run once per 4 hours
  • On each run (once per 4 hours) i need the function to "rest" for 10 seconds.

I already set successfully the cron job of the 4 hours run, but i can't understand how to make this while loop to "rest" for 10 seconds. I tried to use sleep(10) function, but its stop to run after 10 times i guess because "execution time" limit of my server.

function isa_add_every_four_hours_getCompetitorsPrices( $schedules ) {
            $schedules['every_four_hours'] = array(
                    'interval'  => 14400,
                    'display'   => __( 'Every 4 hours', 'textdomain' )
            );
            return $schedules;
        }

if ( ! wp_next_scheduled( 'isa_add_every_four_hours_getCompetitorsPrices' ) ) {
    wp_schedule_event( time(), 'every_four_hours', 'isa_add_every_four_hours_getCompetitorsPrices' );
}


// Hook into that action that'll fire every 4 hours
add_action( 'isa_add_every_four_hours_getCompetitorsPrices', 'my_cronjob_walker_zs_gzp' );
0 Answers
Related