Cronitor is a service for heartbeat-style monitoring of anything that can send an HTTP request. It's particularly well suited for monitoring cron jobs, Laravel scheduled tasks, or any other background task.
This library provides a simple abstraction for the pinging of a Cronitor monitor. For a better understanding of the API this library talks to, please see our Ping API docs. For a general introduction to Cronitor please read How Cronitor Works.
Using composer:
$ composer require cronitor/cronitor-phpThe following versions of PHP are supported by this version.
- PHP 5.4
- PHP 5.5
- PHP 5.6
- PHP 7.0
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!

