-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
153 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Medilies\RmQ\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Medilies\RmQ\Models\RmqFile; | ||
|
||
class RmqCleanupCommand extends Command | ||
{ | ||
public $signature = 'rm-q:cleanup'; | ||
|
||
public $description = 'Cleanup the table from records that were handled successfully'; | ||
|
||
// ? add --with-failed option | ||
// ? --before | ||
|
||
public function handle(): int | ||
{ | ||
RmqFile::whereDeleted()->delete(); | ||
|
||
$this->comment('Table cleaned up.'); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Medilies\RmQ\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\DB; | ||
use Medilies\RmQ\Models\RmqFile; | ||
|
||
class RmqStatsCommand extends Command | ||
{ | ||
public $signature = 'rm-q:stats'; | ||
|
||
public $description = 'Get records count'; | ||
|
||
public function handle(): int | ||
{ | ||
/** @var array<array{Status: string, Count: int}> */ | ||
$rows = RmqFile::select('status', DB::raw('COUNT(*) as total')) // @phpstan-ignore-line | ||
->groupBy('status') | ||
->get() | ||
->map(fn (RmqFile $row) => [ | ||
'Status' => $this->getStatusLabel($row->status), | ||
'Count' => $row->total, // @phpstan-ignore-line | ||
]) | ||
->toArray(); | ||
|
||
$this->table(['Status', 'Count'], [ | ||
...$rows, | ||
[ | ||
'Status' => 'total', | ||
'Count' => array_sum(array_column($rows, 'Count')), | ||
], | ||
]); | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
private function getStatusLabel(int $status): string | ||
{ | ||
return match ($status) { | ||
RmqFile::STAGED => 'Staged', | ||
RmqFile::DELETED => 'Deleted', | ||
RmqFile::FAILED => 'Failed', | ||
default => 'Unknown', | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
use Medilies\RmQ\Commands\RmqCleanupCommand; | ||
use Medilies\RmQ\Facades\RmQ; | ||
use Medilies\RmQ\Models\RmqFile; | ||
use Tests\OrchestraTestCase; | ||
|
||
$signature = (new RmqCleanupCommand)->signature; | ||
|
||
test($signature, function () use ($signature) { | ||
/** @var OrchestraTestCase $this */ | ||
$files = populateFiles(); | ||
|
||
RmQ::stage($files[0]); | ||
Rmq::delete(); | ||
|
||
$this->assertDatabaseHas(RmqFile::tableName(), [ | ||
'path' => $files[0], | ||
'status' => RmqFile::DELETED, | ||
]); | ||
|
||
$this->artisan($signature)->assertExitCode(0); | ||
|
||
$this->assertDatabaseCount(RmqFile::tableName(), 0); | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
use Medilies\RmQ\Commands\RmqStatsCommand; | ||
use Medilies\RmQ\Facades\RmQ; | ||
use Medilies\RmQ\Models\RmqFile; | ||
use Tests\OrchestraTestCase; | ||
|
||
$signature = (new RmqStatsCommand)->signature; | ||
|
||
test($signature, function () use ($signature) { | ||
/** @var OrchestraTestCase $this */ | ||
$files = populateFiles(2); | ||
$filesCount = count($files); | ||
|
||
RmQ::stage($files); | ||
Rmq::delete(); | ||
|
||
$this->assertDatabaseCount(RmqFile::tableName(), $filesCount) | ||
->assertDatabaseHas(RmqFile::tableName(), [ | ||
'path' => $files[0], | ||
'status' => RmqFile::DELETED, | ||
]); | ||
|
||
RmQ::stage($files[1]); | ||
|
||
$this->assertDatabaseHas(RmqFile::tableName(), [ | ||
'path' => $files[1], | ||
'status' => RmqFile::STAGED, | ||
]); | ||
|
||
$this->artisan($signature)->assertExitCode(0); | ||
|
||
depopulateFiles($files); | ||
}); |