Skip to content

Commit

Permalink
Add 'report' config
Browse files Browse the repository at this point in the history
  • Loading branch information
ttrig committed May 22, 2024
1 parent 0eb1ab2 commit e7bf860
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- **BREAKING**: Enum `ResultState`.
- Config `butler.health.heartbeat.report`.

## [0.5.2] - 2023-12-29
### Added
Expand Down
1 change: 1 addition & 0 deletions config/butler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'heartbeat' => [
'url' => env('BUTLER_HEALTH_HEARTBEAT_URL'),
'token' => env('BUTLER_HEALTH_HEARTBEAT_TOKEN'),
'report' => env('BUTLER_HEALTH_HEARTBEAT_REPORT'),
],

],
Expand Down
11 changes: 7 additions & 4 deletions src/Heartbeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ public function send(string $slug, int $minutes = 1): void
if ($this->recording) {
$this->recorded[] = $url;
} else {
Http::withToken(config('butler.health.heartbeat.token'))
$config = config('butler.health.heartbeat');

Http::withToken($config['token'] ?? null)
->timeout(5)
->acceptJson()
->post($url)
->onError(function ($response) {
report($response->toException());
});
->onError(fn ($response) => report_if(
$config['report'] ?? false,
$response->toException(),
));
}
}

Expand Down
13 changes: 12 additions & 1 deletion tests/HeartbeatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,19 @@ public function test_send_happy_path()
&& $request->url() === 'http://localhost/foobar/2');
}

public function test_send_sad_path_reports_exception()
public function test_send_sad_path_do_not_report_exception()
{
Http::fakeSequence()->pushStatus(500);

Heartbeat::send('foobar', 2);

Http::assertSentCount(1);
}

public function test_send_sad_path_reports_exception_when_configured()
{
config(['butler.health.heartbeat.report' => true]);

$this->expectException(RequestException::class);
$this->expectExceptionMessage('HTTP request returned status code 500');

Expand Down

0 comments on commit e7bf860

Please sign in to comment.