Skip to content

Commit aa0f805

Browse files
Merge pull request #23 from atrauzzi/fix/connection-names
Fix - Parameterize config to accept connection names.
2 parents 578ee7d + c17011e commit aa0f805

File tree

5 files changed

+30
-40
lines changed

5 files changed

+30
-40
lines changed

src/CloudTasksQueue.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ class CloudTasksQueue extends LaravelQueue implements QueueContract
1818

1919
private $client;
2020
private $default;
21+
private $config;
2122

2223
public function __construct(array $config, CloudTasksClient $client)
2324
{
2425
$this->client = $client;
2526
$this->default = $config['queue'];
27+
$this->config = $config;
2628
}
2729

2830
public function size($queue = null)
@@ -52,19 +54,19 @@ public function later($delay, $job, $data = '', $queue = null)
5254
protected function pushToCloudTasks($queue, $payload, $delay = 0, $attempts = 0)
5355
{
5456
$queue = $this->getQueue($queue);
55-
$queueName = $this->client->queueName(Config::project(), Config::location(), $queue);
57+
$queueName = $this->client->queueName($this->config['project'], $this->config['location'], $queue);
5658
$availableAt = $this->availableAt($delay);
5759

5860
$httpRequest = $this->createHttpRequest();
59-
$httpRequest->setUrl(Config::handler());
61+
$httpRequest->setUrl($this->config['handler']);
6062
$httpRequest->setHttpMethod(HttpMethod::POST);
6163
$httpRequest->setBody($payload);
6264

6365
$task = $this->createTask();
6466
$task->setHttpRequest($httpRequest);
6567

6668
$token = new OidcToken;
67-
$token->setServiceAccountEmail(Config::serviceAccountEmail());
69+
$token->setServiceAccountEmail($this->config['service_account_email']);
6870
$httpRequest->setOidcToken($token);
6971

7072
if ($availableAt > time()) {

src/Config.php

-25
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,6 @@
66

77
class Config
88
{
9-
public static function credentials()
10-
{
11-
return config('queue.connections.cloudtasks.credentials');
12-
}
13-
14-
public static function project()
15-
{
16-
return config('queue.connections.cloudtasks.project');
17-
}
18-
19-
public static function location()
20-
{
21-
return config('queue.connections.cloudtasks.location');
22-
}
23-
24-
public static function handler()
25-
{
26-
return config('queue.connections.cloudtasks.handler');
27-
}
28-
29-
public static function serviceAccountEmail()
30-
{
31-
return config('queue.connections.cloudtasks.service_account_email');
32-
}
33-
349
public static function validate(array $config)
3510
{
3611
if (empty($config['project'])) {

src/TaskHandler.php

+20-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class TaskHandler
1212
{
1313
private $request;
1414
private $publicKey;
15+
private $config;
1516

1617
public function __construct(CloudTasksClient $client, Request $request, OpenIdVerificator $publicKey)
1718
{
@@ -26,15 +27,27 @@ public function __construct(CloudTasksClient $client, Request $request, OpenIdVe
2627
*/
2728
public function handle($task = null)
2829
{
29-
$this->authorizeRequest();
30-
3130
$task = $task ?: $this->captureTask();
3231

32+
$this->loadQueueConnectionConfiguration($task);
33+
34+
$this->authorizeRequest();
35+
3336
$this->listenForEvents();
3437

3538
$this->handleTask($task);
3639
}
3740

41+
private function loadQueueConnectionConfiguration($task)
42+
{
43+
$command = unserialize($task['data']['command']);
44+
$connection = $command->connection ?? config('queue.default');
45+
$this->config = array_merge(
46+
config("queue.connections.{$connection}"),
47+
['connection' => $connection]
48+
);
49+
}
50+
3851
/**
3952
* @throws CloudTasksException
4053
*/
@@ -64,7 +77,7 @@ protected function validateToken($openIdToken)
6477
throw new CloudTasksException('The given OpenID token is not valid');
6578
}
6679

67-
if ($openIdToken->aud != Config::handler()) {
80+
if ($openIdToken->aud != $this->config['handler']) {
6881
throw new CloudTasksException('The given OpenID token is not valid');
6982
}
7083

@@ -97,7 +110,7 @@ private function listenForEvents()
97110
{
98111
app('events')->listen(JobFailed::class, function ($event) {
99112
app('queue.failer')->log(
100-
'cloudtasks', $event->job->getQueue(),
113+
$this->config['connection'], $event->job->getQueue(),
101114
$event->job->getRawBody(), $event->exception
102115
);
103116
});
@@ -117,14 +130,14 @@ private function handleTask($task)
117130

118131
$worker = $this->getQueueWorker();
119132

120-
$worker->process('cloudtasks', $job, new WorkerOptions());
133+
$worker->process($this->config['connection'], $job, new WorkerOptions());
121134
}
122135

123136
private function getQueueMaxTries(CloudTasksJob $job)
124137
{
125138
$queueName = $this->client->queueName(
126-
Config::project(),
127-
Config::location(),
139+
$this->config['project'],
140+
$this->config['location'],
128141
$job->getQueue()
129142
);
130143

tests/TaskHandlerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function it_needs_an_authorization_header()
7777
$this->expectException(CloudTasksException::class);
7878
$this->expectExceptionMessage('Missing [Authorization] header');
7979

80-
$this->handler->handle();
80+
$this->handler->handle($this->simpleJob());
8181
}
8282

8383
/** @test */
@@ -165,7 +165,7 @@ public function after_max_attempts_it_will_log_to_failed_table()
165165
}
166166

167167
$this->assertDatabaseHas('failed_jobs', [
168-
'connection' => 'cloudtasks',
168+
'connection' => 'my-cloudtasks-connection',
169169
'queue' => 'my-queue',
170170
'payload' => rtrim($this->failingJobPayload()),
171171
]);

tests/TestCase.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ protected function getEnvironmentSetUp($app)
5959
}
6060

6161
$app['config']->set('cache.default', 'file');
62-
$app['config']->set('queue.default', 'cloudtasks');
63-
$app['config']->set('queue.connections.cloudtasks', [
62+
$app['config']->set('queue.default', 'my-cloudtasks-connection');
63+
$app['config']->set('queue.connections.my-cloudtasks-connection', [
6464
'driver' => 'cloudtasks',
6565
'queue' => 'test-queue',
6666
'project' => 'test-project',
@@ -72,6 +72,6 @@ protected function getEnvironmentSetUp($app)
7272

7373
protected function setConfigValue($key, $value)
7474
{
75-
$this->app['config']->set('queue.connections.cloudtasks.' . $key, $value);
75+
$this->app['config']->set('queue.connections.my-cloudtasks-connection.' . $key, $value);
7676
}
7777
}

0 commit comments

Comments
 (0)