Skip to content

Commit 21b5fdf

Browse files
authored
Merge pull request #1 from doppar/commands
queue retry command done
2 parents 52635bd + 6b07307 commit 21b5fdf

3 files changed

Lines changed: 93 additions & 30 deletions

File tree

src/Commands/QueueRetryCommand.php

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

src/Commands/QueueRunCommand.php

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public function __construct(QueueManager $manager)
5656
protected function handle(): int
5757
{
5858
return $this->withTiming(function () {
59-
$queue = $this->getOption('queue', 'default');
60-
$sleep = (int) $this->getOption('sleep', 3);
61-
$maxMemory = (int) $this->getOption('memory', 128);
62-
$maxTime = (int) $this->getOption('timeout', 3600);
59+
$queue = $this->option('queue', 'default');
60+
$sleep = (int) $this->option('sleep', 3);
61+
$maxMemory = (int) $this->option('memory', 128);
62+
$maxTime = (int) $this->option('timeout', 3600);
6363

6464
$this->info("Starting queue worker on queue: {$queue}");
6565
$this->info("Configuration: sleep={$sleep}s, memory={$maxMemory}MB, timeout={$maxTime}s");
@@ -79,29 +79,4 @@ protected function handle(): int
7979
}
8080
}, 'Queue worker stopped gracefully.');
8181
}
82-
83-
/**
84-
* Get an option value.
85-
*
86-
* @param string $key
87-
* @param mixed $default
88-
* @return mixed
89-
*/
90-
protected function getOption(string $key, $default = null)
91-
{
92-
// Implementation depends on your console command system
93-
// This is a placeholder - adapt to your actual implementation
94-
global $argv;
95-
96-
foreach ($argv as $i => $arg) {
97-
if (strpos($arg, "--{$key}=") === 0) {
98-
return substr($arg, strlen("--{$key}="));
99-
}
100-
if ($arg === "--{$key}" && isset($argv[$i + 1])) {
101-
return $argv[$i + 1];
102-
}
103-
}
104-
105-
return $default;
106-
}
10782
}

src/QueueServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Phaseolies\Providers\ServiceProvider;
66
use Doppar\Queue\QueueManager;
77
use Doppar\Queue\Commands\QueueRunCommand;
8+
use Doppar\Queue\Commands\QueueRetryCommand;
89

910
class QueueServiceProvider extends ServiceProvider
1011
{
@@ -32,7 +33,8 @@ public function boot(): void
3233
], 'migrations');
3334

3435
$this->commands([
35-
QueueRunCommand::class
36+
QueueRunCommand::class,
37+
QueueRetryCommand::class
3638
]);
3739
}
3840
}

0 commit comments

Comments
 (0)