Closed mhndev closed 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.
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.
It has a delete
method.
Going to close this as we've only received one request for this. We're still open to PRs.
Adding my request too. It will be great to cancel a job or multiple jobs via the horizon UI
Adding my request to this too. Surprised its not a feature to be honest.
This would be convenient! :)
That will be nice!
+1
+1
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;
}
}
}
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']);
In the Failed Jobs, there is a retry button. Could we add a Cancel button on the pending jobs screen?
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
or