Skip to content

Commit

Permalink
Merge pull request #47 from mcpuishor/main
Browse files Browse the repository at this point in the history
Add queue support for Slack alerts
  • Loading branch information
freekmurze committed May 6, 2024
2 parents e81bbc7 + ea26333 commit 7d89a8c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,25 @@ use Spatie\SlackAlerts\Facades\SlackAlert;
SlackAlert::toChannel('subscription_alerts')->message("You have a new subscriber to the {$newsletter->name} newsletter!");
```

## Queuing
By default, messages are sent by dispatching the job to the `default` queue.

### Configuring the queue
In `.env` file, add

```dotenv
SLACK_ALERT_QUEUE=queue_name
```

### Changing the queue at runtime
You can queue the job to a different queue than the one defined in config by passing it to the `onQueue` function.

```php
use Spatie\SlackAlerts\Facades\SlackAlert;

SlackAlert::onQueue('some-queue')->message("Some message.");
```

## Formatting

### Markdown
Expand Down
1 change: 1 addition & 0 deletions config/slack-alerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
* job to set timeouts, retries, etc...
*/
'job' => Spatie\SlackAlerts\Jobs\SendToSlackChannelJob::class,
'queue' => env('SLACK_ALERT_QUEUE', 'default'),
];
11 changes: 11 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,15 @@ public static function getWebhookUrl(string $name): string|null

return $url;
}

public static function getQueue(): string|null
{
$queue = config('slack-alerts.queue');

if (! $queue) {
return null;
}

return $queue;
}
}
17 changes: 15 additions & 2 deletions src/SlackAlert.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class SlackAlert
protected string $webhookUrlName = 'default';
protected ?string $channel = null;

protected ?string $queue = null;

public function to(string $webhookUrlName): self
{
$this->webhookUrlName = $webhookUrlName;
Expand All @@ -21,6 +23,13 @@ public function toChannel(string $channel): self
return $this;
}

public function onQueue(string $queue): self
{
$this->queue = $queue;

return $this;
}

public function message(string $text): void
{
$webhookUrl = Config::getWebhookUrl($this->webhookUrlName);
Expand All @@ -35,7 +44,9 @@ public function message(string $text): void
'channel' => $this->channel,
]);

dispatch($job);
dispatch(
$job->onQueue( $this->queue ?? Config::getQueue() )
);
}

public function blocks(array $blocks): void
Expand All @@ -52,6 +63,8 @@ public function blocks(array $blocks): void
'channel' => $this->channel,
]);

dispatch($job);
dispatch(
$job->onQueue( $this->queue ?? Config::getQueue() )
);
}
}
23 changes: 23 additions & 0 deletions tests/SlackAlertsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Queue;
use Spatie\SlackAlerts\Exceptions\JobClassDoesNotExist;
use Spatie\SlackAlerts\Exceptions\WebhookUrlNotValid;
use Spatie\SlackAlerts\Facades\SlackAlert;
Expand Down Expand Up @@ -82,3 +83,25 @@

SlackAlert::message('test-data');
})->throws(JobClassDoesNotExist::class);

it('can send a message via a queue set in config file ', function(string $queue){
config()->set('slack-alerts.webhook_urls.default', 'https://test-domain.com');
config()->set('slack-alerts.queue', $queue);

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

Bus::assertDispatched( SendToSlackChannelJob::class);
})->with([
'default', 'my-queue'
]);

it('can send a message via a queue set at runtime ', function(string $queue){
config()->set('slack-alerts.webhook_urls.default', 'https://test-domain.com');
config()->set('slack-alerts.queue', 'custom-queue');

SlackAlert::onQueue( $queue )->message('test-data');

Bus::assertDispatched( SendToSlackChannelJob::class);
})->with([
'default', 'my-queue'
]);

0 comments on commit 7d89a8c

Please sign in to comment.