Skip to content

Commit

Permalink
Allow disabling notifications by the caller
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro committed Nov 23, 2022
1 parent 9514d32 commit 5ae947d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Http/Controllers/Health.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Health extends Controller
public function __construct(Service $healthService)
{
$this->healthService = $healthService;

$this->healthService->setCaller('web');
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,14 @@ public function string(?string $filters = '')
);
});
}

/**
* Set the caller.
*
* @param string $caller
*/
public function setCaller($caller)
{
$this->resourceChecker->setCurrentCaller($caller);
}
}
28 changes: 25 additions & 3 deletions src/Support/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ class Resource implements JsonSerializable
*/
protected $currentAction;

/**
* @var string
*/
protected $currentCaller;

/**
* @var bool|null
*/
Expand Down Expand Up @@ -195,11 +200,14 @@ public function instantiateChecker(string $checker)
* Check all targets for a resource.
*
* @param string $action
* @param string $caller
* @return resource
*/
public function check($action = 'resource')
public function check($action = 'resource', $caller = 'console')
{
$this->setCurrentAction($action)->targets->each(function (
$this->setCurrentAction($action)
->setCurrentCaller($caller)
->targets->each(function (
Target $target
) {
$target->check($target);
Expand Down Expand Up @@ -312,7 +320,8 @@ protected function notificationsAreEnabled()
return
$this->notify &&
config('health.notifications.enabled') &&
config('health.notifications.notify_on.'.$this->currentAction);
config('health.notifications.notify_on.'.$this->currentAction) &&
config('health.notifications.notify_from.'.$this->currentCaller, true);
}

/**
Expand All @@ -328,6 +337,19 @@ public function setCurrentAction(string $currentAction)
return $this;
}

/**
* Set current caller.
*
* @param string $currentCaller
* @return resource
*/
public function setCurrentCaller(string $currentCaller)
{
$this->currentCaller = $currentCaller;

return $this;
}

/**
* Resources setter.
*
Expand Down
34 changes: 33 additions & 1 deletion src/Support/ResourceChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class ResourceChecker
*/
protected $currentAction = 'check';

/**
* The current caller.
*
* @var string
*/
protected $currentCaller = 'console';

/**
* All resources.
*
Expand Down Expand Up @@ -57,6 +64,11 @@ class ResourceChecker
*/
protected $resourceLoader;

/**
* @var boolean
*/
protected $disableNotifications = false;

/**
* ResourceChecker constructor.
*
Expand Down Expand Up @@ -123,7 +135,7 @@ public function checkResource($resource)
$checked = $this->cache->remember($resource->slug, function () use (
$resource
) {
return $resource->check($this->getCurrentAction());
return $resource->check($this->getCurrentAction(), $this->getCurrentCaller());
});

$resource->targets = $checked->targets;
Expand Down Expand Up @@ -162,6 +174,16 @@ public function getCurrentAction()
return $this->currentAction;
}

/**
* Get current caller.
*
* @return string
*/
public function getCurrentCaller()
{
return $this->currentCaller;
}

/**
* Get all non global resources.
*
Expand Down Expand Up @@ -271,6 +293,16 @@ public function setCurrentAction($currentAction)
$this->currentAction = $currentAction;
}

/**
* Set the current caller.
*
* @param string $currentCaller
*/
public function setCurrentCaller($currentCaller)
{
$this->currentCaller = $currentCaller;
}

/**
* Resources setter.
*
Expand Down
7 changes: 6 additions & 1 deletion src/config/health.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
'resource' => false,
],

'notify_from' => [
'web' => false,
'console' => true,
],

'subject' => 'Health Status',

'action-title' => 'View App Health',
Expand All @@ -218,7 +223,7 @@
],

'users' => [
'model' => App\User::class,
'model' => App\Models\User::class,

'emails' => ['admin@mydomain.com'],
],
Expand Down

0 comments on commit 5ae947d

Please sign in to comment.