Skip to content

Commit

Permalink
Merge pull request #90 from JarvusInnovations/features/cron-commands
Browse files Browse the repository at this point in the history
refactor: convert heartbeat event handlers to console commands
  • Loading branch information
themightychris authored Jul 21, 2020
2 parents 3db6eb3 + 2832e23 commit 3e5e2c4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
9 changes: 9 additions & 0 deletions console-commands/gk/flush-metrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$logger = $_COMMAND['LOGGER'];

$logger->info('Flushing metrics');

$flushed = Gatekeeper\Metrics\Metrics::flushMetricSamples();

$logger->info("{$flushed} metrics flushed");
9 changes: 9 additions & 0 deletions console-commands/gk/ping-endpoints.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$logger = $_COMMAND['LOGGER'];

$logger->info('Pinging endpoints...');

$pinged = Gatekeeper\Endpoints\Pinger::pingOverdueEndpoints($logger);

$logger->info("{$pinged} endpoints pinged");
7 changes: 0 additions & 7 deletions event-handlers/Gatekeeper/heartbeat/40_flush-metrics.php

This file was deleted.

3 changes: 0 additions & 3 deletions event-handlers/Gatekeeper/heartbeat/50_ping-endpoints.php

This file was deleted.

14 changes: 9 additions & 5 deletions php-classes/Gatekeeper/Endpoints/Pinger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
use Cache;
use HttpProxy;

use Psr\Log\LoggerInterface;

use Gatekeeper\ApiRequestHandler;
use Gatekeeper\Alerts\TestFailed;
use Gatekeeper\Transactions\PingTransaction;


class Pinger
{
public static function pingOverdueEndpoints($verbose = false)
public static function pingOverdueEndpoints(LoggerInterface $logger = null)
{
$endpoints = Endpoint::getAllByWhere('PingFrequency IS NOT NULL');

Expand All @@ -24,14 +26,16 @@ public static function pingOverdueEndpoints($verbose = false)
$lastPing === false
|| $Endpoint->PingFrequency*60 < time()-$lastPing['time']
) {
static::pingEndpoint($Endpoint, $verbose);
static::pingEndpoint($Endpoint, $logger);
}
}

return count($endpoints);
}

public static function pingEndpoint(Endpoint $Endpoint, $verbose = false)
public static function pingEndpoint(Endpoint $Endpoint, LoggerInterface $logger = null)
{
$verbose && printf('Testing endpoint: /%s...', $Endpoint->Path) && flush();
$logger && $logger->info("/{$Endpoint->Path} is being pinged");


// execute and capture request
Expand Down Expand Up @@ -108,7 +112,7 @@ public static function pingEndpoint(Endpoint $Endpoint, $verbose = false)
}


$verbose && printf("%s\n", $testPassed ? 'passed' : 'failed') && flush();
$logger && $logger->info("/{$Endpoint->Path} ". ($testPassed ? 'passed' : 'failed'));


// cache result and timestamp
Expand Down

0 comments on commit 3e5e2c4

Please sign in to comment.