Skip to content

Commit

Permalink
Merge pull request #214 from tobz/feature/configurable-failure-http-s…
Browse files Browse the repository at this point in the history
…tatus

Make the HTTP failure status code configurable.
  • Loading branch information
kbond authored Aug 1, 2019
2 parents 7a3bc24 + 5366331 commit 28189fa
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 6 deletions.
9 changes: 6 additions & 3 deletions Controller/HealthCheckController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ class HealthCheckController
protected $runnerManager;
protected $pathHelper;
protected $template;
protected $failureStatusCode;

/**
* @param RunnerManager $runnerManager
* @param PathHelper $pathHelper
* @param $template
* @param $failureStatusCode
*/
public function __construct(RunnerManager $runnerManager, PathHelper $pathHelper, $template)
public function __construct(RunnerManager $runnerManager, PathHelper $pathHelper, $template, $failureStatusCode)
{
$this->runnerManager = $runnerManager;
$this->pathHelper = $pathHelper;
$this->template = $template;
$this->failureStatusCode = $failureStatusCode;
}

/**
Expand Down Expand Up @@ -130,7 +133,7 @@ public function runAllChecksHttpStatusAction(Request $request)

return new Response(
'',
($report->getGlobalStatus() === ArrayReporter::STATUS_OK ? 200 : 502)
($report->getGlobalStatus() === ArrayReporter::STATUS_OK ? 200 : $this->failureStatusCode)
);
}

Expand All @@ -146,7 +149,7 @@ public function runSingleCheckHttpStatusAction($checkId, Request $request)

return new Response(
'',
($report->getGlobalStatus() === ArrayReporter::STATUS_OK ? 200 : 502)
($report->getGlobalStatus() === ArrayReporter::STATUS_OK ? 200 : $this->failureStatusCode)
);
}

Expand Down
6 changes: 5 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('liip_monitor');

// Keep compatibility with symfony/config < 4.2
if (\method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->getRootNode();
Expand Down Expand Up @@ -51,6 +51,10 @@ public function getConfigTreeBuilder()
->children()
->booleanNode('enable_controller')->defaultFalse()->end()
->scalarNode('view_template')->defaultNull()->end()
->integerNode('failure_status_code')
->min(100)->max(598)
->defaultValue(502)
->end()
->arrayNode('mailer')
->canBeEnabled()
->children()
Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/LiipMonitorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function load(array $configs, ContainerBuilder $container)

if ($config['enable_controller']) {
$container->setParameter(sprintf('%s.view_template', $this->getAlias()), $config['view_template']);
$container->setParameter(sprintf('%s.failure_status_code', $this->getAlias()), $config['failure_status_code']);
$loader->load('controller.xml');
}

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ groups. Additionally, the `monitor:list` has an option `--groups` to list all re
liip_monitor:
enable_controller: false
view_template: null
failure_status_code: 502
mailer:
enabled: false
recipient: ~ # Required
Expand Down
1 change: 1 addition & 0 deletions Resources/config/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<argument type="service" id="liip_monitor.helper.runner_manager" />
<argument type="service" id="liip_monitor.helper" />
<argument>%liip_monitor.view_template%</argument>
<argument>%liip_monitor.failure_status_code%</argument>
</service>
</services>
</container>
4 changes: 2 additions & 2 deletions Resources/views/health/index.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
</dd>

<dt><a href="<?php echo $request->getUriForPath($request->getPathInfo().'http_status_checks') ?>"><?php echo $request->getPathInfo().'http_status_checks' ?></a></dt>
<dd>Performs all health checks and returns the results within the HTTP Status Code (200 if checks are OK, 502 otherwise).
<dd>Performs all health checks and returns the results within the HTTP Status Code (200 if checks are OK, 502 otherwise). The failure status code is configurable as <code>failure_status_code</code>.
<pre>
$ curl -XPOST -H "Accept: application/json" <?php echo $request->getUriForPath($request->getPathInfo().'http_status_checks') ?>

Expand All @@ -141,7 +141,7 @@
</dd>

<dt><?php echo $request->getPathInfo().'http_status_check/check_id' ?></dt>
<dd>Performs the health check specified by <code>check_id</code> and returns the result within the HTTP Status Code (200 if checks are OK, 502 otherwise).
<dd>Performs the health check specified by <code>check_id</code> and returns the result within the HTTP Status Code (200 if checks are OK, 502 otherwise). The failure status code is configurable as <code>failure_status_code</code>.
<pre>
$ curl -XPOST -H "Accept: application/json" <?php echo $request->getUriForPath($request->getPathInfo().'http_status_check/monitor.check.redis') ?>

Expand Down

0 comments on commit 28189fa

Please sign in to comment.