forked from pelican-dev/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckServerStatus.php
More file actions
66 lines (55 loc) · 2.53 KB
/
CheckServerStatus.php
File metadata and controls
66 lines (55 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace Notjami\Webhooks\Console\Commands;
use App\Models\Server;
use App\Repositories\Daemon\DaemonServerRepository;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Notjami\Webhooks\Enums\WebhookEvent;
use Notjami\Webhooks\Models\Webhook;
use Notjami\Webhooks\Services\DiscordWebhookService;
class CheckServerStatus extends Command
{
protected $signature = 'discord-webhooks:check-status';
protected $description = 'Check server status and trigger webhooks for status changes';
public function handle(DaemonServerRepository $repository, DiscordWebhookService $webhookService): int
{
// Get all servers that have webhooks configured
$serverIds = Webhook::enabled()
->whereNotNull('server_id')
->pluck('server_id')
->unique();
// Also check servers if there are global webhooks
$hasGlobalWebhooks = Webhook::enabled()
->whereNull('server_id')
->exists();
if ($hasGlobalWebhooks) {
$servers = Server::whereNull('status')->get();
} else {
$servers = Server::whereIn('id', $serverIds)->whereNull('status')->get();
}
foreach ($servers as $server) {
try {
$details = $repository->setServer($server)->getDetails();
$currentState = $details['state'] ?? 'offline';
$cacheKey = "webhook_server_status_{$server->id}";
$previousState = Cache::get($cacheKey, 'unknown');
// Always refresh the cache TTL, even if state hasn't changed
Cache::put($cacheKey, $currentState, now()->addHours(24));
if ($previousState !== $currentState) {
if ($previousState !== 'unknown') {
if ($currentState === 'running') {
$webhookService->triggerEvent(WebhookEvent::ServerStarted, $server);
$this->info("Server {$server->name} started - webhook triggered");
} elseif (in_array($currentState, ['offline', 'stopped'])) {
$webhookService->triggerEvent(WebhookEvent::ServerStopped, $server);
$this->info("Server {$server->name} stopped - webhook triggered");
}
}
}
} catch (\Exception $e) {
$this->error("Failed to check server {$server->name}: {$e->getMessage()}");
}
}
return self::SUCCESS;
}
}