Skip to content

Commit b258b9b

Browse files
committed
Merge pull request #73 from kbond/extra-reporters
Added ability to define extra reporters for use in command/controller
2 parents 4a4a25b + ab6dedd commit b258b9b

File tree

8 files changed

+225
-8
lines changed

8 files changed

+225
-8
lines changed

Command/HealthCheckCommand.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Liip\MonitorBundle\Helper\ConsoleReporter;
66
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Input\InputOption;
78
use Symfony\Component\Console\Output\OutputInterface;
89
use Symfony\Component\Console\Input\InputArgument;
910
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
@@ -21,6 +22,12 @@ protected function configure()
2122
InputArgument::OPTIONAL,
2223
'The name of the service to be used to perform the health check.'
2324
),
25+
new InputOption(
26+
'reporter',
27+
InputOption::VALUE_IS_ARRAY,
28+
'Additional reporters to run.',
29+
array()
30+
)
2431
));
2532
}
2633

@@ -29,6 +36,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
2936
$checkName = $input->getArgument('checkName');
3037
$runner = $this->getContainer()->get('liip_monitor.runner');
3138
$runner->addReporter(new ConsoleReporter($output));
39+
$runner->useAdditionalReporters($input->getOption('reporter'));
3240

3341
if (0 === count($runner->getChecks())) {
3442
$output->writeln('<error>No checks configured.</error>');

Controller/HealthCheckController.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Symfony\Component\HttpFoundation\JsonResponse;
77
use Symfony\Component\HttpFoundation\Request;
88
use Symfony\Component\HttpFoundation\Response;
9-
use ZendDiagnostics\Runner\Runner;
9+
use Liip\MonitorBundle\Runner;
1010
use Liip\MonitorBundle\Helper\PathHelper;
1111

1212
class HealthCheckController
@@ -70,11 +70,12 @@ public function listAction()
7070
}
7171

7272
/**
73+
* @param Request $request
7374
* @return \Symfony\Component\HttpFoundation\Response
7475
*/
75-
public function runAllChecksAction()
76+
public function runAllChecksAction(Request $request)
7677
{
77-
$report = $this->runTests();
78+
$report = $this->runTests($request);
7879

7980
return new JsonResponse(array(
8081
'checks' => $report->getResults(),
@@ -84,23 +85,32 @@ public function runAllChecksAction()
8485

8586
/**
8687
* @param string $checkId
88+
* @param Request $request
8789
* @return \Symfony\Component\HttpFoundation\Response
8890
*/
89-
public function runSingleCheckAction($checkId)
91+
public function runSingleCheckAction($checkId, Request $request)
9092
{
91-
$results = $this->runTests($checkId)->getResults();
93+
$results = $this->runTests($request, $checkId)->getResults();
9294

9395
return new JsonResponse($results[0]);
9496
}
9597

9698
/**
99+
* @param Request $request
97100
* @param string|null $checkId
98101
* @return ArrayReporter
99102
*/
100-
protected function runTests($checkId = null)
103+
protected function runTests(Request $request, $checkId = null)
101104
{
105+
$reporters = $request->query->get('reporters', array());
106+
107+
if (!is_array($reporters)) {
108+
$reporters = array($reporters);
109+
}
110+
102111
$reporter = new ArrayReporter();
103112
$this->runner->addReporter($reporter);
113+
$this->runner->useAdditionalReporters($reporters);
104114
$this->runner->run($checkId);
105115

106116
return $reporter;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Liip\MonitorBundle\DependencyInjection\Compiler;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Reference;
8+
9+
/**
10+
* @author Kevin Bond <kevinbond@gmail.com>
11+
*/
12+
class AdditionalReporterCompilerPass implements CompilerPassInterface
13+
{
14+
public function process(ContainerBuilder $container)
15+
{
16+
if (false === $container->hasDefinition('liip_monitor.runner')) {
17+
return;
18+
}
19+
20+
$definition = $container->getDefinition('liip_monitor.runner');
21+
22+
foreach ($container->findTaggedServiceIds('liip_monitor.additional_reporter') as $id => $tags) {
23+
foreach ($tags as $attributes) {
24+
$alias = empty($attributes['alias']) ? $id : $attributes['alias'];
25+
$definition->addMethodCall('addAdditionalReporter', array($alias, new Reference($id)));
26+
}
27+
}
28+
}
29+
}

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ liip_monitor:
8383
checks:
8484
php_extensions: [apc, xdebug]
8585
```
86+
8687
## Adding Health Checks ##
8788
8889
Once you implemented the class then it's time to register the check service with our service container:
@@ -102,7 +103,6 @@ as long as the service is properly tagged. The ``alias`` is optional and will th
102103
define the ``id`` used when running health checks individually, otherwise the full service
103104
id must be used in this case.
104105

105-
106106
## Available Health Checks ##
107107

108108
On top of all the checks provided by the ZendDiagnostics library this Bundle adds the following
@@ -192,6 +192,28 @@ To run health checks as a composer post-install or post-update script, simply ad
192192
},
193193
```
194194

195+
## Adding Additional Reporters ##
196+
197+
There are two default reporters: `ArrayReporter` for the REST API and `ConsoleReporter` for the CLI command. You can
198+
add additional reporters to be used by either of these.
199+
200+
First, define an additional reporter service and tag it with `liip_monitor.additional_reporter`:
201+
202+
```
203+
my_reporter:
204+
class: My\Reporter
205+
tags:
206+
- { name: liip_monitor.additional_reporter, alias: my_reporter }
207+
```
208+
209+
To run additional reporters with the CLI, add `--reporter=...` options for each one:
210+
211+
$ ./app/console monitor:health --reporter=my_reporter
212+
213+
To run this reporter with the REST API, add a `reporters` query parameter:
214+
215+
/monitor/health?reporters[]=my_reporter
216+
195217
## Full Default Config ##
196218

197219
```yaml

Resources/config/runner.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<parameters>
8-
<parameter key="liip_monitor.runner.class">ZendDiagnostics\Runner\Runner</parameter>
8+
<parameter key="liip_monitor.runner.class">Liip\MonitorBundle\Runner</parameter>
99
</parameters>
1010

1111
<services>

Runner.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Liip\MonitorBundle;
4+
5+
use ZendDiagnostics\Runner\Reporter\ReporterInterface;
6+
use ZendDiagnostics\Runner\Runner as BaseRunner;
7+
8+
/**
9+
* @author Kevin Bond <kevinbond@gmail.com>
10+
*/
11+
class Runner extends BaseRunner
12+
{
13+
private $additionalReporters = array();
14+
15+
/**
16+
* @param string $alias
17+
* @param ReporterInterface $reporter
18+
*/
19+
public function addAdditionalReporter($alias, ReporterInterface $reporter)
20+
{
21+
$this->additionalReporters[$alias] = $reporter;
22+
}
23+
24+
/**
25+
* @param array $aliases
26+
*/
27+
public function useAdditionalReporters(array $aliases)
28+
{
29+
foreach ($this->additionalReporters as $alias => $reporter) {
30+
if (in_array($alias, $aliases)) {
31+
$this->addReporter($reporter);
32+
}
33+
}
34+
}
35+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Liip\MonitorBundle\Tests\DependencyInjection\Compiler;
4+
5+
use Liip\MonitorBundle\DependencyInjection\Compiler\AdditionalReporterCompilerPass;
6+
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
use Symfony\Component\DependencyInjection\Definition;
9+
use Symfony\Component\DependencyInjection\Reference;
10+
11+
/**
12+
* @author Kevin Bond <kevinbond@gmail.com>
13+
*/
14+
class AdditionalReporterCompilerPassTest extends AbstractCompilerPassTestCase
15+
{
16+
public function testProcessWithAlias()
17+
{
18+
$runner = new Definition();
19+
$this->setDefinition('liip_monitor.runner', $runner);
20+
21+
$reporter = new Definition();
22+
$reporter->addTag('liip_monitor.additional_reporter', array('alias' => 'foo'));
23+
$this->setDefinition('foo_reporter', $reporter);
24+
25+
$this->compile();
26+
27+
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
28+
'liip_monitor.runner',
29+
'addAdditionalReporter',
30+
array(
31+
'foo',
32+
new Reference('foo_reporter')
33+
)
34+
);
35+
}
36+
37+
public function testProcessWithoutAlias()
38+
{
39+
$runner = new Definition();
40+
$this->setDefinition('liip_monitor.runner', $runner);
41+
42+
$reporter = new Definition();
43+
$reporter->addTag('liip_monitor.additional_reporter');
44+
$this->setDefinition('foo_reporter', $reporter);
45+
46+
$this->compile();
47+
48+
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall(
49+
'liip_monitor.runner',
50+
'addAdditionalReporter',
51+
array(
52+
'foo_reporter',
53+
new Reference('foo_reporter')
54+
)
55+
);
56+
}
57+
58+
protected function registerCompilerPass(ContainerBuilder $container)
59+
{
60+
$container->addCompilerPass(new AdditionalReporterCompilerPass());
61+
}
62+
}

Tests/RunnerTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Liip\MonitorBundle\Tests;
4+
5+
use Liip\MonitorBundle\Runner;
6+
use ZendDiagnostics\Runner\Reporter\ReporterInterface;
7+
8+
/**
9+
* @author Kevin Bond <kevinbond@gmail.com>
10+
*/
11+
class RunnerTest extends \PHPUnit_Framework_TestCase
12+
{
13+
public function testAdditionalReporters()
14+
{
15+
$runner = new Runner();
16+
17+
$this->assertCount(0, $runner->getReporters());
18+
19+
$runner->addAdditionalReporter('foo', $this->createMockReporter());
20+
$runner->addAdditionalReporter('bar', $this->createMockReporter());
21+
22+
$this->assertCount(0, $runner->getReporters());
23+
24+
$runner->useAdditionalReporters(array('baz'));
25+
26+
$this->assertCount(0, $runner->getReporters());
27+
28+
$runner->useAdditionalReporters(array('foo'));
29+
30+
$this->assertCount(1, $runner->getReporters());
31+
32+
$runner->useAdditionalReporters(array('bar'));
33+
34+
$this->assertCount(2, $runner->getReporters());
35+
36+
$runner = new Runner();
37+
$runner->addAdditionalReporter('foo', $this->createMockReporter());
38+
$runner->addAdditionalReporter('bar', $this->createMockReporter());
39+
$runner->useAdditionalReporters(array('bar', 'foo'));
40+
41+
$this->assertCount(2, $runner->getReporters());
42+
}
43+
44+
/**
45+
* @return ReporterInterface
46+
*/
47+
private function createMockReporter()
48+
{
49+
return $this->getMock('ZendDiagnostics\Runner\Reporter\ReporterInterface');
50+
}
51+
}

0 commit comments

Comments
 (0)