Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prunable method to QueueMonitor model and #28

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/fix-php-code-style-issues.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ jobs:
php-code-styling:
runs-on: ubuntu-latest

permissions:
# Give the default GITHUB_TOKEN write permission to commit and push the
# added or changed files to the repository.
contents: write

steps:
- name: Checkout code
uses: actions/checkout@v4
Expand All @@ -18,4 +23,4 @@ jobs:
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Fix styling
commit_message: Fix styling
43 changes: 32 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,47 @@ return [
];
```

### Extending Model

Sometimes it's useful to extend the model to add some custom methods. You can do it by extending the model by creating your own model :

```php
$ php artisan make:model MyQueueMonitor
```

Then you can extend the model by adding your own methods :

```php

<?php

namespace App\Models;

use \Croustibat\FilamentJobsMonitor\Models\QueueMonitor as CroustibatQueueMonitor;

class MyQueueMonitor extends CroustibatQueueMonitor {}

```

### Using Filament Panels

If you are using Filament Panels, you can register the Plugin to your Panel configuration. This will register the plugin's resources as well as allow you to set configuration using optional chainable methods.

For example in your `app/Providers/Filament/AdminPanelProvider.php` file:

```php
<?php


use \Croustibat\FilamentJobsMonitor\FilamentJobsMonitorPlugin;

...

public function panel(Panel $panel): Panel
{
return $panel
->plugins([
\Croustibat\FilamentJobsMonitor\FilamentJobsMonitorPlugin::make()
->label('Job')
->pluralLabel('Jobs')
->enableNavigation(true)
->navigationIcon('heroicon-o-cpu-chip')
->navigationGroup('Settings')
->navigationSort(5)
->navigationCountBadge(true)
->enablePruning(true)
->pruningRetention(7)
->resource(\App\Filament\Resources\CustomJobMonitorResource::class)
FilamentJobsMonitorPlugin::make()
]);
}
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"require": {
"php": "^8.1|^8.2",
"php": "^8.1|^8.2|^8.3",
"filament/filament": "^3.0",
"illuminate/contracts": "^10.0",
"spatie/laravel-package-tools": "^1.13.5"
Expand Down
2 changes: 1 addition & 1 deletion examples/UsersCsvExportJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class UsersCsvExportJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, QueueProgress;
use Dispatchable, InteractsWithQueue, Queueable, QueueProgress, SerializesModels;

/**
* The data to be exported as CSV.
Expand Down
5 changes: 3 additions & 2 deletions src/Models/QueueMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Croustibat\FilamentJobsMonitor\FilamentJobsMonitorPlugin;
use Illuminate\Contracts\Queue\Job as JobContract;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -74,7 +75,7 @@ public function isFinished(): bool
return true;
}

return null !== $this->finished_at;
return $this->finished_at !== null;
}

/**
Expand Down Expand Up @@ -102,7 +103,7 @@ public function hasSucceeded(): bool
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function prunable()
public function prunable(): Builder|bool
{
if (FilamentJobsMonitorPlugin::get()->getPruning()) {
return static::where('created_at', '<=', now()->subDays(FilamentJobsMonitorPlugin::get()->getPruningRetention()));
Expand Down
6 changes: 3 additions & 3 deletions src/QueueMonitorProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ protected static function jobStarted(JobContract $job): void
/**
* Finish Queue Monitoring for Job.
*/
protected static function jobFinished(JobContract $job, bool $failed = false, \Throwable $exception = null): void
protected static function jobFinished(JobContract $job, bool $failed = false, ?\Throwable $exception = null): void
{
$monitor = QueueMonitor::query()
->where('job_id', self::getJobId($job))
->where('attempt', $job->attempts())
->orderByDesc('started_at')
->first();

if (null === $monitor) {
if ($monitor === null) {
return;
}

Expand All @@ -96,7 +96,7 @@ protected static function jobFinished(JobContract $job, bool $failed = false, \T
'failed' => $failed,
];

if (null !== $exception) {
if ($exception !== null) {
$attributes += [
'exception_message' => mb_strcut($exception->getMessage(), 0, 65535),
];
Expand Down
25 changes: 24 additions & 1 deletion src/Resources/QueueMonitorResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use Filament\Resources\Resource;
use Filament\Tables\Actions\DeleteBulkAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;

class QueueMonitorResource extends Resource
Expand Down Expand Up @@ -49,7 +51,6 @@ public static function table(Table $table): Table
TextColumn::make('status')
->badge()
->label(__('filament-jobs-monitor::translations.status'))
->sortable()
->formatStateUsing(fn (string $state): string => __("filament-jobs-monitor::translations.{$state}"))
->color(fn (string $state): string => match ($state) {
'running' => 'primary',
Expand All @@ -74,6 +75,28 @@ public static function table(Table $table): Table
->defaultSort('started_at', 'desc')
->bulkActions([
DeleteBulkAction::make(),
])
->filters([
SelectFilter::make('status')
->options([
'running' => 'Running',
'succeeded' => 'Succeeded',
'failed' => 'Failed',
])
->query(function (Builder $query, array $data) {
if ($data['value'] === 'succeeded') {
return $query
->whereNotNull('finished_at')
->where('failed', 0);
} elseif ($data['value'] === 'failed') {
return $query
->whereNotNull('finished_at')
->where('failed', 1);
} elseif ($data['value'] === 'running') {
return $query
->whereNull('finished_at');
}
}),
]);
}

Expand Down