Skip to content

Commit e2dd35a

Browse files
Improve sub-minute task capture (#265)
Co-authored-by: Tim MacDonald <[email protected]>
1 parent cd82dc5 commit e2dd35a

22 files changed

+204
-123
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"psr/log": "^1.0|^2.0|^3.0",
3131
"ramsey/uuid": "^4.0",
3232
"symfony/console": "^6.0|^7.0",
33-
"symfony/http-foundation": "^6.0|^7.0"
33+
"symfony/http-foundation": "^6.0|^7.0",
34+
"symfony/polyfill-php84": "^1.29"
3435
},
3536
"require-dev": {
3637
"ext-pcntl": "*",

src/Concerns/CapturesState.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ trait CapturesState
5555

5656
private bool $paused = false;
5757

58-
private bool $waitingForJob = false;
59-
6058
/**
6159
* @var WeakMap<Route, bool>
6260
*/
@@ -75,7 +73,7 @@ public function sample(float $rate = 1.0): void
7573

7674
$this->sampling = $sample;
7775

78-
$this->ingest->shouldDigest($sample);
76+
$this->ingest->shouldDigestWhenBufferIsFull($sample);
7977

8078
Compatibility::addSamplingToContext($sample);
8179
}
@@ -173,7 +171,9 @@ public function report(Throwable $e, ?bool $handled = null): void
173171

174172
try {
175173
if ($e instanceof FatalError) {
176-
$this->ingest->writeNow($this->sensor->fatalError($e));
174+
if ($this->sampling) {
175+
$this->ingest->writeNow($this->sensor->fatalError($e));
176+
}
177177
} else {
178178
$this->ingest->write($this->sensor->exception($e, $handled));
179179
}
@@ -458,9 +458,9 @@ public function attachMiddlewareToRoute(Route $route): void
458458
/**
459459
* @internal
460460
*/
461-
public function waitForJob(): void
461+
public function waitForExecution(): void
462462
{
463-
$this->waitingForJob = true;
463+
$this->dontSample();
464464
}
465465

466466
/**
@@ -469,7 +469,7 @@ public function waitForJob(): void
469469
public function configureForJobs(): void
470470
{
471471
$this->executionState->source = 'job';
472-
$this->waitingForJob = true;
472+
$this->waitForExecution();
473473
}
474474

475475
/**
@@ -496,7 +496,6 @@ public function prepareForJob(Job $job): void
496496
Compatibility::getSamplingFromContext(default: true) ? 1.0 : 0.0
497497
);
498498

499-
$this->waitingForJob = false;
500499
$this->executionState->timestamp = $this->clock->microtime();
501500
$this->executionState->setId($this->uuid->make());
502501
$this->executionState->executionPreview = Str::tinyText($job->resolveName());
@@ -558,6 +557,7 @@ public function command(InputInterface $input, int $status): void
558557
public function configureForScheduledTasks(): void
559558
{
560559
$this->executionState->source = 'schedule';
560+
$this->waitForExecution();
561561
}
562562

563563
/**
@@ -579,6 +579,7 @@ public function prepareForNextScheduledTask(): void
579579
$this->executionState->trace = $trace;
580580
$this->executionState->setId($trace);
581581
$this->executionState->timestamp = $this->clock->microtime();
582+
$this->sample();
582583
}
583584

584585
/**

src/Contracts/Ingest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Laravel\Nightwatch\Contracts;
44

5+
use Deprecated;
6+
57
/**
68
* @internal
79
*/
@@ -19,7 +21,10 @@ public function writeNow(array $record): void;
1921

2022
public function ping(): void;
2123

22-
public function shouldDigest(bool $bool): void;
24+
#[Deprecated('Use shouldDigestWhenBufferIsFull instead')]
25+
public function shouldDigest(bool $bool = true): void;
26+
27+
public function shouldDigestWhenBufferIsFull(bool $bool = true): void;
2328

2429
public function digest(): void;
2530

src/Core.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ public function guzzleMiddleware(): callable
7878
*
7979
* @return $this
8080
*/
81-
public function digest(): self
81+
public function finishExecution(): self
8282
{
83-
if ($this->waitingForJob) {
84-
return $this;
85-
}
86-
8783
try {
88-
$this->ingest->digest();
84+
if ($this->sampling) {
85+
$this->ingest->digest();
86+
} else {
87+
$this->ingest->flush();
88+
}
8989
} catch (Throwable $e) {
9090
Nightwatch::unrecoverableExceptionOccurred($e);
9191
}

src/Hooks/CommandLifecycleIsLongerThanHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ public function __invoke(Carbon $startedAt, InputInterface $input, int $status):
3737
$this->nightwatch->report($e, handled: true);
3838
}
3939

40-
$this->nightwatch->digest();
40+
$this->nightwatch->finishExecution();
4141
}
4242
}

src/Hooks/CommandStartingListener.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private function registerJobHooks(CommandStarting $event): void
6565
$this->nightwatch->configureForJobs();
6666

6767
/**
68-
* @see \Laravel\Nightwatch\Core::digest()
68+
* @see \Laravel\Nightwatch\Core::finishExecution()
6969
* @see \Laravel\Nightwatch\State\CommandState::flush()
7070
* @see \Laravel\Nightwatch\State\CommandState::$timestamp
7171
* @see \Laravel\Nightwatch\State\CommandState::$id
@@ -79,7 +79,7 @@ private function registerJobHooks(CommandStarting $event): void
7979

8080
/**
8181
* @see \Laravel\Nightwatch\Records\JobAttempt
82-
* @see \Laravel\Nightwatch\Core::digest()
82+
* @see \Laravel\Nightwatch\Core::finishExecution()
8383
*/
8484
$this->events->listen([
8585
JobProcessed::class,
@@ -99,7 +99,7 @@ private function registerScheduledTaskHooks(): void
9999
$this->events->listen(ScheduledTaskStarting::class, (new ScheduledTaskStartingListener($this->nightwatch))(...));
100100

101101
/**
102-
* @see \Laravel\Nightwatch\Core::digest()
102+
* @see \Laravel\Nightwatch\Core::finishExecution()
103103
*/
104104
$this->events->listen([
105105
ScheduledTaskFinished::class,
@@ -126,7 +126,7 @@ private function registerCommandHooks(CommandStarting $event): void
126126
/**
127127
* @see \Laravel\Nightwatch\ExecutionStage::End
128128
* @see \Laravel\Nightwatch\Records\Command
129-
* @see \Laravel\Nightwatch\Core::digest()
129+
* @see \Laravel\Nightwatch\Core::finishExecution()
130130
*/
131131
$this->kernel->whenCommandLifecycleIsLongerThan(-1, new CommandLifecycleIsLongerThanHandler($this->nightwatch));
132132
}

src/Hooks/HttpKernelResolvedHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __invoke(KernelContract $kernel, Application $app): void
3636
/**
3737
* @see \Laravel\Nightwatch\ExecutionStage::End
3838
* @see \Laravel\Nightwatch\Records\Request
39-
* @see \Laravel\Nightwatch\Core::digest()
39+
* @see \Laravel\Nightwatch\Core::finishExecution()
4040
*/
4141
$kernel->whenRequestLifecycleIsLongerThan(-1, new RequestLifecycleIsLongerThanHandler($this->nightwatch));
4242
} catch (Throwable $e) {

src/Hooks/RequestLifecycleIsLongerThanHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ public function __invoke(Carbon $startedAt, Request $request, Response $response
4444
$this->nightwatch->report($e, handled: true);
4545
}
4646

47-
$this->nightwatch->digest();
47+
$this->nightwatch->finishExecution();
4848
}
4949
}

src/Hooks/ScheduledTaskListener.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,17 @@ public function __invoke(ScheduledTaskFinished|ScheduledTaskSkipped|ScheduledTas
3636
return;
3737
}
3838

39+
if ($event instanceof ScheduledTaskSkipped) {
40+
$this->nightwatch->prepareForNextScheduledTask();
41+
}
42+
3943
try {
4044
$this->nightwatch->scheduledTask($event);
4145
} catch (Throwable $e) {
4246
$this->nightwatch->report($e, handled: true);
4347
}
4448

45-
$this->nightwatch->digest();
49+
$this->nightwatch->finishExecution()->waitForExecution();
4650
}
4751

4852
private function isFinishedEventForFailedTask(ScheduledTaskFinished|ScheduledTaskSkipped|ScheduledTaskFailed $event): bool

src/Hooks/VaporWorkCommandFinishedListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ public function __construct(
2222

2323
public function __invoke(CommandFinished $event): void
2424
{
25-
$this->nightwatch->digest();
25+
$this->nightwatch->finishExecution();
2626
}
2727
}

0 commit comments

Comments
 (0)