2

I have a PHP script that checks the last time a SQLite database has been updated (30 minutes timespan) when a user visits the page. If it has been longer than 30 minutes, then the script will pull new information into the database. However, I'm worried that the user might leave while the database is updating, therefore neglecting to update some of the entries. What can I do to keep the script executing even after the user leaves?

I've looked at some of the similar questions here and found people suggesting using ignore_user_abort(), however there seems to be issues with that when data cannot be sent back to the client. Any other suggestions would be greatly appreciated. Thanks!

Sam
  • 45
  • 4
  • Treat ignore_user_abort() as a method to ensure any outstanding DB work continues and nothing more, the problem that "Data can't be sent to a user" is pretty clear since a user has aborted the connection, I dont think this will impact your DB processes at all. – Scuzzy Jul 18 '11 at 01:40

2 Answers2

1

You can't send data back to the client when he exits/stop to visit your website. You can open sock witch prevent from slowdown on client's side when loading page

$fp = fsockopen($host, 80, $errno, $errstr, 10);
if (!$fp) {
    echo "$errstr ($errno)\n";
} else {
   $header = "GET /cron.php HTTP/1.1\r\n";
   $header .= "Host: $host\r\n";
   $header .= "Connection: close\r\n\r\n";
   fputs($fp, $header);
   fclose($fp);
}

//do another stuff

this will send signal to /cron.php to do stuff you want and ALSO does not slowdown user's browsing experience ;)

genesis
  • 50,477
  • 20
  • 96
  • 125
1

Asynchronous PHP call

One option would be to do asynchronous PHP call(Request). See Asynchronous PHP calls? for more information. But keep in mind that when doing this a lot you are spawing a lot of background processes which could kill your server.

P.S: Also when you are using shared hosting, doing this stuff is generally not appreciated much

Message Queue

A way better way to do this would be using a Message Queue(MQ). You could use Redis or Beanstalkd just to name two popular MQs. You have a free redis instance provided to you thanks to http://redistogo.com/. From the client/producer(user visiting your page) you would then add message to the queue using RPUSH. From the consumer(SQLite) which is a PHP process running in the background endlessly(CLI) you would retrieve messages put on the queue using BLPOP. Spawning processes is expensive and is avoided when using a message queue.

Community
  • 1
  • 1
Alfred
  • 60,935
  • 33
  • 147
  • 186