Skip to content

Commit

Permalink
Additional commands
Browse files Browse the repository at this point in the history
  • Loading branch information
medilies committed Aug 25, 2024
1 parent 187e12b commit 7dc6f2c
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 5 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ DB::transaction(function () {
});
```

> [!WARNING]
> Make sure to not call `Rmq::stage` within a loop since each call does a database insertion.
### Phase 2: Deleting the files

Delete the files staged by the singleton:
Expand All @@ -123,7 +126,7 @@ use Medilies\RmQ\Facades\RmQ;
RmQ::deleteAll();
```

Delete all the staged files using a command:
Delete all the staged files using a command (you can also [schedule](https://laravel.com/docs/11.x/scheduling#scheduling-artisan-commands) it):

```shell
php artisan rm-q:delete
Expand All @@ -141,8 +144,6 @@ Route::put('edit-user-details', function (Request $request) {
})->middleware(RmqMiddleware::class);
```

Using a Queued Job (TODO).

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
25 changes: 25 additions & 0 deletions src/Commands/RmqCleanupCommand.php
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;
}
}
2 changes: 0 additions & 2 deletions src/Commands/RmqDeleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class RmqDeleteCommand extends Command

public $description = 'Delete staged files';

// TODO: add a job

public function handle(): int
{
RmQ::deleteAll();
Expand Down
47 changes: 47 additions & 0 deletions src/Commands/RmqStatsCommand.php
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',
};
}
}
14 changes: 14 additions & 0 deletions src/Models/RmqFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* @property ?Carbon $deleted_at
*
* @method static Builder|static whereStaged()
* @method static Builder|static whereDeleted()
* @method static Builder|static whereFailed()
* @method static Builder|static whereInstance(?string $instance)
* @method static Builder|static whereBeforeSeconds(int $beforeSeconds)
*/
Expand Down Expand Up @@ -77,6 +79,18 @@ public function scopeWhereStaged(Builder $query): void
$query->where('status', static::STAGED);
}

/** @param Builder<static> $query */
public function scopeWhereDeleted(Builder $query): void
{
$query->where('status', static::DELETED);
}

/** @param Builder<static> $query */
public function scopeWhereFailed(Builder $query): void
{
$query->where('status', static::FAILED);
}

/** @param Builder<static> $query */
public function scopeWhereInstance(Builder $query, ?string $instance): void
{
Expand Down
4 changes: 4 additions & 0 deletions src/RmqServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Medilies\RmQ;

use Illuminate\Support\ServiceProvider;
use Medilies\RmQ\Commands\RmqCleanupCommand;
use Medilies\RmQ\Commands\RmqDeleteCommand;
use Medilies\RmQ\Commands\RmqStatsCommand;

class RmqServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -47,6 +49,8 @@ protected function registerCommands(): void

$this->commands([
RmqDeleteCommand::class,
RmqCleanupCommand::class,
RmqStatsCommand::class,
]);
}
}
25 changes: 25 additions & 0 deletions tests/laravel/RmqCleanupCommandTest.php
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.
34 changes: 34 additions & 0 deletions tests/laravel/RmqStatsCommandTest.php
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);
});

0 comments on commit 7dc6f2c

Please sign in to comment.