Skip to content

Commit 4c6765e

Browse files
Expose queue flow facts in visibility snapshots
Expose queue flow facts in visibility snapshots
1 parent 89a71ed commit 4c6765e

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎src/V2/Support/OperatorQueueVisibility.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ private static function stats(
199199
static fn (array $poller): bool => ($poller['is_stale'] ?? false) !== true,
200200
));
201201
$stalePollers = count($pollers) - $activePollers;
202+
$recentTaskFlow = self::recentTaskFlow($namespace, $taskQueue, $now);
202203

203204
return [
204205
'approximate_backlog_count' => $readyCounts[TaskType::Workflow->value] + $readyCounts[TaskType::Activity->value],
@@ -229,6 +230,28 @@ private static function stats(
229230
'stale_count' => $stalePollers,
230231
'stale_after_seconds' => $staleAfterSeconds,
231232
],
233+
'tasks_added_last_minute' => $recentTaskFlow['tasks_added_last_minute'],
234+
'tasks_dispatched_last_minute' => $recentTaskFlow['tasks_dispatched_last_minute'],
235+
];
236+
}
237+
238+
/**
239+
* @return array{tasks_added_last_minute: int, tasks_dispatched_last_minute: int}
240+
*/
241+
private static function recentTaskFlow(string $namespace, string $taskQueue, CarbonInterface $now): array
242+
{
243+
$taskTable = self::taskTable();
244+
$windowStart = self::databaseTimestamp($now->copy()->subMinute());
245+
$query = self::baseTaskQuery($namespace, $taskQueue);
246+
247+
return [
248+
'tasks_added_last_minute' => (clone $query)
249+
->where($taskTable . '.created_at', '>=', $windowStart)
250+
->count(),
251+
'tasks_dispatched_last_minute' => (clone $query)
252+
->whereNotNull($taskTable . '.last_dispatched_at')
253+
->where($taskTable . '.last_dispatched_at', '>=', $windowStart)
254+
->count(),
232255
];
233256
}
234257

‎tests/Feature/V2/V2OperatorQueueVisibilityTest.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,18 @@ public function testForQueueSummarizesBacklogPollersLeasesAndRepairCandidates():
5959
'lease_expires_at' => now()
6060
->subMinute(),
6161
'attempt_count' => 2,
62+
'created_at' => now()
63+
->subMinutes(2),
6264
]
6365
);
6466
$activityTask = $this->createTask($run, '01JQUEUEVISIBLETASK000004', TaskType::Activity, TaskStatus::Leased, [
6567
'lease_owner' => 'worker-active',
6668
'lease_expires_at' => now()
6769
->addMinute(),
70+
'created_at' => now()
71+
->subMinutes(2),
72+
'last_dispatched_at' => now()
73+
->subSeconds(10),
6874
]);
6975

7076
ActivityAttempt::query()->create([
@@ -119,6 +125,8 @@ public function testForQueueSummarizesBacklogPollersLeasesAndRepairCandidates():
119125
$this->assertSame(1, $payload['stats']['pollers']['active_count']);
120126
$this->assertSame(1, $payload['stats']['pollers']['stale_count']);
121127
$this->assertSame(60, $payload['stats']['pollers']['stale_after_seconds']);
128+
$this->assertSame(2, $payload['stats']['tasks_added_last_minute']);
129+
$this->assertSame(1, $payload['stats']['tasks_dispatched_last_minute']);
122130
$this->assertSame('worker-active', $payload['pollers'][0]['worker_id']);
123131
$this->assertFalse($payload['pollers'][0]['is_stale']);
124132
$this->assertSame('worker-stale', $payload['pollers'][1]['worker_id']);
@@ -151,6 +159,10 @@ public function testForNamespaceListsTaskQueuesFromTasksAndInjectedPollerOnlyQue
151159

152160
$this->createTask($defaultRun, '01JQUEUENSDEFAULTTASK001', TaskType::Workflow, TaskStatus::Ready, [
153161
'queue' => 'alpha',
162+
'created_at' => now()
163+
->subSeconds(30),
164+
'last_dispatched_at' => now()
165+
->subSeconds(15),
154166
]);
155167
$this->createTask($otherRun, '01JQUEUENSOTHERTASK0001', TaskType::Workflow, TaskStatus::Ready, [
156168
'queue' => 'beta',
@@ -170,8 +182,12 @@ public function testForNamespaceListsTaskQueuesFromTasksAndInjectedPollerOnlyQue
170182
$this->assertSame(['alpha', 'poller-only'], array_column($snapshot['task_queues'], 'name'));
171183
$queues = collect($snapshot['task_queues'])->keyBy('name');
172184
$this->assertSame(1, $queues->get('alpha')['stats']['approximate_backlog_count']);
185+
$this->assertSame(1, $queues->get('alpha')['stats']['tasks_added_last_minute']);
186+
$this->assertSame(1, $queues->get('alpha')['stats']['tasks_dispatched_last_minute']);
173187
$this->assertSame(1, $queues->get('poller-only')['stats']['pollers']['active_count']);
174188
$this->assertSame(0, $queues->get('poller-only')['stats']['approximate_backlog_count']);
189+
$this->assertSame(0, $queues->get('poller-only')['stats']['tasks_added_last_minute']);
190+
$this->assertSame(0, $queues->get('poller-only')['stats']['tasks_dispatched_last_minute']);
175191
$this->assertFalse($queues->has('beta'));
176192
}
177193

0 commit comments

Comments
 (0)