Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting queued jobs in laravel

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.

like image 424
Claire Avatar asked Dec 10 '13 15:12

Claire


People also ask

How do I delete a job in Laravel?

app('queue')->getDatabase()->table('jobs')->where('id', $job_id)->delete();

What is queue job in Laravel?

The Laravel queue service provides a unified API across a variety of different queue back-ends. Queues allow you to defer the processing of a time consuming task, such as sending an e-mail, until a later time which drastically speeds up web requests to your application.


5 Answers

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
like image 180
DutGRIFF Avatar answered Oct 03 '22 18:10

DutGRIFF


Restart Beanstalk. On Ubuntu:

sudo service beanstalkd restart
like image 36
Jerome Jaglale Avatar answered Oct 03 '22 17:10

Jerome Jaglale


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

like image 41
morrislaptop Avatar answered Oct 03 '22 17:10

morrislaptop


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
like image 21
Ahmad Hajjar Avatar answered Oct 03 '22 17:10

Ahmad Hajjar


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
like image 1
Guy Avatar answered Oct 03 '22 17:10

Guy