Jake
Jake

Reputation: 105

How to check if process is completed?

I have a php script that is currently invoked directly by a webhook. The webhook method was fine up until this past week where the volume of requests is becoming problematic for API rate limits.

What I have been trying to do is make a second PHP file ($path/webhook-receiver.php) to be invoked by webhooks only when there isn't a process running. I'll be using the process user webb recommended, which is in the invoked script ($path/event-finance-reporting.php) it will create a file as the first action, and the delete that file as the last exection.

Before invoking the script the automation will check the directory to make sure it is empty, otherwise it will kick back an error to the user telling them to wait until the current job is completed before submitting another one.

The problem I'm running into now is that both $command1 and $command2'. both end up invoking the$path/webhook-reciever.phpinstead of$path/event-finance-reporting.php`.

$command1 = "php -f  $path/event-finance-reporting.php 123456789";
$command2 = "/usr/bin/php -q -f  $path/event-finance-reporting.php 123456789";

Anyone know why would be?

Upvotes: 0

Views: 363

Answers (1)

webb
webb

Reputation: 4340

The goal it to have only one instance of event-finance-reporting.php run at a time. One strategy is to create a unique lockfile, don't run if it exists, and delete it when it finishes, e.g.,:

$lockfilepath = '.../event-finance-reporting.lock';
if(file_exists($lockfilepath)){
  print("try again later");
  exit();
}
touch($lockfilepath);
...
// event-finance-reporting.php code
...
unlink($lockfilepath);

You could also do something more complicated in the if, such as checking the age of the lockfile, then deleting and ignoring it if it was left behind awhile ago by a crashed instance of event-finance-reporting.php.

With this strategy, you also don't need two separate phps.

Upvotes: 1

Related Questions