Skip to content
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

configure job connections #323

Merged
merged 4 commits into from
Sep 24, 2024
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
1 change: 1 addition & 0 deletions src/Actions/AfterAuthorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function __invoke(ShopIdValue $shopId): bool
} else {
// Run later
$job::dispatch($shop)
->onConnection(Util::getShopifyConfig('job_connections')['after_authenticate'])
->onQueue(Util::getShopifyConfig('job_queues')['after_authenticate']);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Actions/DispatchScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public function __invoke(ShopIdValue $shopId, bool $inline = false): bool
($this->jobClass)::dispatch(
$shop->getId(),
$scripttags
)->onQueue(Util::getShopifyConfig('job_queues')['scripttags']);
)->onConnection(Util::getShopifyConfig('job_connections')['scripttags'])
->onQueue(Util::getShopifyConfig('job_queues')['scripttags']);
}

return true;
Expand Down
3 changes: 2 additions & 1 deletion src/Actions/DispatchWebhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public function __invoke(ShopIdValue $shopId, bool $inline = false): bool
($this->jobClass)::dispatch(
$shop->getId(),
$webhooks
)->onQueue(Util::getShopifyConfig('job_queues')['webhooks']);
)->onConnection(Util::getShopifyConfig('job_connections')['webhooks'])
->onQueue(Util::getShopifyConfig('job_queues')['webhooks']);
}

return true;
Expand Down
3 changes: 2 additions & 1 deletion src/Traits/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public function handle(string $type, Request $request): ResponseResponse
$jobClass::dispatch(
$request->header('x-shopify-shop-domain'),
$jobData
)->onQueue(Util::getShopifyConfig('job_queues')['webhooks']);
)->onConnection(Util::getShopifyConfig('job_connections')['webhooks'])
->onQueue(Util::getShopifyConfig('job_queues')['webhooks']);

return Response::make('', ResponseResponse::HTTP_CREATED);
}
Expand Down
14 changes: 14 additions & 0 deletions src/resources/config/shopify-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,21 @@
'scripttags' => env('SCRIPTTAGS_JOB_QUEUE', null),
'after_authenticate' => env('AFTER_AUTHENTICATE_JOB_QUEUE', null),
],
/*
|--------------------------------------------------------------------------
| Job Connections
|--------------------------------------------------------------------------
|
| This option is for setting a specific job connection for webhooks, scripttags
| and after_authenticate_job.
|
*/

'job_connections' => [
'webhooks' => env('WEBHOOKS_JOB_CONNECTION', null),
'scripttags' => env('SCRIPTTAGS_JOB_CONNECTION', null),
'after_authenticate' => env('AFTER_AUTHENTICATE_JOB_CONNECTION', null),
],
/*
|--------------------------------------------------------------------------
| Config API Callback
Expand Down
41 changes: 41 additions & 0 deletions tests/Actions/AfterAuthorizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,47 @@ public function testRunDispatch(): void
Queue::assertPushed($jobClass);
}

public function testRunDispatchCustomConnection(): void
{
// Fake the queue
Queue::fake();

// Create the config
$jobClass = AfterAuthorizeJob::class;
$this->app['config']->set('shopify-app.after_authenticate_job', [
[
'job' => $jobClass,
'inline' => false,
],
[
'job' => $jobClass,
'inline' => false,
],
]);

// Define the custom job connection
$customConnection = 'custom_connection';

// Set up the configuration
$this->app['config']->set('shopify-app.job_connections', [
'after_authenticate' => $customConnection,
]);

// Create the shop
$shop = factory($this->model)->create();

// Run
call_user_func(
$this->action,
$shop->getId()
);

// Assert the job was pushed with the correct connection
Queue::assertPushed($jobClass, function ($job) use ($customConnection) {
return $job->connection === $customConnection;
});
}

public function testRunInline(): void
{
// Create the config
Expand Down
42 changes: 42 additions & 0 deletions tests/Actions/DispatchScriptsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,48 @@ public function testRunDispatch(): void
$this->assertTrue($result);
}

public function testRunDispatchCustomConnection(): void
{
// Fake the queue
Queue::fake();

// Create the config
$this->app['config']->set('shopify-app.scripttags', [
[
'src' => 'https://js-aplenty.com/foo.js',
],
]);

// Define the custom job connection
$customConnection = 'custom_connection';

// Set up the configuration
$this->app['config']->set('shopify-app.job_connections', [
'scripttags' => $customConnection,
]);

// Setup API stub
$this->setApiStub();
ApiStub::stubResponses(['get_script_tags']);

// Create the shop
$shop = factory($this->model)->create();

// Run
$result = call_user_func(
$this->action,
$shop->getId(),
false // async
);

// Assert the job was pushed with the correct connection
Queue::assertPushed(ScripttagInstaller::class, function ($job) use ($customConnection) {
return $job->connection === $customConnection;
});

$this->assertTrue($result);
}

public function testRunDispatchNow(): void
{
// Fake the queue
Expand Down
47 changes: 47 additions & 0 deletions tests/Actions/DispatchWebhooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,53 @@ public function testRunDispatch(): void
$this->assertTrue($result);
}

public function testRunDispatchCustomConnection(): void
{
// Fake the queue
Queue::fake();

// Create the config
$this->app['config']->set('shopify-app.webhooks', [
[
'topic' => 'orders/create',
'address' => 'https://localhost/webhooks/orders-create',
],
[
'topic' => 'app/uninstalled',
'address' => 'http://apple.com/uninstall',
],
]);

// Define the custom job connection
$customConnection = 'custom_connection';

// Set up the configuration
$this->app['config']->set('shopify-app.job_connections', [
'webhooks' => $customConnection,
]);

// Setup API stub
$this->setApiStub();
ApiStub::stubResponses(['get_webhooks']);

// Create the shop
$shop = factory($this->model)->create();

// Run
$result = call_user_func(
$this->action,
$shop->getId(),
false // async
);

// Assert the job was pushed with the correct connection
Queue::assertPushed(WebhookInstaller::class, function ($job) use ($customConnection) {
return $job->connection === $customConnection;
});

$this->assertTrue($result);
}

public function testRunDispatchNow(): void
{
// Fake the queue
Expand Down
45 changes: 45 additions & 0 deletions tests/Traits/WebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,51 @@ public function testHandleWithCustomClassMapping(): void
);
}

public function testHandleDispatchesJobWithCustomConnection(): void
{
// Fake the queue
Queue::fake();

// Extend Job::class into a custom class
$shop = factory($this->model)->create(['name' => 'example.myshopify.com']);

// Define the custom job connection
$customConnection = 'custom_connection';

// Set up the configuration
$this->app['config']->set('shopify-app.job_connections', [
'webhooks' => $customConnection,
]);

// Mock headers that match Shopify
$headers = [
'HTTP_CONTENT_TYPE' => 'application/json',
'HTTP_X_SHOPIFY_SHOP_DOMAIN' => $shop->name,
'HTTP_X_SHOPIFY_HMAC_SHA256' => 'hvTE9wpDzMcDnPEuHWvYZ58ElKn5vHs0LomurfNIuUc=', // Matches fixture data and API secret
];

// Create a webhook call and pass in our own headers and data
$response = $this->call(
'post',
'/webhook/orders-create-example',
[],
[],
[],
$headers,
file_get_contents(__DIR__.'/../fixtures/webhook.json')
);

// Check it was created and job was pushed
$response->assertStatus(Response::HTTP_CREATED);
$response->assertStatus(201);


// Assert the job was pushed with the correct connection
Queue::assertPushed(OrdersCreateJob::class, function ($job) use ($customConnection) {
return $job->connection === $customConnection;
});
}

/**
* Override the default config
* Allow config change to persist when using $this->call()
Expand Down
Loading