0

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?

NickF
  • 5,637
  • 12
  • 44
  • 75

1 Answers1

2

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.

Alex W
  • 37,233
  • 13
  • 109
  • 109
  • And that ensures me that this task runs only once and accessible from my external PHP pages (they are located under my Drupal root folder) ? – NickF Dec 23 '13 at 15:56
  • Isn't it faster/resource friendly/disk io friendly to just store it in the database and configure your server to update it every 60 seconds with a cronjob? – Anyone Dec 23 '13 at 15:56
  • @NickF Correct. The task should only run once during the interval specified via Cron. That file will be accessible to any scripts running on the same server, provided the permissions are set correctly on it. – Alex W Dec 23 '13 at 16:01
  • And how do I access to that variable? (let call it $x) – NickF Dec 23 '13 at 16:07
  • @Anyone It could easily be stored in the database, depending on what type of data it is. I think the only advantage would be portability/backup. The database actually uses files on the filesystem and will have to create 3 I believe for a new table in MySQL? The web browser will take care of caching it, which mitigates MySQL's caching advantage. – Alex W Dec 23 '13 at 16:08
  • @NickF I would need to see an example of the data you are trying to store. – Alex W Dec 23 '13 at 16:09
  • @Alex W it going to an array that will be accessible by all possible sessions. I mean each time my none drupal service is called I check some value in that array. Better without database. – NickF Dec 23 '13 at 16:12
  • @NickF See [this question](http://stackoverflow.com/questions/2662268/php-how-do-i-store-an-array-in-a-file-to-access-as-an-array-later-with-php). You can store your array in the file in JSON format and easily access it with PHP. – Alex W Dec 23 '13 at 16:14
  • Storing something in a mysql server (which is hosted on a dedicated server for that) is a lot more IO efficient than having your webserver do all the IO. Having your webserver do it, means there is less CPU cycles to process the actual serving of pages, but that's just my POV. – Anyone Dec 23 '13 at 16:29