Skip to content

Commit

Permalink
Refactor job list command and add job clear command
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Jan 15, 2024
1 parent 6f8930f commit 5087c18
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
40 changes: 40 additions & 0 deletions src/Commands/Jobs/JobClearCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Kiwilan\Steward\Commands\Jobs;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Kiwilan\Steward\Commands\Commandable;

class JobClearCommand extends Commandable
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'job:clear';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Clear jobs.';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$this->title();

$this->info('Clearing all jobs...');
DB::table('jobs')->truncate();
$this->info('All jobs cleared.');

return Command::SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace Kiwilan\Steward\Commands;
namespace Kiwilan\Steward\Commands\Jobs;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Kiwilan\Steward\Commands\Commandable;

class JobListCommand extends Commandable
{
Expand All @@ -13,7 +14,6 @@ class JobListCommand extends Commandable
* @var string
*/
protected $signature = 'job:list
{--c|clear : clear all jobs}
{--l|limit= : limit jobs output}
{--f|full : display full job informations}
{--count : count of jobs}';
Expand All @@ -34,7 +34,6 @@ public function handle()
{
$this->title();

$clear = (bool) $this->option('clear') ?: false;
$limit = $this->option('limit') ?: false;

if ($limit && ! is_numeric($limit)) {
Expand All @@ -51,14 +50,6 @@ public function handle()
return Command::SUCCESS;
}

if ($clear) {
$this->info('Clearing all jobs...');
$this->clearAll();
$this->info('All jobs cleared.');

return Command::SUCCESS;
}

$this->parseJobs($limit, $full);

return Command::SUCCESS;
Expand All @@ -75,7 +66,6 @@ private function parseJobs(int|false $limit, bool $full): void
'id' => $job->id,
'queue' => $job->queue,
'payload' => json_decode($job->payload)->displayName,
'reserved_at' => $job->reserved_at,
'available_at' => date('Y-m-d H:i:s', substr($job->available_at, 0, 10)),
];

Expand All @@ -100,14 +90,4 @@ private function parseJobs(int|false $limit, bool $full): void
$table = $full ? ['id', 'queue', 'payload', 'attempts', 'reserved_at', 'available_at'] : ['id', 'queue', 'payload', 'reserved_at', 'available_at'];
$this->table($table, $items);
}

/**
* Clear all jobs.
*
* @return void
*/
protected function clearAll()
{
DB::table('jobs')->truncate();
}
}
3 changes: 2 additions & 1 deletion src/StewardServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function configurePackage(Package $package): void
\Kiwilan\Steward\Commands\TagCleanCommand::class,
\Kiwilan\Steward\Commands\Optimize\OptimizeFeshCommand::class,
\Kiwilan\Steward\Commands\ClearFreshCommand::class,
\Kiwilan\Steward\Commands\JobListCommand::class,
\Kiwilan\Steward\Commands\Jobs\JobListCommand::class,
\Kiwilan\Steward\Commands\Jobs\JobClearCommand::class,
])
;
}
Expand Down

0 comments on commit 5087c18

Please sign in to comment.