Skip to content

Commit

Permalink
Merge pull request #166 from silinternational/release/5.2.0
Browse files Browse the repository at this point in the history
Release 5.2.0 -- new monitoring option
  • Loading branch information
briskt authored Mar 13, 2024
2 parents 4d077b7 + a4cbd5d commit 26280ab
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 19 deletions.
39 changes: 39 additions & 0 deletions application/common/components/Monitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Sil\Idp\IdSync\common\components;

use GuzzleHttp\Client;
use yii\base\Component;

class Monitor extends Component
{
/**
* @var null|string the URL to request for a heartbeat check
*/
public $heartbeatUrl = '';

/**
* @var null|string the HTTP request method (verb) to use for a heartbeat check
*/
public $heartbeatMethod = '';

public function Heartbeat()
{
if (empty($this->heartbeatUrl)) {
return;
}

$client = new Client();

$method = 'POST';
if (!empty($this->heartbeatMethod)) {
$method = $this->heartbeatMethod;
}

try {
$client->request($method, $this->heartbeatUrl);
} catch (\Throwable $e) {
\Yii::error('heartbeat error: ' . $e->getMessage());
}
}
}
9 changes: 8 additions & 1 deletion application/common/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Sentry\Event;
use Sil\Idp\IdSync\common\components\IdBrokerBase;
use Sil\Idp\IdSync\common\components\IdStoreBase;
use Sil\Idp\IdSync\common\components\Monitor;
use Sil\Idp\IdSync\common\components\notify\EmailServiceNotifier;
use Sil\JsonLog\target\EmailServiceTarget;
use Sil\JsonLog\target\JsonStreamTarget;
Expand Down Expand Up @@ -131,7 +132,7 @@
'clientOptions' => [
'attach_stacktrace' => false, // stack trace identifies the logger call stack, not helpful
'environment' => YII_ENV,
'release' => 'idp-id-sync@5.1.2',
'release' => 'idp-id-sync@5.2.0',
'before_send' => function (Event $event) use ($idpName): ?Event {
$event->setExtra(['idp' => $idpName]);
return $event;
Expand All @@ -142,6 +143,12 @@
],

'notifier' => $notifierConfig,

'monitor' => [
'class' => Monitor::class,
'heartbeatUrl' => Env::get('HEARTBEAT_URL'),
'heartbeatMethod' => Env::get('HEARTBEAT_METHOD'),
]
],
'params' => [
'syncSafetyCutoff' => Env::get('SYNC_SAFETY_CUTOFF'),
Expand Down
55 changes: 37 additions & 18 deletions application/console/controllers/BatchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Sil\Idp\IdSync\console\controllers;

use Sentry\CheckInStatus;
use Sil\Idp\IdSync\common\components\Monitor;
use Sil\Idp\IdSync\common\traits\SyncProvider;
use Yii;
use yii\console\Controller;
Expand All @@ -15,19 +16,28 @@ class BatchController extends Controller

public function actionFull()
{
$checkInId = captureCheckIn(
slug: Yii::$app->params['sentryMonitorSlug'],
status: CheckInStatus::inProgress()
);
$sentryMonitorSlug = Yii::$app->params['sentryMonitorSlug'];
if ($sentryMonitorSlug !== "") {
$checkInId = captureCheckIn(
slug: $sentryMonitorSlug,
status: CheckInStatus::inProgress()
);
}

$synchronizer = $this->getSynchronizer();
$synchronizer->syncAllNotifyException();

captureCheckIn(
slug: Yii::$app->params['sentryMonitorSlug'],
status: CheckInStatus::ok(),
checkInId: $checkInId,
);
if ($sentryMonitorSlug != "") {
captureCheckIn(
slug: $sentryMonitorSlug,
status: CheckInStatus::ok(),
checkInId: $checkInId,
);
}

/* @var $monitor Monitor */
$monitor = Yii::$app->monitor;
$monitor->Heartbeat();
}

/**
Expand All @@ -36,21 +46,30 @@ public function actionFull()
*/
public function actionIncremental()
{
$checkInId = captureCheckIn(
slug: Yii::$app->params['sentryMonitorSlug'],
status: CheckInStatus::inProgress()
);
$sentryMonitorSlug = Yii::$app->params['sentryMonitorSlug'];
if ($sentryMonitorSlug !== "") {
$checkInId = captureCheckIn(
slug: $sentryMonitorSlug,
status: CheckInStatus::inProgress()
);
}

$synchronizer = $this->getSynchronizer();
$synchronizer->syncUsersChangedSince(strtotime('-11 minutes'));

$synchronizer = $this->getSynchronizer();
$synchronizer->syncAllNotifyException();

captureCheckIn(
slug: Yii::$app->params['sentryMonitorSlug'],
status: CheckInStatus::ok(),
checkInId: $checkInId,
);
if ($sentryMonitorSlug != "") {
captureCheckIn(
slug: $sentryMonitorSlug,
status: CheckInStatus::ok(),
checkInId: $checkInId,
);
}

/* @var $monitor Monitor */
$monitor = Yii::$app->monitor;
$monitor->Heartbeat();
}
}
4 changes: 4 additions & 0 deletions local.env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,7 @@ SENTRY_MONITOR_SLUG=
#TEST_SECURE_USER_CONFIG_apiKey=abc123
#TEST_SECURE_USER_CONFIG_apiSecret=abc123
#TEST_SECURE_USER_EMPLOYEE_ID=123456

# Optional: configure the URL and http method of a monitoring service to call after every successful sync
HEARTBEAT_URL=https://push.nodeping.com/v1?id=MY_CHECK_ID_HERE&checktoken=LONG_CHECK_TOKEN_HERE'
#HEARTBEAT_METHOD=POST

0 comments on commit 26280ab

Please sign in to comment.