Skip to content

Application health checks

Johannes Ahrndt edited this page Oct 7, 2023 · 13 revisions

The Oh Dear plugin comes with a couple of built-in health checks that are ready to use. Make sure you configured the health checks in the settings before you proceed. Some of the built-in checks are:

  • Used Disk Space
  • Available Updates
  • Failed Queue Jobs
  • Queue Health
  • Redis Connection
  • Server Requirements
  • Environment Check
  • DevMode Check
  • SMTP Connection Settings (Coming soon)

💡 Pro tip: You can view the results of the currently configured health checks on the App-Health page in the Control Panel Utilities.

Enabling built-in checks

To make use of the checks, you first have to grab the plugin config template and put it into you config/ folder. You may use this command:

cp -n vendor/webhubworks/craft-ohdear/src/config.php config/ohdear.php

In the template you'll find a usage example of a check. Make sure to uncomment the 4 lines regarding the health checks:

use webhubworks\ohdear\health\checks\Check;

return [
//    'apiToken' => null,
//    'siteId' => null,
//    'healthCheckSecret' => null,
    'healthChecks' => [
        Check::availableUpdates()->warnWhenTotalAvailableUpdatesIsAtLeast(3),
    ],
];

Most checks are configurable in some way, but all options have sensible defaults. Try to use your IDE auto-completion to discover other checks and their config options. For convenience, all checks are discoverable through the Check class.

Setting up the Queue Health Check requires an additional step.

Custom checks

The plugin provides an API to create custom checks.

You may create your first own check in 4 steps.

A check is a single class that extends the plugin's Check class. You can put your custom checks anywhere you want but let's assume you put them into config/ohdear/.

  1. First, create that folder
mkdir -p config/ohdear
  1. You have to tell Composer to auto-load that location by putting the following into your composer.json. You might want to run composer dump-autoload after that.
  "autoload": {
    "psr-4": {
      "ohdear\\": "config/ohdear/"
    }
  },
  1. Create a PHP class with the following content in that folder. You are free to return any instance of CheckResult from the run() method. You might refer to the implementation of the built-in checks for further guidance.

Use this code template as a start:

View code
<?php

namespace ohdear;

use OhDear\HealthCheckResults\CheckResult;
use webhubworks\ohdear\health\checks\Check;

class MyCustomCheck extends Check
{
    public function run(): CheckResult
    {
        return new CheckResult(
            name: 'Custom check',
            label: 'My very own custom check',
            notificationMessage: 'Some longer message that describes the status',
            shortSummary: 'terse',
            status: CheckResult::STATUS_OK,
//            status: CheckResult::STATUS_WARNING,
//            status: CheckResult::STATUS_FAILED,
//            status: CheckResult::STATUS_CRASHED,
//            status: CheckResult::STATUS_SKIPPED,
            meta: [
                'key1' => 'meta entries are shown',
                'key2' => 'in the check details',
            ]
        );
    }
}
  1. Finally, add that check to the plugin config with ohdear\MyCustomCheck::new().