Skip to content
Snippets Groups Projects
Commit cefd3f98 authored by Tibo's avatar Tibo
Browse files

don't trigger heartbeat agent for each new record, is way too heavygit add app/

parent baa49c55
No related branches found
No related tags found
No related merge requests found
Pipeline #19717 failed
...@@ -21,34 +21,34 @@ use Symfony\Component\Finder\SplFileInfo; ...@@ -21,34 +21,34 @@ use Symfony\Component\Finder\SplFileInfo;
*/ */
class AgentScheduler class AgentScheduler
{ {
/** /**
* *
* @var LazyCollection<Sensor> * @var LazyCollection<Sensor>
*/ */
private $sensors; private $sensors;
// associative array // associative array
// trigger_label => array<Sensor> // trigger_label => array<Sensor>
private array $triggers; private array $triggers;
private function __construct() private function __construct()
{ {
$this->sensors = $this->autodiscover(); $this->sensors = $this->autodiscover();
$this->triggers = $this->register($this->sensors); $this->triggers = $this->register($this->sensors);
} }
private static $instance; private static $instance;
public static function get() : AgentScheduler public static function get() : AgentScheduler
{ {
if (self::$instance == null) { if (self::$instance == null) {
self::$instance = new self(); self::$instance = new self();
} }
return self::$instance; return self::$instance;
} }
/** /**
* *
* @return LazyCollection<Sensor> * @return LazyCollection<Sensor>
...@@ -57,7 +57,7 @@ class AgentScheduler ...@@ -57,7 +57,7 @@ class AgentScheduler
{ {
return $this->sensors; return $this->sensors;
} }
/** /**
* *
* @return LazyCollection<Sensor> * @return LazyCollection<Sensor>
...@@ -66,22 +66,22 @@ class AgentScheduler ...@@ -66,22 +66,22 @@ class AgentScheduler
{ {
$ROOT = __DIR__ . "/Sensor/"; $ROOT = __DIR__ . "/Sensor/";
return LazyCollection::make(File::allFiles($ROOT))->map(function (SplFileInfo $file) { return LazyCollection::make(File::allFiles($ROOT))->map(function (SplFileInfo $file) {
$interface_name = "\App\Sensor"; $interface_name = "\App\Sensor";
$class_name = '\App\Sensor\\' . $file->getFilenameWithoutExtension(); $class_name = '\App\Sensor\\' . $file->getFilenameWithoutExtension();
if (!is_a($class_name, $interface_name, true)) { if (!is_a($class_name, $interface_name, true)) {
return; return;
} }
$reflection = new \ReflectionClass($class_name); $reflection = new \ReflectionClass($class_name);
if ($reflection->isAbstract()) { if ($reflection->isAbstract()) {
return; return;
} }
return new $class_name; return new $class_name;
})->filter(); })->filter();
} }
/** /**
* *
* @param LazyCollection<Sensor> $sensors * @param LazyCollection<Sensor> $sensors
...@@ -98,7 +98,7 @@ class AgentScheduler ...@@ -98,7 +98,7 @@ class AgentScheduler
} }
return $triggers; return $triggers;
} }
/** /**
* Get the list of defined agent labels. * Get the list of defined agent labels.
* *
...@@ -110,42 +110,38 @@ class AgentScheduler ...@@ -110,42 +110,38 @@ class AgentScheduler
return $sensor->config()->label; return $sensor->config()->label;
})->toArray(); })->toArray();
} }
// ------------------ SCHEDULING of agents // ------------------ SCHEDULING of agents
public function notify(Record $record) public function notify(Record $record)
{ {
$trigger_label = $record->label; $trigger_label = $record->label;
if (! isset($this->triggers[$trigger_label])) { if (! isset($this->triggers[$trigger_label])) {
return; return;
} }
foreach ($this->triggers[$trigger_label] as $agent) { foreach ($this->triggers[$trigger_label] as $agent) {
/** @var Sensor $agent */ /** @var Sensor $agent */
RunAgent::dispatch($agent, $record); RunAgent::dispatch($agent, $record);
} }
// special one : trigger heartbeat
$agent = new Heartbeat();
RunAgent::dispatch($agent, $record);
} }
public function notifySummary(ReportSummary $summary) public function notifySummary(ReportSummary $summary)
{ {
(new StatusChangeDetector())->analyze($summary); (new StatusChangeDetector())->analyze($summary);
} }
public function notifyStatusChange(StatusChange $change) public function notifyStatusChange(StatusChange $change)
{ {
(new Sensor\ChangeAlert())->analyze($change); (new Sensor\ChangeAlert())->analyze($change);
} }
public function notifyReport(Report $report) public function notifyReport(Report $report)
{ {
$server = $report->server; $server = $report->server;
$reports = $this->lastReportsOf($server); $reports = $this->lastReportsOf($server);
$summary = new ReportSummary(); $summary = new ReportSummary();
$summary->time = time(); $summary->time = time();
$summary->server_id = $server->id; $summary->server_id = $server->id;
...@@ -153,7 +149,7 @@ class AgentScheduler ...@@ -153,7 +149,7 @@ class AgentScheduler
$summary->status_code = Status::max($reports)->code(); $summary->status_code = Status::max($reports)->code();
$summary->save(); $summary->save();
} }
/** /**
* Get the last report for each label. * Get the last report for each label.
* *
...@@ -167,7 +163,7 @@ class AgentScheduler ...@@ -167,7 +163,7 @@ class AgentScheduler
} }
return $reports->filter(); return $reports->filter();
} }
public function lastReportOf(Server $server, string $label) : ?Report public function lastReportOf(Server $server, string $label) : ?Report
{ {
$start = time() - 24 * 3600; $start = time() - 24 * 3600;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment