0

I have a web page that contains a php script that pulls data from an API and stores it in a database. My problem is that the script takes 10 minutes to run and stops if the user navigates away from the page on which it's loading - not ideal!

To solve this I'd like to run/load this page on my server as a background job so that a users can still navigate the site without the site or script hanging.

I've had a look at shell_exec but cant seem to make that work:

<?php
shell_exec("/usr/bin/php http://example.com/data_base_update.php &");
?>

Does anyone know how to trigger a URL on the server, for example http://example.com/data_base_update.php without using a cron job?

Thanks,

Matt

user1419810
  • 836
  • 1
  • 16
  • 29

2 Answers2

0

I am not a PHP expert, however your approach for achieving the goal seems bit wrong.

My recommendation is whenever the URL is hit, start an async task in the background and return the control immediately. Writing async task in PHP does not look simple, however this theard will help. You can also use PThreads.

Community
  • 1
  • 1
SyntaX
  • 2,090
  • 17
  • 30
0

Not sure why you are opposed to using cron or some kind of automatic task scheduler to handle the long-running process in the background.

I would recommend separating / isolating the long-running task from the code that displays your webpage, move it into a php script that can be called via command line. Then, when a user arrives on your page, you can add the pertinent details to a separate table that would act as a queue.

A cron job could run periodically on your server that would call the php cli script where your API code lives, for each user in the queue table. You could even e-mail notification to the user when the task is complete, or have a button on the page that the user can check if the task is completed yet.

One thing to keep in mind is that if each user requires a 10-minute API call, this could be a VERY long-running script... without knowing the details of the web service you are calling, I would make certain their is no way to submit multiple users in a single call... as this would be a lot more efficient.

Remember, it is important to communicate to the end-user what is happening with dialog / messages like "your information is being gathered and will be available shortly" ... you get the idea.

Jason Fingar
  • 3,358
  • 1
  • 21
  • 27