Skip to content

Fix - Parameterize config to accept connection names. #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/CloudTasksQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ class CloudTasksQueue extends LaravelQueue implements QueueContract

private $client;
private $default;
private $config;

public function __construct(array $config, CloudTasksClient $client)
{
$this->client = $client;
$this->default = $config['queue'];
$this->config = $config;
}

public function size($queue = null)
Expand Down Expand Up @@ -52,19 +54,19 @@ public function later($delay, $job, $data = '', $queue = null)
protected function pushToCloudTasks($queue, $payload, $delay = 0, $attempts = 0)
{
$queue = $this->getQueue($queue);
$queueName = $this->client->queueName(Config::project(), Config::location(), $queue);
$queueName = $this->client->queueName($this->config['project'], $this->config['location'], $queue);
$availableAt = $this->availableAt($delay);

$httpRequest = $this->createHttpRequest();
$httpRequest->setUrl(Config::handler());
$httpRequest->setUrl($this->config['handler']);
$httpRequest->setHttpMethod(HttpMethod::POST);
$httpRequest->setBody($payload);

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

$token = new OidcToken;
$token->setServiceAccountEmail(Config::serviceAccountEmail());
$token->setServiceAccountEmail($this->config['service_account_email']);
$httpRequest->setOidcToken($token);

if ($availableAt > time()) {
Expand Down
25 changes: 0 additions & 25 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,6 @@

class Config
{
public static function credentials()
{
return config('queue.connections.cloudtasks.credentials');
}

public static function project()
{
return config('queue.connections.cloudtasks.project');
}

public static function location()
{
return config('queue.connections.cloudtasks.location');
}

public static function handler()
{
return config('queue.connections.cloudtasks.handler');
}

public static function serviceAccountEmail()
{
return config('queue.connections.cloudtasks.service_account_email');
}

public static function validate(array $config)
{
if (empty($config['project'])) {
Expand Down
27 changes: 20 additions & 7 deletions src/TaskHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class TaskHandler
{
private $request;
private $publicKey;
private $config;

public function __construct(CloudTasksClient $client, Request $request, OpenIdVerificator $publicKey)
{
Expand All @@ -26,15 +27,27 @@ public function __construct(CloudTasksClient $client, Request $request, OpenIdVe
*/
public function handle($task = null)
{
$this->authorizeRequest();

$task = $task ?: $this->captureTask();

$this->loadQueueConnectionConfiguration($task);

$this->authorizeRequest();

$this->listenForEvents();

$this->handleTask($task);
}

private function loadQueueConnectionConfiguration($task)
{
$command = unserialize($task['data']['command']);
$connection = $command->connection ?? config('queue.default');
$this->config = array_merge(
config("queue.connections.{$connection}"),
['connection' => $connection]
);
}

/**
* @throws CloudTasksException
*/
Expand Down Expand Up @@ -64,7 +77,7 @@ protected function validateToken($openIdToken)
throw new CloudTasksException('The given OpenID token is not valid');
}

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

Expand Down Expand Up @@ -97,7 +110,7 @@ private function listenForEvents()
{
app('events')->listen(JobFailed::class, function ($event) {
app('queue.failer')->log(
'cloudtasks', $event->job->getQueue(),
$this->config['connection'], $event->job->getQueue(),
$event->job->getRawBody(), $event->exception
);
});
Expand All @@ -117,14 +130,14 @@ private function handleTask($task)

$worker = $this->getQueueWorker();

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

private function getQueueMaxTries(CloudTasksJob $job)
{
$queueName = $this->client->queueName(
Config::project(),
Config::location(),
$this->config['project'],
$this->config['location'],
$job->getQueue()
);

Expand Down
4 changes: 2 additions & 2 deletions tests/TaskHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function it_needs_an_authorization_header()
$this->expectException(CloudTasksException::class);
$this->expectExceptionMessage('Missing [Authorization] header');

$this->handler->handle();
$this->handler->handle($this->simpleJob());
}

/** @test */
Expand Down Expand Up @@ -165,7 +165,7 @@ public function after_max_attempts_it_will_log_to_failed_table()
}

$this->assertDatabaseHas('failed_jobs', [
'connection' => 'cloudtasks',
'connection' => 'my-cloudtasks-connection',
'queue' => 'my-queue',
'payload' => rtrim($this->failingJobPayload()),
]);
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ protected function getEnvironmentSetUp($app)
}

$app['config']->set('cache.default', 'file');
$app['config']->set('queue.default', 'cloudtasks');
$app['config']->set('queue.connections.cloudtasks', [
$app['config']->set('queue.default', 'my-cloudtasks-connection');
$app['config']->set('queue.connections.my-cloudtasks-connection', [
'driver' => 'cloudtasks',
'queue' => 'test-queue',
'project' => 'test-project',
Expand All @@ -72,6 +72,6 @@ protected function getEnvironmentSetUp($app)

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