0

I'm writing a function that supposed to update the price for all woocommerce products.. the price data i will get from amazon using amazon api which has a certain x query per second limit thats why i will have to sleep on each loop. Im planing to run that function as a cron job. the below function is just an example for what im planing to do so please ignore he missing variable declaration etc..

I understand that i can increase the php timeout limit but im imagining what if i have hundreads or thousand of products and on each query i will have to to sleep for a while to avoid query throtling so to update all products it can take hours in this case so im wondering what is the best and easiest solution to keep that function looping for hours and stop after reaching the last id on $products_ids array?

function text(){
  foreach ($products_ids as $products_id) {
  //apm_single_lookup func do an API call which has max query/sec limit thats why i added the next sleep
  $lookup_data = apm_single_lookup($eu_asin, $amazon_domain);   
  update_post_meta($products_id, $field_name, esc_attr($lookup_data['price']));
  sleep(1);
  }
}
3
  • "I understand that i can increase the php timeout limit" - That is the way to stop PHP from timing out. Commented Mar 2, 2019 at 3:23
  • a2hosting.com/kb/developer-corner/php/using-php.ini-directives/… Commented Mar 2, 2019 at 3:24
  • @MagnusEriksson I know but imagin to have thousand of products and you wanna update them and sleep 1 second after each product... most webhosting servers has a 300seconds timeout limit so i was looking for other solusion... for instance a backup plugin im using to avoid that it break the process into a smaller tasks each one of them run for less than 300 seonds so im wondering if its posible to do that and if its posible in my case then how to do that? Commented Mar 2, 2019 at 3:27

3 Answers 3

1

You can change max_execution_time of the server.

Or use : http://php.net/manual/fr/function.set-time-limit.php

like this :

set_time_limit(3600);
function text(){
  ...
}

Or another solution :

Split your loop in multiple Ajax call (or cronjobs), so you can stop and go and do what you want.

Sign up to request clarification or add additional context in comments.

6 Comments

Create a cronjob or automated task to do that. You don't need browser with that.
thank you, I guess if i used cron then i will need to store in database on which key on the ids array i ended so the next cron can start where the previous cron job ended, right? as far as i understand cron allow to run script in a specific interval so i can run that function every hour however what i was looking for to do using cron is to split the loob on a multiple crons that run after each other then re-update all the products once a week, is it posible to do so using cron?
All is possible with cronjob. you can create as much cronjob as you want. Just store steps in database. You have well understand how to do.
All is possible with cronjob I would like to say not everything is for example $_SERVER['SERVER_ADDR'] doesen't exist on the command line ... lol .... because PHP is running outside of Apache and as such there is no server. But in spite of that it's still a good choice for this... :-0
You can use gethostname(); instead.
|
1

Like this:

function text(){
    foreach ($products_ids as $products_id) {
          set_time_limit(60); //timelimit per loop iteration
          //apm_single_lookup func do an API call which has max query/sec limit thats why i added the next sleep
          $lookup_data = apm_single_lookup($eu_asin, $amazon_domain);   
          update_post_meta($products_id, $field_name, esc_attr($lookup_data['price']));
          sleep(1);
     }    
    //set_time_limit(5*60); //set back to longer after the loop if you want etc.
}

I think it's better in the case of loop to reset the timer on each iteration.

When called, set_time_limit() restarts the timeout counter from zero.

This way you can keep the timout small per iteration and not worry if it you have a lot of iterations. That made more sense in my head ....

It's even better to do it in the CLI (Command Line). Even when you set PHP's max execution time, Apache has it's own time limits too (likely in mod_fcgi etc).

Comments

1

put this code before loop:-

ini_set('max_execution_time', 0);

or

set_time_limit(0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.