Skip to content

Commit d6e8121

Browse files
authored
Capture scheduled task sub-minute frequency (#302)
1 parent d0dab11 commit d6e8121

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/Compatibility.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ final class Compatibility
3737

3838
public static bool $queuedJobDurationCapturable = false;
3939

40+
public static bool $subMinuteScheduledTasksSupported = false;
41+
4042
/**
4143
* @var array{
4244
* nightwatch_should_sample?: bool|null,
@@ -97,6 +99,13 @@ public static function boot(Application $app): void
9799
self::$queuedJobDurationCapturable =
98100
version_compare($version, '10.42.0', '>=');
99101

102+
/**
103+
* @see https://github.com/laravel/framework/pull/47279
104+
* @see https://github.com/laravel/framework/releases/tag/v10.15.0
105+
*/
106+
self::$subMinuteScheduledTasksSupported =
107+
version_compare($version, '10.15.0', '>=');
108+
100109
/**
101110
* @see https://github.com/laravel/framework/commit/6da5093aa672d26d0357b35
102111
* @see https://github.com/laravel/framework/releases/tag/v11.5.0

src/Sensors/ScheduledTaskSensor.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Console\Scheduling\CallbackEvent;
1212
use Illuminate\Console\Scheduling\Event as SchedulingEvent;
1313
use Laravel\Nightwatch\Clock;
14+
use Laravel\Nightwatch\Compatibility;
1415
use Laravel\Nightwatch\Concerns\RecordsContext;
1516
use Laravel\Nightwatch\State\CommandState;
1617
use Laravel\Nightwatch\Types\Str;
@@ -49,19 +50,23 @@ public function __invoke(ScheduledTaskFinished|ScheduledTaskSkipped|ScheduledTas
4950
$now = $this->clock->microtime();
5051
$name = $this->normalizeTaskName($event->task);
5152
$timezone = $event->task->timezone instanceof DateTimeZone ? $event->task->timezone->getName() : $event->task->timezone;
53+
$repeatSeconds = Compatibility::$subMinuteScheduledTasksSupported && $event->task->repeatSeconds !== null ? $event->task->repeatSeconds : 0;
5254

5355
return [
5456
'v' => 1,
5557
't' => 'scheduled-task',
5658
'timestamp' => $this->commandState->timestamp,
5759
'deploy' => $this->commandState->deploy,
5860
'server' => $this->commandState->server,
59-
'_group' => hash('xxh128', "{$name},{$event->task->expression},{$timezone}"),
61+
'_group' => $repeatSeconds > 0
62+
? hash('xxh128', "{$name},{$event->task->expression},{$timezone},{$repeatSeconds}")
63+
: hash('xxh128', "{$name},{$event->task->expression},{$timezone}"),
6064
'trace_id' => $this->commandState->trace,
6165
// --- //
6266
'name' => $name,
6367
'cron' => $event->task->expression,
6468
'timezone' => $timezone,
69+
'repeat_seconds' => $repeatSeconds,
6570
'without_overlapping' => $event->task->withoutOverlapping,
6671
'on_one_server' => $event->task->onOneServer,
6772
'run_in_background' => $event->task->runInBackground,

tests/Feature/Sensors/ScheduledTaskSensorTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function test_it_ingests_processed_tasks(): void
6767
'name' => $name,
6868
'cron' => '* * * * *',
6969
'timezone' => 'UTC',
70+
'repeat_seconds' => 0,
7071
'without_overlapping' => false,
7172
'on_one_server' => false,
7273
'run_in_background' => false,
@@ -116,6 +117,7 @@ public function test_it_ingests_skipped_tasks(): void
116117
'name' => $name,
117118
'cron' => '* * * * *',
118119
'timezone' => 'UTC',
120+
'repeat_seconds' => 0,
119121
'without_overlapping' => false,
120122
'on_one_server' => false,
121123
'run_in_background' => false,
@@ -167,6 +169,7 @@ public function test_it_ingests_failed_tasks(): void
167169
'name' => $name,
168170
'cron' => '* * * * *',
169171
'timezone' => 'UTC',
172+
'repeat_seconds' => 0,
170173
'without_overlapping' => false,
171174
'on_one_server' => false,
172175
'run_in_background' => false,
@@ -217,6 +220,7 @@ public function test_it_ingests_tasks_run_in_background(): void
217220
'name' => 'php artisan app:fly tokyo',
218221
'cron' => '* * * * *',
219222
'timezone' => 'UTC',
223+
'repeat_seconds' => 0,
220224
'without_overlapping' => false,
221225
'on_one_server' => false,
222226
'run_in_background' => true,
@@ -248,6 +252,7 @@ public function test_it_ingests_subminute_tasks(): void
248252
$line = __LINE__ + 1;
249253
$task = $this->app[Schedule::class]->call(fn () => $this->travelTo(now()->addMicroseconds(30_000_000)))->everyThirtySeconds();
250254
$name = "Closure at: tests/Feature/Sensors/ScheduledTaskSensorTest.php:{$line}";
255+
$repeatSeconds = Compatibility::$subMinuteScheduledTasksSupported ? 30 : 0;
251256

252257
Artisan::call('schedule:run');
253258

@@ -260,11 +265,12 @@ public function test_it_ingests_subminute_tasks(): void
260265
'timestamp' => 946688523.456789,
261266
'deploy' => 'v1.2.3',
262267
'server' => 'scheduler-01',
263-
'_group' => hash('xxh128', "{$name},{$task->expression},{$task->timezone}"),
268+
'_group' => hash('xxh128', "{$name},{$task->expression},{$task->timezone},{$repeatSeconds}"),
264269
'trace_id' => '00000000-0000-0000-0000-000000000000',
265270
'name' => $name,
266271
'cron' => '* * * * *',
267272
'timezone' => 'UTC',
273+
'repeat_seconds' => Compatibility::$subMinuteScheduledTasksSupported ? 30 : 0,
268274
'without_overlapping' => false,
269275
'on_one_server' => false,
270276
'run_in_background' => false,
@@ -296,11 +302,12 @@ public function test_it_ingests_subminute_tasks(): void
296302
'timestamp' => 946688553.456789,
297303
'deploy' => 'v1.2.3',
298304
'server' => 'scheduler-01',
299-
'_group' => hash('xxh128', "{$name},{$task->expression},{$task->timezone}"),
305+
'_group' => hash('xxh128', "{$name},{$task->expression},{$task->timezone},{$repeatSeconds}"),
300306
'trace_id' => '00000000-0000-0000-0000-000000000000',
301307
'name' => $name,
302308
'cron' => '* * * * *',
303309
'timezone' => 'UTC',
310+
'repeat_seconds' => $repeatSeconds,
304311
'without_overlapping' => false,
305312
'on_one_server' => false,
306313
'run_in_background' => false,

0 commit comments

Comments
 (0)