Skip to content
Snippets Groups Projects
StatusChangeDetection.php 2.56 KiB
Newer Older
Tibo's avatar
Tibo committed
<?php

namespace App\Jobs;

Tibo's avatar
Tibo committed
use App\Organization;
use App\Server;
use App\StatusChange;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class StatusChangeDetection implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        foreach (Organization::all() as $organization) {
            /* @var $organization \App\Organization */
            foreach ($organization->servers as $server) {
                $this->detectChangeForServer($server);
            }
        }
    }

Tibo's avatar
Tibo committed
    public function detectChangeForServer(Server $server)
    {
Tibo's avatar
Tibo committed
        $last_change = StatusChange::getLastChangeForServer($server->id);
        $current_status = $server->status();

        if ($last_change->status == $current_status) {
            // no change
            return;
        }

        $change = new StatusChange();
        $change->server_id = $server->id;
        $change->time = time();
        $change->status = $current_status;
        $change->save();

        $this->sendNotificationIfRequired($change);
    }

    /**
     * Maximum number of notifications sent per day.
     */
    const NOTIFICATIONS_PER_DAY = 4;

    public function sendNotificationIfRequired(StatusChange $change)
    {
        $server = $change->server();
        $server_id = $server->id;
        $onedayago = time() - 24 * 3600;
        $sent_notifications_count = Notification::findForServer($server_id, $onedayago)->count();
Tibo's avatar
Tibo committed

        if ($sent_notifications_count < self::NOTIFICATIONS_PER_DAY) {
            $notification = new Notification();
            $notification->server()->associate($server);
            $notification->type = "change";
            $notification->change_id = $change->id;
            $notification->saveAndSend();

            return;
Tibo's avatar
Tibo committed
        }

        if ($sent_notifications_count == self::NOTIFICATIONS_PER_DAY) {
            $notification = new Notification();
            $notification->server()->associate($server);
            $notification->type = "bouncing";
            $notification->change_id = $change->id;
            $notification->saveAndSend();

            return;
        }

        // nothing to do if number of sent notifications > COUNT

Tibo's avatar
Tibo committed
    }
}