laravel / horizon

Dashboard and code-driven configuration for Laravel queues.
https://laravel.com/docs/horizon
MIT License
3.87k stars 657 forks source link

Remove a job from queue #714

Closed mhndev closed 4 years ago

mhndev commented 4 years ago

Isn't it nice to be able to remove a job from a queue by its identifier or by its tags ? consider I have job which manipulate my main image and create multiple versions of each image (thumb, ... ) when an image is replaced and uploaded again, I need to be able to remove older job. This is how I think it should be : when dispatching a job we can give it and identifier or some tags or ..., and there is a builtin laravel functionality which helps us remove the job .

for example

Job::removeById($id);

or

Job::removeByTag($tag);
driesvints commented 4 years ago

Jobs already have an id or tag so I believe this technically would indeed be possible. The jobs in Laravel have a delete method which can be called.

mhndev commented 4 years ago

Hi @driesvints, can you please show me how can I delete a job? I know that a job is a record in the broker, which can be redis, database (mysql) , ... and I know somehow I can delete that data using underlying connection to the broker. but I don't think that's the best way to do so.

driesvints commented 4 years ago

It has a delete method.

driesvints commented 4 years ago

Going to close this as we've only received one request for this. We're still open to PRs.

ziming commented 3 years ago

Adding my request too. It will be great to cancel a job or multiple jobs via the horizon UI

BlueBayPhil commented 2 years ago

Adding my request to this too. Surprised its not a feature to be honest.

mennorenkens commented 2 years ago

This would be convenient! :)

dimzeta commented 2 years ago

That will be nice!

erickneverson commented 2 years ago

+1

robbanl commented 1 year ago

+1

wdda commented 1 year ago

Hi! I spent half a day looking for an opportunity to remove a job from Laravel + Redis + Horizon.

I have accessed any queue data, but I have not been able to successfully delete the job.

//Photo model

public function deleteJobs()
{
    $queueItems = Redis::connection()->lrange('queues:test', 0, -1);

    foreach ($queueItems as $key => $queueItem) {
        $decodedItem = json_decode($queueItem, true);
        $jobId = $decodedItem['id'];

        $data = (array)unserialize($decodedItem['data']['command']);

        if ($data['photoId'] == $this->id) {
            //???
        }
    }
}

It is strange that there is an easy way to delete only for failed jobs.

And also any attempts to delete a uniqueID with a slash do not bring results.

public function deleteJobs()
{
    $redis = \Queue::getRedis();

    $queueItems = $redis->lrange('queues:test', 0, -1);

    foreach ($queueItems as $key => $queueItem) {
        $decodedItem = json_decode($queueItem, true);
        $jobId = $decodedItem['id'];

        $data = (array)unserialize($decodedItem['data']['command']);

        if ($data['photoId'] == $this->id) {
            Redis::command('del', ['laravel_horizon:' . $jobId]);

            $cursor = 0;
            $keys = [];
            do {
                // laravel_database_laravel_cache_:laravel_unique_job:App\\Jobs\\FindClientJobfind_client_6_12
                $result = Redis::scan($cursor, 'MATCH', 'laravel_database_laravel_cache_:laravel_unique_job:*FindClientJobfind_client_' . $this->id . '_*');
                $cursor = $result[0];
                $keys = array_merge($keys, $result[1]);
            } while ($cursor !== '0');

            if (!empty($keys)) {
                Redis::command('del', $keys);
            }

            echo $jobId;
        }
    }
}
noatudor commented 9 months ago

I've spent an hour on this issue and came up with the following solution: You can use the ZREM command from Redis to remove the pending jobs from Laravel Horizon. Here's how you can implement it:

Redis::connection('horizon')->command('ZREM', ['laravel_horizon:pending_jobs', 'job-id']);

rinkla3024 commented 7 months ago

In the Failed Jobs, there is a retry button. Could we add a Cancel button on the pending jobs screen?