Skip to content

Commit 1ec96f6

Browse files
committed
Move add task to monitor to service provider
1 parent fada692 commit 1ec96f6

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/CloudTasksQueue.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,9 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
120120
$task->setScheduleTime(new Timestamp(['seconds' => $availableAt]));
121121
}
122122

123-
if (CloudTasks::monitorEnabled()) {
124-
MonitoringService::make()->addToMonitor($queue, $task);
125-
}
126-
127123
$createdTask = CloudTasksApi::createTask($queueName, $task);
128124

129-
event(new TaskCreated($createdTask));
125+
event((new TaskCreated)->queue($queue)->task($createdTask));
130126
}
131127

132128
private function withUuid(string $payload): string

src/CloudTasksServiceProvider.php

+8
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ private function registerRoutes(Router $router): void
151151

152152
private function registerMonitoring(): void
153153
{
154+
app('events')->listen(TaskCreated::class, function (TaskCreated $event) {
155+
if (CloudTasks::monitorDisabled()) {
156+
return;
157+
}
158+
159+
MonitoringService::make()->addToMonitor($event->queue, $event->task);
160+
});
161+
154162
app('events')->listen(JobFailed::class, function (JobFailed $event) {
155163
if (!$event->job instanceof CloudTasksJob) {
156164
return;

src/TaskCreated.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@
88

99
class TaskCreated
1010
{
11+
public string $queue;
1112
public Task $task;
1213

13-
public function __construct(Task $task)
14+
public function task(Task $task): self
1415
{
1516
$this->task = $task;
17+
18+
return $this;
19+
}
20+
21+
public function queue(string $queue): self
22+
{
23+
$this->queue = $queue;
24+
25+
return $this;
1626
}
1727
}

0 commit comments

Comments
 (0)