Created
December 8, 2022 14:17
-
-
Save Gummibeer/5a6acdd7d74c6498a89c7a6fb3584aad to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
namespace App\Nova; | |
use App\Nova\Fields\DateTime; | |
use Carbon\CarbonImmutable; | |
use Cron\CronExpression; | |
use Illuminate\Http\Request; | |
use Laravel\Nova\Fields\Boolean; | |
use Laravel\Nova\Fields\Text; | |
use Lorisleiva\CronTranslator\CronTranslator; | |
use Spatie\ScheduleMonitor\Models\MonitoredScheduledTask; | |
class ScheduledTask extends Resource | |
{ | |
public static $model = MonitoredScheduledTask::class; | |
public static $title = 'name'; | |
public static $globallySearchable = false; | |
public static $searchable = false; | |
public static $polling = true; | |
public static $pollingInterval = 30; | |
public function fields(Request $request): array | |
{ | |
return [ | |
Boolean::make('Healthy') | |
->resolveUsing(function ($value, MonitoredScheduledTask $model): bool { | |
$previous = CarbonImmutable::instance( | |
(new CronExpression($model->cron_expression))->getPreviousRunDate(timeZone: $this->scheduleTimezone()) | |
); | |
return $previous->lessThanOrEqualTo($model->last_started_at); | |
}), | |
Text::make('Name') | |
->sortable(), | |
Text::make('Cron Expression') | |
->resolveUsing(function (string $cron): string { | |
$readable = CronTranslator::translate($cron, 'en', true); | |
$next = CarbonImmutable::instance( | |
(new CronExpression($cron))->getNextRunDate(timeZone: $this->scheduleTimezone()) | |
); | |
return <<<HTML | |
<p style="white-space:nowrap;"> | |
<code class="block">{$cron}</code> | |
<span class="block text-sm">{$readable}</span> | |
<time datetime="{$next->toIso8601String()}" class="block text-xs"> | |
{$next->format('D, d M Y H:i')} | |
</time> | |
<time datetime="{$next->toIso8601String()}" class="block text-xs"> | |
{$next->diffForHumans()} | |
</time> | |
</p> | |
HTML; | |
}) | |
->textAlign('right') | |
->asHtml(), | |
DateTime::make('Last Started At') | |
->sortable() | |
->textAlign('right'), | |
DateTime::make('Last Finished At') | |
->sortable() | |
->textAlign('right'), | |
DateTime::make('Last Skipped At') | |
->sortable() | |
->textAlign('right'), | |
DateTime::make('Last Failed At') | |
->sortable() | |
->textAlign('right'), | |
]; | |
} | |
protected function scheduleTimezone(): string | |
{ | |
return config('app.schedule_timezone', config('app.timezone')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment