Let schedules run in a chosen timezone (#2626)
This commit is contained in:
1 parent
35ca947558
commit
46fc1cf0eb
12 files changed
+122
-14
No files matched your search
@@ -51,13 +51,16 @@ class CreateSchedule extends CreateRecord
|
||||
$data['server_id'] = $server->id;
|
||||
}
|
||||
|
||||
$data['timezone'] = user()->timezone ?? 'UTC';
|
||||
|
||||
if (!isset($data['next_run_at'])) {
|
||||
$data['next_run_at'] = ScheduleResource::getNextRun(
|
||||
$data['cron_minute'],
|
||||
$data['cron_hour'],
|
||||
$data['cron_day_of_month'],
|
||||
$data['cron_month'],
|
||||
$data['cron_day_of_week']
|
||||
$data['cron_day_of_week'],
|
||||
$data['timezone']
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -37,12 +37,14 @@ class EditSchedule extends EditRecord
|
||||
|
||||
protected function mutateFormDataBeforeSave(array $data): array
|
||||
{
|
||||
$data['timezone'] = user()->timezone ?? 'UTC';
|
||||
$data['next_run_at'] = ScheduleResource::getNextRun(
|
||||
$data['cron_minute'],
|
||||
$data['cron_hour'],
|
||||
$data['cron_day_of_month'],
|
||||
$data['cron_month'],
|
||||
$data['cron_day_of_week']
|
||||
$data['cron_day_of_week'],
|
||||
$data['timezone']
|
||||
);
|
||||
|
||||
return $data;
|
||||
|
||||
@@ -101,7 +101,7 @@ class ScheduleResource extends Resource
|
||||
Section::make(trans('server/schedule.cron'))
|
||||
->description(function (Get $get) {
|
||||
try {
|
||||
$nextRun = Utilities::getScheduleNextRunDate($get('cron_minute'), $get('cron_hour'), $get('cron_day_of_month'), $get('cron_month'), $get('cron_day_of_week'))->timezone(user()->timezone ?? 'UTC');
|
||||
$nextRun = Utilities::getScheduleNextRunDate((string) $get('cron_minute'), (string) $get('cron_hour'), (string) $get('cron_day_of_month'), (string) $get('cron_month'), (string) $get('cron_day_of_week'), user()->timezone ?? 'UTC')->timezone(user()->timezone ?? 'UTC');
|
||||
} catch (Exception) {
|
||||
$nextRun = trans('server/schedule.invalid');
|
||||
}
|
||||
@@ -312,7 +312,8 @@ class ScheduleResource extends Resource
|
||||
->searchable(),
|
||||
TextColumn::make('cron')
|
||||
->label(trans('server/schedule.cron'))
|
||||
->state(fn (Schedule $schedule) => $schedule->cron_minute . ' ' . $schedule->cron_hour . ' ' . $schedule->cron_day_of_month . ' ' . $schedule->cron_month . ' ' . $schedule->cron_day_of_week),
|
||||
->state(fn (Schedule $schedule) => $schedule->cron_minute . ' ' . $schedule->cron_hour . ' ' . $schedule->cron_day_of_month . ' ' . $schedule->cron_month . ' ' . $schedule->cron_day_of_week)
|
||||
->description(fn (Schedule $schedule) => $schedule->timezone),
|
||||
TextColumn::make('status')
|
||||
->label(trans('server/schedule.status'))
|
||||
->state(fn (Schedule $schedule) => $schedule->status->getLabel())
|
||||
@@ -379,10 +380,10 @@ class ScheduleResource extends Resource
|
||||
];
|
||||
}
|
||||
|
||||
public static function getNextRun(string $minute, string $hour, string $dayOfMonth, string $month, string $dayOfWeek): Carbon
|
||||
public static function getNextRun(string $minute, string $hour, string $dayOfMonth, string $month, string $dayOfWeek, string $timezone = 'UTC'): Carbon
|
||||
{
|
||||
try {
|
||||
return Utilities::getScheduleNextRunDate($minute, $hour, $dayOfMonth, $month, $dayOfWeek);
|
||||
return Utilities::getScheduleNextRunDate($minute, $hour, $dayOfMonth, $month, $dayOfWeek, $timezone);
|
||||
} catch (Exception) {
|
||||
Notification::make()
|
||||
->title(trans('server/schedule.notification_invalid_cron'))
|
||||
|
||||
@@ -38,11 +38,12 @@ class Utilities
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function getScheduleNextRunDate(string $minute, string $hour, string $dayOfMonth, string $month, string $dayOfWeek): Carbon
|
||||
public static function getScheduleNextRunDate(string $minute, string $hour, string $dayOfMonth, string $month, string $dayOfWeek, string $timezone = 'UTC'): Carbon
|
||||
{
|
||||
// Evaluate the cron in the schedule's timezone (so DST is respected), but always return UTC for storage.
|
||||
return Carbon::instance((new CronExpression(
|
||||
sprintf('%s %s %s %s %s', $minute, $hour, $dayOfMonth, $month, $dayOfWeek)
|
||||
))->getNextRunDate(now('UTC')));
|
||||
))->getNextRunDate(now($timezone)))->utc();
|
||||
}
|
||||
|
||||
public static function checked(string $name, mixed $default): string
|
||||
|
||||
@@ -129,7 +129,7 @@ class ScheduleController extends ClientApiController
|
||||
'cron_minute' => $request->input('minute'),
|
||||
'is_active' => $active,
|
||||
'only_when_online' => (bool) $request->input('only_when_online'),
|
||||
'next_run_at' => $this->getNextRunAt($request),
|
||||
'next_run_at' => $this->getNextRunAt($request, $schedule->timezone),
|
||||
];
|
||||
|
||||
// Toggle the processing state of the scheduled task when it is enabled or disabled so that an
|
||||
@@ -186,7 +186,7 @@ class ScheduleController extends ClientApiController
|
||||
*
|
||||
* @throws DisplayException
|
||||
*/
|
||||
protected function getNextRunAt(Request $request): Carbon
|
||||
protected function getNextRunAt(Request $request, string $timezone = 'UTC'): Carbon
|
||||
{
|
||||
try {
|
||||
return Utilities::getScheduleNextRunDate(
|
||||
@@ -194,7 +194,8 @@ class ScheduleController extends ClientApiController
|
||||
$request->input('hour'),
|
||||
$request->input('day_of_month'),
|
||||
$request->input('month'),
|
||||
$request->input('day_of_week')
|
||||
$request->input('day_of_week'),
|
||||
$timezone
|
||||
);
|
||||
} catch (Exception) {
|
||||
throw new DisplayException('The cron data provided does not evaluate to a valid expression.');
|
||||
|
||||
@@ -23,6 +23,7 @@ use Illuminate\Support\Carbon;
|
||||
* @property string $cron_day_of_month
|
||||
* @property string $cron_hour
|
||||
* @property string $cron_minute
|
||||
* @property string $timezone
|
||||
* @property bool $is_active
|
||||
* @property bool $is_processing
|
||||
* @property Carbon|null $last_run_at
|
||||
@@ -83,6 +84,7 @@ class Schedule extends Model implements Validatable
|
||||
'cron_day_of_month',
|
||||
'cron_hour',
|
||||
'cron_minute',
|
||||
'timezone',
|
||||
'is_active',
|
||||
'is_processing',
|
||||
'only_when_online',
|
||||
@@ -97,6 +99,7 @@ class Schedule extends Model implements Validatable
|
||||
'cron_day_of_month' => '*',
|
||||
'cron_hour' => '*',
|
||||
'cron_minute' => '*',
|
||||
'timezone' => 'UTC',
|
||||
'is_active' => true,
|
||||
'is_processing' => false,
|
||||
'only_when_online' => false,
|
||||
@@ -111,6 +114,7 @@ class Schedule extends Model implements Validatable
|
||||
'cron_day_of_month' => ['required', 'string'],
|
||||
'cron_hour' => ['required', 'string'],
|
||||
'cron_minute' => ['required', 'string'],
|
||||
'timezone' => ['string', 'timezone'],
|
||||
'is_active' => ['boolean'],
|
||||
'is_processing' => ['boolean'],
|
||||
'only_when_online' => ['boolean'],
|
||||
@@ -145,7 +149,7 @@ class Schedule extends Model implements Validatable
|
||||
*/
|
||||
public function getNextRunDate(): string
|
||||
{
|
||||
return Utilities::getScheduleNextRunDate($this->cron_minute, $this->cron_hour, $this->cron_day_of_month, $this->cron_month, $this->cron_day_of_week)->toDateTimeString();
|
||||
return Utilities::getScheduleNextRunDate($this->cron_minute, $this->cron_hour, $this->cron_day_of_month, $this->cron_month, $this->cron_day_of_week, $this->timezone)->toDateTimeString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,6 +22,7 @@ class ScheduleExporterService
|
||||
'cron_day_of_month' => $schedule->cron_day_of_month,
|
||||
'cron_month' => $schedule->cron_month,
|
||||
'cron_day_of_week' => $schedule->cron_day_of_week,
|
||||
'timezone' => $schedule->timezone,
|
||||
|
||||
'tasks' => $schedule->tasks->map(function (Task $task) {
|
||||
return [
|
||||
|
||||
@@ -34,6 +34,7 @@ class ScheduleImporterService
|
||||
$dayOfMonth = Arr::get($parsed, 'cron_day_of_month', '*');
|
||||
$month = Arr::get($parsed, 'cron_month', '*');
|
||||
$dayOfWeek = Arr::get($parsed, 'cron_day_of_week', '*');
|
||||
$timezone = Arr::get($parsed, 'timezone') ?? 'UTC';
|
||||
|
||||
$schedule = Schedule::create([
|
||||
'server_id' => $server->id,
|
||||
@@ -45,7 +46,8 @@ class ScheduleImporterService
|
||||
'cron_day_of_month' => $dayOfMonth,
|
||||
'cron_month' => $month,
|
||||
'cron_day_of_week' => $dayOfWeek,
|
||||
'next_run_at' => Utilities::getScheduleNextRunDate($minute, $hour, $dayOfMonth, $month, $dayOfWeek),
|
||||
'timezone' => $timezone,
|
||||
'next_run_at' => Utilities::getScheduleNextRunDate($minute, $hour, $dayOfMonth, $month, $dayOfWeek, $timezone),
|
||||
]);
|
||||
|
||||
foreach (Arr::get($parsed, 'tasks', []) as $task) {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('schedules', function (Blueprint $table) {
|
||||
$table->string('timezone')->default('UTC')->after('cron_minute');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('schedules', function (Blueprint $table) {
|
||||
$table->dropColumn('timezone');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -30,7 +30,7 @@ return [
|
||||
'enabled' => 'Enable Schedule?',
|
||||
'enabled_hint' => 'This schedule will be executed automatically if enabled.',
|
||||
|
||||
'cron_body' => 'Please keep in mind that the cron inputs below always assume UTC.',
|
||||
'cron_body' => 'The cron inputs below use your timezone.',
|
||||
'cron_timezone' => 'Next run in your timezone (:timezone): <b> :next_run </b>',
|
||||
|
||||
'invalid' => 'Invalid',
|
||||
|
||||
@@ -48,6 +48,26 @@ class UpdateServerScheduleTest extends ClientApiIntegrationTestCase
|
||||
$this->assertSame($expected->toAtomString(), $schedule->next_run_at->toAtomString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that updating a schedule evaluates the cron in the schedule's timezone.
|
||||
*/
|
||||
public function test_next_run_uses_schedule_timezone(): void
|
||||
{
|
||||
[$user, $server] = $this->generateTestAccount();
|
||||
|
||||
/** @var Schedule $schedule */
|
||||
$schedule = Schedule::factory()->create(['server_id' => $server->id, 'timezone' => 'America/New_York']);
|
||||
$data = array_merge($this->updateData, ['minute' => '0', 'hour' => '3']);
|
||||
|
||||
$this->actingAs($user)
|
||||
->postJson("/api/client/servers/{$server->uuid}/schedules/{$schedule->id}", $data)
|
||||
->assertOk();
|
||||
|
||||
$nextRun = $schedule->refresh()->next_run_at->timezone('America/New_York');
|
||||
|
||||
$this->assertSame('03:00', $nextRun->format('H:i'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an error is returned if the schedule exists but does not belong to this
|
||||
* specific server instance.
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tests\Unit\Helpers;
|
||||
|
||||
use App\Helpers\Utilities;
|
||||
use App\Tests\TestCase;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class ScheduleNextRunDateTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Carbon::setTestNow();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_defaults_to_utc(): void
|
||||
{
|
||||
Carbon::setTestNow(Carbon::parse('2026-07-01 00:00:00', 'UTC'));
|
||||
|
||||
$next = Utilities::getScheduleNextRunDate('0', '3', '*', '*', '*');
|
||||
|
||||
$this->assertSame('2026-07-01 03:00:00', $next->toDateTimeString());
|
||||
$this->assertSame('UTC', $next->timezoneName);
|
||||
}
|
||||
|
||||
public function test_cron_is_evaluated_in_the_given_timezone_across_dst(): void
|
||||
{
|
||||
// Berlin is UTC+2 in summer (CEST) and UTC+1 in winter (CET).
|
||||
Carbon::setTestNow(Carbon::parse('2026-07-01 00:00:00', 'UTC'));
|
||||
$summer = Utilities::getScheduleNextRunDate('0', '3', '*', '*', '*', 'Europe/Berlin');
|
||||
|
||||
Carbon::setTestNow(Carbon::parse('2026-12-01 00:00:00', 'UTC'));
|
||||
$winter = Utilities::getScheduleNextRunDate('0', '3', '*', '*', '*', 'Europe/Berlin');
|
||||
|
||||
$this->assertSame('2026-07-01 01:00:00', $summer->toDateTimeString());
|
||||
$this->assertSame('UTC', $summer->timezoneName);
|
||||
$this->assertSame('2026-12-01 02:00:00', $winter->toDateTimeString());
|
||||
}
|
||||
|
||||
public function test_spring_forward_does_not_skip_the_day(): void
|
||||
{
|
||||
// 2026-03-29 02:00 Berlin jumps to 03:00, so 03:00 local is 01:00 UTC that day.
|
||||
Carbon::setTestNow(Carbon::parse('2026-03-28 23:00:00', 'UTC'));
|
||||
|
||||
$next = Utilities::getScheduleNextRunDate('0', '3', '*', '*', '*', 'Europe/Berlin');
|
||||
|
||||
$this->assertSame('2026-03-29 01:00:00', $next->toDateTimeString());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user