Skip to content

Commit 435d785

Browse files
authored
Merge pull request #4 from doppar/commands
queue:flush command for queue
2 parents fafeb61 + e02b357 commit 435d785

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

src/Commands/QueueFlushCommand.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 QueueFlushCommand extends Command
10+
{
11+
/**
12+
* The name of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $name = 'queue:flush {--id=}';
17+
18+
/**
19+
* The command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Delete failed job(s) by ID or all if no ID is provided';
24+
25+
/**
26+
* Execute the console command
27+
* Example: php pool queue:flush --id=1
28+
*
29+
* @return int
30+
*/
31+
protected function handle(): int
32+
{
33+
$id = $this->option('id');
34+
35+
if ($id) {
36+
return $this->flushJobById($id);
37+
}
38+
39+
FailedJob::query()
40+
->cursor(function (FailedJob $failedJob) {
41+
$failedJob->delete();
42+
$this->info("✔ Job with ID {$failedJob->id} has been deleted.");
43+
});
44+
45+
return Command::SUCCESS;
46+
}
47+
48+
protected function flushJobById(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+
$failedJob->delete();
58+
$this->info("✔ Job with ID {$failedJob->id} has been deleted.");
59+
60+
return Command::SUCCESS;
61+
}
62+
}

src/Job.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ abstract class Job implements JobInterface
1111
*
1212
* @var int
1313
*/
14-
public $tries = 3;
14+
public $tries = 1;
1515

1616
/**
1717
* The number of seconds to wait before retrying the job.

src/QueueServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Doppar\Queue;
44

55
use Doppar\Queue\Commands\MakeJobCommand;
6+
use Doppar\Queue\Commands\QueueFlushCommand;
67
use Phaseolies\Providers\ServiceProvider;
78
use Doppar\Queue\QueueManager;
89
use Doppar\Queue\Commands\QueueRunCommand;
@@ -36,7 +37,8 @@ public function boot(): void
3637
$this->commands([
3738
QueueRunCommand::class,
3839
QueueRetryCommand::class,
39-
MakeJobCommand::class
40+
MakeJobCommand::class,
41+
QueueFlushCommand::class
4042
]);
4143
}
4244
}

0 commit comments

Comments
 (0)