NickF
NickF

Reputation: 5737

Drupal cron vs PHP background task

I have a Drupal 7 site with some none Drupal web services that use some Drupal API.
I need to hold a global array with some values that I need to update each minute, that is available to each call to the web service.
I'm new to Drupal and PHP and I'm wondering if should I use pure PHP like:

 while(true){
        doSomething();
        sleep(60);
    }

or Drupal cron or something else?

Upvotes: 0

Views: 564

Answers (1)

Alex W
Alex W

Reputation: 38163

Yes, you should use Drupal's Cron. There's a link to a comprehensive setup video for Drupal's Cron in the link you provided. Using sleep() in an infinite loop is a bad idea because if you are on a shared hosting server, such as GoDaddy, the number of concurrent processes that can be running is limited. So if 20 users are sending requests to your server and 20 PHP processes are sleeping it can cause your server to crash (i.e. HTTP 50x error).

With Cron, you can just save the data you need in a file that is updated by Cron and access the file concurrently (multiple PHP processes) within your PHP script.

Upvotes: 2

Related Questions