Skip to content

Commit a6189b4

Browse files
committed
Refactor and cleanups
1 parent 52ae5ea commit a6189b4

15 files changed

+279
-306
lines changed

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ Add a new queue connection to `config/queue.php`
5959
'handler' => env('STACKKIT_CLOUD_TASKS_HANDLER', ''),
6060
'service_account_email' => env('STACKKIT_CLOUD_TASKS_SERVICE_EMAIL', ''),
6161

62-
// Optional: The deadline in seconds for requests sent to the worker. If the worker
63-
// does not respond by this deadline then the request is cancelled and the attempt
64-
// is marked as a DEADLINE_EXCEEDED failure.
65-
'dispatch_deadline' => null,
6662
'backoff' => 0,
6763
],
6864
```

assets/dashboard.png

-875 KB
Binary file not shown.

config/cloud-tasks.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,4 @@
88

99
// If the application only dispatches jobs
1010
'disable_task_handler' => env('STACKKIT_CLOUD_TASKS_DISABLE_TASK_HANDLER', false),
11-
12-
'dashboard' => [
13-
'enabled' => env('STACKKIT_CLOUD_TASKS_DASHBOARD_ENABLED', false),
14-
'password' => env('STACKKIT_CLOUD_TASKS_DASHBOARD_PASSWORD', 'MyPassword1!'),
15-
],
1611
];

migrations/2024_02_14_211616_create_cloud_tasks_table.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/CloudTasksJob.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,31 +62,21 @@ public function setAttempts(int $attempts): void
6262
$this->job['internal']['attempts'] = $attempts;
6363
}
6464

65-
public function getTaskName(): string
66-
{
67-
return $this->job['internal']['taskName'];
68-
}
69-
7065
public function delete(): void
7166
{
7267
// Laravel automatically calls delete() after a job is processed successfully.
7368
// However, this is not what we want to happen in Cloud Tasks because Cloud Tasks
7469
// will also delete the task upon a 200 OK status, which means a task is deleted twice.
7570
}
7671

77-
public function hasError(): bool
78-
{
79-
return data_get($this->job, 'internal.errored') === true;
80-
}
81-
8272
public function release($delay = 0): void
8373
{
8474
parent::release($delay);
8575

8676
$this->driver->release($this, $delay);
8777

8878
if (! data_get($this->job, 'internal.errored')) {
89-
app('events')->dispatch(new JobReleased($this->getConnectionName(), $this, $delay));
79+
event(new JobReleased($this->getConnectionName(), $this, $delay));
9080
}
9181
}
9282
}

src/CloudTasksQueue.php

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Google\Cloud\Tasks\V2\HttpRequest;
1313
use Google\Cloud\Tasks\V2\OidcToken;
1414
use Google\Cloud\Tasks\V2\Task;
15-
use Google\Protobuf\Duration;
1615
use Google\Protobuf\Timestamp;
1716
use Illuminate\Contracts\Queue\Queue as QueueContract;
1817
use Illuminate\Queue\Queue as LaravelQueue;
@@ -138,22 +137,10 @@ protected function pushToCloudTasks($queue, $payload, $delay, mixed $job)
138137

139138
$task = tap(new Task())->setName($this->taskName($queue, $payload['displayName']));
140139

141-
$payload = $this->enrichPayloadWithInternalData(
142-
payload: $payload,
143-
queueName: $queue,
144-
taskName: $task->getName(),
145-
connectionName: $this->getConnectionName(),
146-
);
140+
$payload = $this->enrichPayloadWithAttempts($payload);
147141

148142
$this->addPayloadToTask($payload, $task, $job);
149143

150-
// The deadline for requests sent to the app. If the app does not respond by
151-
// this deadline then the request is cancelled and the attempt is marked as
152-
// a failure. Cloud Tasks will retry the task according to the RetryConfig.
153-
if (! empty($this->config['dispatch_deadline'])) {
154-
$task->setDispatchDeadline(new Duration(['seconds' => $this->config['dispatch_deadline']]));
155-
}
156-
157144
$availableAt = $this->availableAt($delay);
158145
if ($availableAt > time()) {
159146
$task->setScheduleTime(new Timestamp(['seconds' => $availableAt]));
@@ -175,22 +162,18 @@ private function taskName(string $queueName, string $displayName): string
175162
$queueName,
176163
str($displayName)
177164
->afterLast('\\')
165+
->replaceMatches('![^-\pL\pN\s]+!u', '-')
166+
->replaceMatches('![-\s]+!u', '-')
178167
->prepend((string) Str::ulid(), '-')
179168
->toString(),
180169
);
181170
}
182171

183-
private function enrichPayloadWithInternalData(
172+
private function enrichPayloadWithAttempts(
184173
array $payload,
185-
string $queueName,
186-
string $taskName,
187-
string $connectionName,
188174
): array {
189175
$payload['internal'] = [
190176
'attempts' => $payload['internal']['attempts'] ?? 0,
191-
'queue' => $queueName,
192-
'taskName' => $taskName,
193-
'connection' => $connectionName,
194177
];
195178

196179
return $payload;
@@ -241,7 +224,7 @@ public function pop($queue = null)
241224

242225
public function delete(CloudTasksJob $job): void
243226
{
244-
CloudTasksApi::deleteTask($job->getTaskName());
227+
// Job deletion will be handled by Cloud Tasks.
245228
}
246229

247230
public function release(CloudTasksJob $job, int $delay = 0): void

src/CloudTasksServiceProvider.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,6 @@ private function registerEvents(): void
9595
);
9696
});
9797

98-
$events->listen(JobProcessing::class, function (JobProcessing $event) {
99-
if (! $event->job instanceof CloudTasksJob) {
100-
return;
101-
}
102-
});
103-
104-
$events->listen(JobProcessed::class, function (JobProcessed $event) {
105-
if (! $event->job instanceof CloudTasksJob) {
106-
return;
107-
}
108-
109-
data_set($event->job->job, 'internal.processed', true);
110-
});
111-
11298
$events->listen(JobExceptionOccurred::class, function (JobExceptionOccurred $event) {
11399
if (! $event->job instanceof CloudTasksJob) {
114100
return;

src/Events/TaskIncoming.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Stackkit\LaravelGoogleCloudTasksQueue\Events;
6+
7+
use Stackkit\LaravelGoogleCloudTasksQueue\IncomingTask;
8+
9+
class TaskIncoming
10+
{
11+
public function __construct(public IncomingTask $task)
12+
{
13+
//
14+
}
15+
}

src/IncomingTask.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Stackkit\LaravelGoogleCloudTasksQueue;
66

7+
use Error;
8+
use Illuminate\Contracts\Encryption\Encrypter;
79
use Safe\Exceptions\JsonException;
810

911
use function Safe\json_decode;
@@ -33,17 +35,42 @@ public function isInvalid(): bool
3335

3436
public function connection(): string
3537
{
36-
return $this->task['internal']['connection'];
38+
if ($connection = data_get($this->command(), 'connection')) {
39+
return $connection;
40+
}
41+
42+
return config('queue.default');
3743
}
3844

3945
public function queue(): string
4046
{
41-
return $this->task['internal']['queue'];
47+
if ($queue = data_get($this->command(), 'queue')) {
48+
return $queue;
49+
}
50+
51+
return config('queue.connections.'.$this->connection().'.queue');
4252
}
4353

4454
public function taskName(): string
4555
{
46-
return $this->task['internal']['taskName'];
56+
return request()->header('X-CloudTasks-TaskName')
57+
?? request()->header('X-AppEngine-TaskName')
58+
?? throw new Error('Unable to extract taskname from header');
59+
}
60+
61+
public function command(): array
62+
{
63+
$command = $this->task['data']['command'];
64+
65+
if (str_starts_with($command, 'O:')) {
66+
return (array) unserialize($command, ['allowed_classes' => false]);
67+
}
68+
69+
if (app()->bound(Encrypter::class)) {
70+
return (array) unserialize(app(Encrypter::class)->decrypt($command));
71+
}
72+
73+
return [];
4774
}
4875

4976
public function toArray(): array

src/TaskHandler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Google\Cloud\Tasks\V2\Client\CloudTasksClient;
88
use Illuminate\Container\Container;
99
use Illuminate\Queue\WorkerOptions;
10+
use Stackkit\LaravelGoogleCloudTasksQueue\Events\TaskIncoming;
1011

1112
class TaskHandler
1213
{
@@ -21,6 +22,8 @@ public function handle(?string $task = null): void
2122
{
2223
$task = IncomingTask::fromJson($task ?: request()->getContent());
2324

25+
event(new TaskIncoming($task));
26+
2427
if ($task->isInvalid()) {
2528
abort(422, 'Invalid task payload');
2629
}

0 commit comments

Comments
 (0)