Skip to content

Commit 0bd94d4

Browse files
authored
Merge pull request #4 from opsie-app/feature/logging
[feature] Logging
2 parents 6885afb + 73a474b commit 0bd94d4

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

app/Commands/Monitor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ class Monitor extends Command
5555
*/
5656
public function handle()
5757
{
58-
$this->monitor = AppMonitor::website($this->argument('url'));
58+
$this->monitor = AppMonitor::website($this->argument('url'), $this);
5959

6060
$this->configureHttp();
6161
$this->configureWebhooks();
6262
$this->configureDns();
6363

64+
$this->line('Running the operator...', verbosity: 'v');
65+
6466
$this->monitor->run();
6567
}
6668

app/Concerns/SendsWebhooks.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function webhooks(array $webhooks)
3737
*/
3838
protected function deliverPayload(array $payload): void
3939
{
40+
/** @var \App\Monitor $this */
4041
foreach ($this->webhooks as $webhook) {
4142
WebhookCall::create()
4243
->url($webhook['url'])
@@ -46,6 +47,8 @@ protected function deliverPayload(array $payload): void
4647
->timeoutInSeconds($this->timeout)
4748
->withHeaders(['User-Agent' => 'Opsiebot/1.0'])
4849
->dispatch();
50+
51+
$this->cli->line('['.now()->toIso8601String().'] Sent webhook to '.$webhook['url'], verbosity: 'v');
4952
}
5053
}
5154
}

app/Monitor.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Concerns\MonitorsSsl;
88
use App\Concerns\SendsWebhooks;
99
use Illuminate\Support\Str;
10+
use LaravelZero\Framework\Commands\Command;
1011

1112
class Monitor
1213
{
@@ -40,21 +41,24 @@ class Monitor
4041
* Create a new Monitor instance.
4142
*
4243
* @param string $url
44+
* @param Command $cli
4345
* @return static
4446
*/
45-
public static function website(string $url)
47+
public static function website(string $url, Command $cli)
4648
{
47-
return new static($url);
49+
return new static($url, $cli);
4850
}
4951

5052
/**
5153
* Create a new Monitor instance.
5254
*
5355
* @param string $url
56+
* @param Command $cli
5457
* @return void
5558
*/
5659
public function __construct(
5760
protected string $url,
61+
protected Command $cli,
5862
) {
5963
$this->shouldCheckSsl = Str::startsWith($url, 'https://');
6064
}
@@ -66,7 +70,19 @@ public function __construct(
6670
*/
6771
public function run(): void
6872
{
73+
if ($this->shouldCheckSsl) {
74+
$this->cli->line('The SSL checks are enabled.');
75+
}
76+
77+
if ($this->shouldCheckDns) {
78+
$this->cli->line('The DNS checks and records retrievals are enabled.');
79+
}
80+
81+
$this->cli->line("The checks will run at an interval of {$this->interval} seconds.");
82+
6983
while (true) {
84+
$this->cli->line('['.now()->toIso8601String().'] Performing a check...', verbosity: 'v');
85+
7086
$httpPayload = $this->checkHttp($this->url);
7187
$sslPayload = $this->checkSsl($this->url);
7288
$dnsPayload = $this->checkDns($this->url);
@@ -78,6 +94,8 @@ public function run(): void
7894
'metadata' => $this->metadata,
7995
]);
8096

97+
$this->cli->line('Check done.', verbosity: 'v');
98+
8199
if ($this->once) {
82100
break;
83101
}

tests/Feature/MonitoringTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function test_healthy_website()
1919
'--webhook-secret' => ['secret'],
2020
'--dns-checking' => true,
2121
'--once' => true,
22+
'--verbose' => true,
2223
]);
2324

2425
Queue::assertPushed(CallWebhookJob::class, function ($job) {
@@ -65,6 +66,7 @@ public function test_down_website()
6566
'--webhook-secret' => ['secret'],
6667
'--dns-checking' => true,
6768
'--once' => true,
69+
'--verbose' => true,
6870
]);
6971

7072
Queue::assertPushed(CallWebhookJob::class, function ($job) {
@@ -113,6 +115,7 @@ public function test_ssl_broken_website()
113115
'--webhook-secret' => ['secret'],
114116
'--dns-checking' => true,
115117
'--once' => true,
118+
'--verbose' => true,
116119
]);
117120

118121
Queue::assertPushed(CallWebhookJob::class, function ($job) {

0 commit comments

Comments
 (0)