Skip to content

Commit

Permalink
Merge pull request #47 from alessandrodorazio/custom-connection
Browse files Browse the repository at this point in the history
Custom job dispatch connection
  • Loading branch information
freekmurze authored May 16, 2024
2 parents d85895d + 93cd5e9 commit f80591f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ return [
* job to set timeouts, retries, etc...
*/
'job' => Spatie\DiscordAlerts\Jobs\SendToDiscordChannelJob::class,

/*
* It is possible to specify the queue connection that should be used to send the alert.
*/
'queue_connection' => 'redis',
];

```
Expand Down
11 changes: 11 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,15 @@ public static function getWebhookUrl(string $name): string

return $url;
}

public static function getConnection(): string
{
$connection = config("discord-alerts.queue_connection");

if(is_null($connection)) {
$connection = config("queue.default");
}

return $connection;
}
}
2 changes: 1 addition & 1 deletion src/DiscordAlert.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function message(string $text, array $embeds = []): void

$job = Config::getJob($jobArguments);

dispatch($job);
dispatch($job)->onConnection(Config::getConnection());
}

private function parseNewline(string $text): string
Expand Down
11 changes: 11 additions & 0 deletions tests/DiscordAlertsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
Bus::assertDispatched(SendToDiscordChannelJob::class);
});

it('can dispatch a job to send a message to discord using the default webhook url and a custom queue', function () {
config()->set('discord-alerts.webhook_urls.default', 'https://test-domain.com');
config()->set('discord-alerts.queue_connection', 'custom-queue-connection');

DiscordAlert::message('test-data');

Bus::assertDispatched(SendToDiscordChannelJob::class, function ($job) {
return $job->connection === 'custom-queue-connection';
});
});

it('can dispatch a job to send a message to discord using an alternative webhook url', function () {
config()->set('discord-alerts.webhook_urls.marketing', 'https://test-domain.com');

Expand Down

0 comments on commit f80591f

Please sign in to comment.