Laravel - Push queue task with a custom handling method and deleting job

1.4k views Asked by At

Consider this kind of code which pushes a task to queue to run a custom handler method that is located within the same class:

<?php namespace Space;

class Spaceship {

    public static function cruise()
    {
        // Throtting in 3 seconds...
        Queue::later(3, '\Space\Spaceship@throttle', $coordinates, 'queue-name');
    }

    public static function throttle($job, $data)
    {
        $job->delete();
        return 'ok';
    }

}

This worked fine before I added $job->delete(); but now its giving an error:

exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to a member function delete() on a non-object

In Laravel 4.2 docs there is no explanation why with a push queue task pointed to a custom handler method you wouldn't need to explicitly delete the task from the queue with $job->delete() ? Yet that kind of implementation works and when the $job parameter is logged, it's false.

1

There are 1 answers

2
Mysteryos On BEST ANSWER

Go to app\config\queue.php and check the default key. If the latter is set to sync, then the above behaviour is expected.

sync driver runs your task immediately, which means, Queue::later is in truth Queue::push.

Try converting your Queue::later into Queue::push and the code will run without errors.

As for explicit deletion of task in the queue, it is for most part, unnecessary. However, you have to account for queue services which don't provide for automatic deletion of the job.

For my part, AWS SQS (Message Queue Service) automatically deletes the job after it is pulled from the queue tube.