6

I need to check if job added to queue (Beanstalkd) has been completed in Laravel (Laravel 5) and in case it's completed I need to return updated record (task added to queue updates record in database). I've added to my composer.json:

"pda/pheanstalk": "3.*"

I add job to queue this way:

$jobId = Queue::push('App\Class', $object->toArray(), $this->getQueueName());

I use to check if job was completed using is the following function:

public function find($queueName, $jobId, $recordId)
{
    $phean = Queue::getPheanstalk();

    try {
        $phean->peek($jobId);
        $data = ['status' => 'waiting'];
    } catch (ServerException $e) {
        $message = $e->getMessage();

        if ($message == 'NOT_FOUND: Job ' . $jobId . ' does not exist.') {
            $data = ... // here I get from database data for $recordId

        } else {
            $data = ['status' => 'error'];
        }
    }

    return $data;
}

The question is - is that reliable method to check if job has been completed? I compare here just message I get from exception. I haven't found any other way to check if job has been completed.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291

3 Answers3

1

I haven't used Laravel 5 yet, but on Laravel 4, you have the failed jobs table. Where you can see which jobs DIDN'T complete. I'm assuming that L5 might have something or keep the same process. That wouldn't solve your problem?

From my point of view it seems that you are only inverting the perspective. Instead of looking for what failed, you are looking for what worked.

Source

Madhuri Patel
  • 1,270
  • 12
  • 24
Eduardo Cruz
  • 617
  • 6
  • 18
0

I do not use Laravel 5 yet. But with Laravel 4, when a job is finished, it needs to be manually deleted from the queue. Here is the official documentation:

Deleting A Processed Job: Once you have processed a job, it must be deleted from the queue, which can be done via the delete method on the Job instance

It simply means that if you have not use such a method, the job status will remain unfinished. In your case, you can get the queue and see if the job id is there.

Ray
  • 631
  • 5
  • 6
  • You are right, but job goes to failed jobs when it fails. So when I want to check if job has been completed even if I looked into failed jobs, I need to check somehow if job is still in the queue because it may be still in the queue and not in failed jobs. – Marcin Nabiałek Jan 13 '15 at 05:42
  • Hey, it seems that pbeanstalk does not offer a way to get a whole list of running jobs. You may store job-related info into database and mark its status when it's deleted or failed. – Ray Jan 13 '15 at 07:21
0

If you need to trigger a command based upon a job finishing - then it sounds like you should just fire an queued command when the job is complete - then your system can handle the rest?

Otherwise 'polling' for the job to see if/when it is completed seems cumbersome and inefficient.

In Laravel 5 - you could event use the new Commands tool that Taylor has provided, and have the ShouldBeQueued implementation applied - so the command is queued to run at the completion of your other task.

Laurence
  • 58,936
  • 21
  • 171
  • 212