Question

I have added some jobs to a queue in Laravel. However, I forgot to put $job->delete() in the function and there is an error in my function. This means the job is never ending. It keeps going being replaced onto the queue and keeps erroring in my log file. How can I delete it from the command line?

I am using beanstalkd for my queuing.

Was it helpful?

Solution 2

Restart Beanstalk. On Ubuntu:

sudo service beanstalkd restart

OTHER TIPS

I am using Redis instead of Beanstalkd but this should be the same in both. Restarting Redis doesn't solve the problem. I looked at RedisQueues in the Laravel 4.2 API Docs and found:

public Job|null pop(string $queue = null)
  //Pop the next job off of the queue.

This is the same if you look at BeanstalkedQueue.

I threw it in app/routes.php inside dd*, loaded that page and voila.

Route::get('/', function() {
  dd(Queue::pop());
  #return View::make('hello');
});

NOTE: Reload the page once per queue.

The queue was pulled off the stack. I would like to see a cleaner solution but this worked for me more than once.

*dd($var) = Laravel's die and dump function = die(var_dump($var))

Edit 1: For Redis

The above obviously isn't the best solution so here is a better way. Be careful!

FLUSHDB - Delete all the keys of the currently selected DB. This command never fails.

For Redis use FLUSHDB. This will flush the Redis database not Laravel's database. In the terminal:

$ redis-cli
127.0.0.1:6379> FLUSHDB
OK
127.0.0.1:6379> exit

I made an artisan command which will clear all the jobs in your queue. You can optionally specify the connection and/or the pipe.

https://github.com/morrislaptop/laravel-queue-clear

Important note: This solution works only for beanstalk

There are two solutions:

1- From Your PHP Code

To delete jobs programatically, you can do this:

    //Que the job first. (YourJobProcessor is a class that has a method called fire like `fire($job,$data)`
    $res = Queue::later(5, YourJobProcessor::class, $data, 'queue_name');
    //get the job from the que that you just pushed it to
    $job = Queue::getPheanstalk()->useTube("queue_name")->peek($res);
    //get the job from the que that you just pushed it to
    $res = Queue::getPheanstalk()->useTube("queue_name")->delete($job);

If everything went good, the job will not execute, else the job will execute after 5 seconds

2- From Command Line (Linux and Mac only)

From command line (In linux and mac) you can use beanstool.

For example, if you want to delete 100 ready jobs from the queue_name tube you can do the following:

  for i in {1..100}; do beanstool delete -t queue_name --state=ready; done

Only way I could do it was to restart my computer. Could not find a way to delete a job.

I've used this php-based web admin console in the past.

Otherwise, I believe you'll find yourself using Terminal + telnet, altho I can't find any documentation for deleting via telnet (Just viewing a list of jobs in queue).

It seems that most articles tell you to use your code+library of choice and loop around queues jobs to delete them in this situation.

Here is Laravel 5.1 compatible command, which allows you to clear Beanstalkd queue. The command takes queue name as argument ('default' by default). Do not forget to register it in app/Console/Kernel.php

For Redis users, instead of flushing, using redis-cli I ran this command:

KEYS *queue*

on the Redis instance holding queued jobs, then deleted whatever keys in the response

DEL queues:default queues:default:reserved
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top