forked from doppar/queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueRetryCommand.php
More file actions
86 lines (69 loc) · 2.08 KB
/
QueueRetryCommand.php
File metadata and controls
86 lines (69 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace Doppar\Queue\Commands;
use Phaseolies\Console\Schedule\Command;
use Doppar\Queue\QueueManager;
use Doppar\Queue\Models\FailedJob;
class QueueRetryCommand extends Command
{
/**
* The name of the console command.
*
* @var string
*/
protected $name = 'queue:retry {--id=}';
/**
* The command description.
*
* @var string
*/
protected $description = 'Retry failed job(s) by ID or all if no ID is provided';
/**
* Execute the console command
* Example: php pool queue:retry --id=4
*
* @return int
*/
public function handle(): int
{
$id = $this->option('id');
$manager = app(QueueManager::class);
if ($id) {
return $this->retryJobById($manager, $id);
}
FailedJob::query()
->cursor(function (FailedJob $failedJob) use ($manager) {
$this->retryFailedJob($manager, $failedJob);
});
return Command::SUCCESS;
}
protected function retryJobById(QueueManager $manager, int $id): int
{
$failedJob = FailedJob::find($id);
if (!$failedJob) {
$this->error("Failed job with ID {$id} not found.");
return Command::FAILURE;
}
if ($this->retryFailedJob($manager, $failedJob)) {
return Command::SUCCESS;
}
return Command::FAILURE;
}
protected function retryFailedJob(QueueManager $manager, FailedJob $failedJob): bool
{
try {
$job = $manager->unserializeJob($failedJob->payload);
$jobClass = get_class($job);
// Reset attempts
$job->attempts = 0;
// Push back to queue
$manager->push($job);
// Delete from failed jobs
$failedJob->delete();
$this->info("✔ Retried job [{$jobClass}] (ID: {$failedJob->id})");
return true;
} catch (\Throwable $e) {
$this->error("✖ Failed to retry job ID {$failedJob->id}: " . $e->getMessage());
return false;
}
}
}