A simple PHP Cronitor.io Monitor Caller Client
Clone or download
nathanielks Merge pull request #12 from NauxLiu/patch-1
Add exception class name to failed message
Latest commit 3862961 Jun 26, 2017

README.md

Unofficial PHP Cronitor.io Monitor Caller Client

Build Status Scrutinizer Code Quality Code Coverage StyleCI

Table of Contents

Installation

Using composer:

$ composer require nathanielks/cronitor-io-php

Requirements

The following versions of PHP are supported by this version.

  • PHP 5.4
  • PHP 5.5
  • PHP 5.6
  • PHP 7.0

Examples

You can use Cronitor\Client by itself:

$monitorId = 'id of the monitor found on the cronitor dashboard';
$client = new Cronitor\Client($monitorId);
$client->run();
// do some stuff...
$client->complete();

You can also use the included cronitorMonitorTask function. It will wrap the provided function in run()/complete() and catch and then rethrow any Exceptions thrown in the process. By default, it will send a message to Cronitor's /fail endpoint with the message generated by the Exception.

$monitorId = 'id of the monitor found on the cronitor dashboard';
$client = new Cronitor\Client($monitorId);

try{
  cronitorMonitorTask(
    $client,
    function(){
      // do some stuff...
    }
  );
} catch (Exception $e){
  // If there are any errors thrown, cronitorMonitorTask will catch the
  // exception and use the error message as the message to send to Cronitor's
  // /fail endpoint. It will then rethrow the exception to be caught and
  // processed here

  // Do some other stuff, like logging perhaps?
}

You can also provide a function to return a message to send to Cronitor:

$monitorId = 'id of the monitor found on the cronitor dashboard';
$client = new Cronitor\Client($monitorId);

try{
  cronitorMonitorTask(
    $client,
    function(){
      // do some stuff...
    },
    function($exception, $client){
      return array(
        'msg' => 'This will be the error message sent to Cronitor.io'
      );
    }
  );
} catch (Exception $e){
  // Do some other stuff, like logging perhaps?
}

The expected return value is an array with msg and pause as keys. msg is a string that will be url-encoded and pause is an integer representing the number of hours to pause alerts on this particular monitor.

Interested in returning something from the closure? You sure can!

$monitorId = 'id of the monitor found on the cronitor dashboard';
$client = new Cronitor\Client($monitorId);

try{
  $someValue = cronitorMonitorTask(
    $client,
    function(){
      // do some stuff...
      return 'heck yeah you can!';
    }
  );
} catch (Exception $e){
  // Do some other stuff, like logging perhaps?
}

Enjoy!