I'm trying to create a PHP script that runs in the background and forks child processes. (I'm aware that could explode the server; there are extra safeguards in place that are outside the scope of this question)
In a nutshell, the code works like this:
$pids = array();
$ok = true;
while ($ok)
{
$job = $pheanstalk->watch('jobs')->ignore('default')->reserve();
echo 'Reserved job #' . $job->getId();
$pids[$job->getId()] = pcntl_fork();
if(!$pids[$job->getId()]) {
doStuff();
$pheanstalk->delete($job);
exit();
}
}
The problem is that once I fork the process, I get the error:
Reserved job #0
PHP Fatal error: Uncaught exception 'Pheanstalk_Exception_ServerException' with message 'Cannot delete job 0: NOT_FOUND'
My question is, how is it that pheanstalk returned a job with no ID and no payload? It almost feels like $pheanstalk is damaged once I fork it. If I remove the forking, everything works fine. (Though it has to wait on each process)
Put this if condition before you delete the pheanstalk job:
Thats because its very much possible that another php instance of your file has already removed that job before code reaches this place. (The other instance could still retrieve this job using reserve() until the job is deleted from the queue.