I had to use php exec

      No Comments on I had to use php exec

So, I was faced with a situation of needing a process I had written in php to run in the background since it could last over a minute and users won’t wait that long.

I contemplated writing a daemon (The web app lives on a Red Hat box running the standard LAMP stack), but pear’s Daemon class wouldn’t run on the box unless I recompiled php with the proper support.  Normally I wouldn’t be opposed to this, but this is on a live site with cpanel/whm managing the server, so I was afraid to touch anything.  I like the usefulness of that package for hosting sites, but it seems horrible for web apps where you need good control over the server setup.  I really didn’t have the time to rewrite the process in python to make it a good daemon.  (Plus I barely know python, so I would need to learn a language and rewrite the process.)

Crontab entered my mind for a second, but then I realized that there were so many things wrong with it that I just couldn’t do it.  Mostly it would be the wasted server resources every time the script ran when it didn’t need to, but there was also the fact that I could schedule it for every one min, but what happens if the script takes longer then a minute to run?

I finally landed on php’s exec function.  A function that lets you run a linux command from php.  I simply made an ajax call to a script that wrote a bit of data and initated a php script via exec/command line php that did the rest of the work while the user was free to browse the site.

So, here’s my solution.  Probably not the best solution, but given what I had to deal with, I like the outcome.

Step 1: Speed up the process of collecting user data.
I started by taking user input and sending it directly to the web service that returned the data I needed.  Since it was a web service, it could take some time to return.  I changed the process to write the user input to a local MySql database.  Much faster, so no wait on their end.

Step 2: Write a process to deal with the MySql data.
I had the class written to take the user input to the web service, but the data came from my first process, so I needed something in the middle, that could run in the background, to read the data from MySql and start up the web service call.  All this process does is load data that needs to be processed into an array and sends that array to the class already written to get data from the web service.  I know it could be improved, but the basics of it look like this.  (Note, I use a MySql class a bit, so their functions are in here for data reading. I can’t find their website, otherwise I would post it to show credit.)

[php]
$SQL = new BASE_SQL();
$SQL->query("Select ID, userId, data from tableWithData where processed = 0 and beingProcessed = 0");
$SQL3 = new BASE_SQL();
$SQL3->query("Update tableWithData set beingProcessed = 1 where processed = 0 and beingProcessed = 0");
while($SQL->next()) {
$rec = $SQL->record;
$recordId = $rec[‘ID’];
$userId = $rec[‘userId’];
$object->objectData = $rec[‘data’];
myFunction($userId, $object);  //This is the previous function that is recycled in the moving of the process to the background.
$SQL2 = new BASE_SQL();
$SQL2->query("update tableWithData set processed = 1, beingProcessed = 0 where ID = " . $recordId);
}
[/php]

Step 3: Call that process right after a user submits their data to the MySql database.
Previously, when a user submitted their data, I did an ajax post to a script that did everything.  Now, that same script writes the data to a local MySql database, then starts the script that does everything in the background using this simple command:

[php]
exec(‘/usr/bin/php -f /path/to/file/file.php > /dev/null &’);
[/php]

The first part specifies the program (“usr/bin/php), the next part says what that program should do (-f /path/to/file/file.php = run that file in php). I’m unclear of the next two’s specifics (Hence why I said I have a lot to learn) but they basically say don’t log anything (> /dev/null/) and run the process in the background (&).

I feel a bit hackish since I know it can be improved upon, but I successfully moved a long script to the background of a web app, so the client is happy.

The web app it’s on is http://www.argosrisk.com/A software service offering credit risk analysis for small companies.

Leave a Reply

Your email address will not be published. Required fields are marked *