Skip to content

Commit a475750

Browse files
authored
Merge pull request #2 from OpenConext/feature/support-custom-query
Improve query executed by DoctrineConnHealthCheck
2 parents b5cc6d3 + 99bb1b7 commit a475750

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,23 @@ services:
114114
- @test_service
115115
tags:
116116
- { name: surfnet.monitor.health_check }
117-
```
117+
```
118+
119+
## Overriding a default HealthCheck
120+
To run a custom query with the DoctrineConnectionHealthCheck you will need to override it in your own project.
121+
122+
For example in your ACME bunde that is using the monitor bundle:
123+
124+
`services.yml`
125+
```yaml
126+
# Override the service, service names can be found in `/src/Resources/config/services.yml`
127+
openconext.monitor.database_health_check:
128+
# Point to your own implementation of the check
129+
class: Acme\GreatSuccessBundle\HealthCheck\DoctrineConnectionHealthCheck
130+
# Do not forget to apply the correct tag
131+
tags:
132+
- { name: openconext.monitor.health_check }
133+
134+
```
135+
136+
The rest of the service configuration is up to your own needs. You can inject arguments, factory calls and other service features as need be.

src/HealthCheck/DoctrineConnectionHealthCheck.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
* Test if there is a working database connection.
2727
*
2828
* This test is based on the Doctrine configuration. The default entity manager is injected (if configured) and is used
29-
* to perform a simple query on the configured
30-
* connection.
29+
* to perform a simple query on the configured connection.
3130
*/
3231
class DoctrineConnectionHealthCheck implements HealthCheckInterface
3332
{
@@ -51,11 +50,20 @@ public function check(HealthReportInterface $report)
5150
{
5251
// Was the entityManager injected? When it is not the project does not use Doctrine ORM
5352
if (!is_null($this->entityManager)) {
54-
// Try a SELECT 1 query on the database to test if it's up and running
5553
try {
56-
$this->entityManager->getConnection()->exec('SELECT 1;');
54+
// Get the schema manager and grab the first table to later query on
55+
$sm = $this->entityManager->getConnection()->getSchemaManager();
56+
$tables = $sm->listTables();
57+
if (!empty($tables)) {
58+
$table = reset($tables);
59+
// Perform a light-weight query on the chosen table
60+
$query = 'SELECT * FROM `%s` LIMIT 1';
61+
$this->entityManager->getConnection()->exec(sprintf($query, $table->getName()));
62+
}
5763
} catch (Exception $e) {
58-
return HealthReport::buildStatusDown('Unable to execute a query on the database');
64+
// On error close the connection to prevent sleeping processes
65+
$this->entityManager->getConnection()->close();
66+
return HealthReport::buildStatusDown('Unable to execute a query on the database.');
5967
}
6068
}
6169
return $report;

0 commit comments

Comments
 (0)