Skip to content

Commit

Permalink
Merge pull request #50 from igoptx/feature/addDelay
Browse files Browse the repository at this point in the history
Add functions to delay the job that delivers the message
  • Loading branch information
Nielsvanpach authored Dec 30, 2024
2 parents 9a382ef + 058f72c commit d5c9f44
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ use Spatie\DiscordAlerts\Facades\DiscordAlert;
DiscordAlert::to('https://custom-url.com')->message("You have a new subscriber to the {$newsletter->name} newsletter!");
```

### Using the delay feature

The `delayMinutes` of the `delayHours` function can be used to delay the message to Discord and can be used in parallel.

```php
use Spatie\DiscordAlerts\Facades\DiscordAlert;

DiscordAlert::to('https://custom-url.com')->delayMinutes(5)->message("You have a new subscriber to the {$newsletter->name} newsletter!");
DiscordAlert::to('https://custom-url.com')->delayHours(1)->message("You have a new subscriber to the {$newsletter->name} newsletter!");

DiscordAlert::to('https://custom-url.com')->delayHours(1)->delayMinutes(10)->message("You have a new subscriber to the {$newsletter->name} newsletter!");
```

## Formatting

### Markdown
Expand Down
17 changes: 16 additions & 1 deletion src/DiscordAlert.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,24 @@ class DiscordAlert
{
protected string $webhookUrlName = 'default';

protected int $delay = 0; // minutes

public function to(string $webhookUrlName): self
{
$this->webhookUrlName = $webhookUrlName;
$this->delay = 0;

return $this;
}

public function delayMinutes(int $minutes = 0){

Check failure on line 19 in src/DiscordAlert.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\DiscordAlerts\DiscordAlert::delayMinutes() has no return type specified.

Check failure on line 19 in src/DiscordAlert.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\DiscordAlerts\DiscordAlert::delayMinutes() has no return type specified.
$this->delay+= $minutes;

return $this;
}

public function delayHours(int $hours = 0){

Check failure on line 25 in src/DiscordAlert.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\DiscordAlerts\DiscordAlert::delayHours() has no return type specified.

Check failure on line 25 in src/DiscordAlert.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Spatie\DiscordAlerts\DiscordAlert::delayHours() has no return type specified.
$this->delay+= $hours * 60;

return $this;
}
Expand Down Expand Up @@ -37,7 +52,7 @@ public function message(string $text, array $embeds = []): void

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

dispatch($job)->onConnection(Config::getConnection());
dispatch($job)->delay(now()->addMinutes($this->delay))->onConnection(Config::getConnection());
}

private function parseNewline(string $text): string
Expand Down
2 changes: 2 additions & 0 deletions src/Facades/DiscordAlert.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

/**
* @method static self to(string $text)
* @method static self delayMinutes(int $minutes)
* @method static self delayHours(int $hours)
* @method static void message(string $text, array $embeds = null)
*
* @see \Spatie\DiscordAlerts\DiscordAlert
Expand Down
30 changes: 30 additions & 0 deletions tests/DiscordAlertsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,33 @@
return $job->text === "test " . PHP_EOL . " data" && count($job->embeds);
});
});

it('can delay a message by minutes', function () {
config()->set('discord-alerts.webhook_urls.default', 'https://test-domain.com');

DiscordAlert::delayMinutes(10)->message('test-data');

Bus::assertDispatched(SendToDiscordChannelJob::class, function ($job) {
return $job->delay === 10;
});
});

it('can delay a message by hours', function () {
config()->set('discord-alerts.webhook_urls.default', 'https://test-domain.com');

DiscordAlert::delayHours(1)->message('test-data');

Bus::assertDispatched(SendToDiscordChannelJob::class, function ($job) {
return $job->delay === 60;
});
});

it('can delay a message by hours and minutes', function () {
config()->set('discord-alerts.webhook_urls.default', 'https://test-domain.com');

DiscordAlert::delayHours(1)->delayMinutes(10)->message('test-data');

Bus::assertDispatched(SendToDiscordChannelJob::class, function ($job) {
return $job->delay === 70;
});
});

0 comments on commit d5c9f44

Please sign in to comment.