From b300c75777fa66d320ea9060d4467245841aadb3 Mon Sep 17 00:00:00 2001 From: Christoph Reiter <reiter.christoph@gmail.com> Date: Thu, 3 Feb 2022 16:50:33 +0100 Subject: [PATCH] Add a simple health check Check if the LDAP connections works --- src/Service/HealthCheck.php | 47 +++++++++++++++++++++++++++++++++++++ src/Service/LDAPApi.php | 7 ++++++ 2 files changed, 54 insertions(+) create mode 100644 src/Service/HealthCheck.php diff --git a/src/Service/HealthCheck.php b/src/Service/HealthCheck.php new file mode 100644 index 0000000..835648c --- /dev/null +++ b/src/Service/HealthCheck.php @@ -0,0 +1,47 @@ +<?php + +declare(strict_types=1); + +namespace Dbp\Relay\BasePersonConnectorLdapBundle\Service; + +use Dbp\Relay\CoreBundle\HealthCheck\CheckInterface; +use Dbp\Relay\CoreBundle\HealthCheck\CheckOptions; +use Dbp\Relay\CoreBundle\HealthCheck\CheckResult; + +class HealthCheck implements CheckInterface +{ + private $ldap; + + public function __construct(LDAPApi $ldap) + { + $this->ldap = $ldap; + } + + public function getName(): string + { + return 'base-person-connector-ldap'; + } + + private function checkMethod(string $description, callable $func): CheckResult + { + $result = new CheckResult($description); + try { + $func(); + } catch (\Throwable $e) { + $result->set(CheckResult::STATUS_FAILURE, $e->getMessage(), ['exception' => $e]); + + return $result; + } + $result->set(CheckResult::STATUS_SUCCESS); + + return $result; + } + + public function check(CheckOptions $options): array + { + $results = []; + $results[] = $this->checkMethod('Check if we can connect to the LDAP server', [$this->ldap, 'checkConnection']); + + return $results; + } +} diff --git a/src/Service/LDAPApi.php b/src/Service/LDAPApi.php index 30e4bc4..458f8fd 100644 --- a/src/Service/LDAPApi.php +++ b/src/Service/LDAPApi.php @@ -106,6 +106,13 @@ class LDAPApi implements LoggerAwareInterface, ServiceSubscriberInterface $this->providerConfig['port'] = ($encryption === 'start_tls') ? 389 : 636; } + public function checkConnection() + { + $provider = $this->getProvider(); + $builder = $this->getCachedBuilder($provider); + $builder->first(); + } + public function setDeploymentEnvironment(string $env) { $this->deploymentEnv = $env; -- GitLab