mal heartbeat: qol

This commit is contained in:
Irfan 2022-02-18 05:13:23 +05:00
parent 292723ac1e
commit c1d1f8465f
2 changed files with 25 additions and 10 deletions

View File

@ -15,13 +15,13 @@ class SourceHeartbeatEvent extends Event
public const BAD_HEALTH = 1;
public const GOOD_HEALTH = 0;
public $health;
public $status;
public int $health;
public int $status;
/**
* SourceHeartbeatEvent constructor.
* @param int $health
* @param int $status
* @param int|null $health
* @param int|null $status
*/
public function __construct(?int $health, ?int $status)
{

View File

@ -29,7 +29,10 @@ class SourceHeartbeatListener
if (SourceHeartbeatProvider::isFailoverEnabled()) {
$lastFailoverLockTimestamp = $this->getLastFailoverLockTimestamp();
$this->logger->debug('Failover is RUNNING');
if (env('APP_DEBUG', false)) {
$this->logger->debug('Failover is RUNNING');
}
// Disable failover if it has expired
if (time() > ($lastFailoverLockTimestamp + env('SOURCE_BAD_HEALTH_RECHECK'))) {
@ -48,7 +51,10 @@ class SourceHeartbeatListener
public function handle(SourceHeartbeatEvent $event)
{
$eventCount = $this->insertFail($event);
$this->logger->debug('Event count: '.$eventCount);
if (env('APP_DEBUG', false)) {
$this->logger->debug('Event count: '.$eventCount);
}
if ($this->getSuccessfulRequestsScore() <= 0.25) {
$this->enableFailover();
@ -70,7 +76,10 @@ class SourceHeartbeatListener
{
// create lock file
Storage::put('source_failover.lock', '');
$this->logger->debug('Failover ENABLED');
if (env('APP_DEBUG', false)) {
$this->logger->debug('Failover ENABLED');
}
}
private function disableFailover()
@ -88,8 +97,11 @@ class SourceHeartbeatListener
if ($score >= env('SOURCE_GOOD_HEALTH_SCORE', 0.9)) {
$this->disableFailover();
$this->logger->debug('Failover disabled; Score: '.$score);
$this->logger->debug('Failover DISABLED');
if (env('APP_DEBUG', false)) {
$this->logger->debug('Failover disabled; Score: '.$score);
$this->logger->debug('Failover DISABLED');
}
return true;
}
@ -149,7 +161,10 @@ class SourceHeartbeatListener
}
$scored = $score / max($totalFails, 1);
$this->logger->debug('Failover successful requests score: '.$scored);
if(env('APP_DEBUG', false)) {
$this->logger->debug('Failover successful requests score: '.$scored);
}
return $scored;
}