Browse Source

added cron to healthChecks plugin

CauseFX 4 years ago
parent
commit
0c55b8d204

+ 36 - 0
api/plugins/healthChecks/advancedCron.php

@@ -0,0 +1,36 @@
+<?php
+/*
+ * Simple Cron job
+ */
+/* COMMENTED OUT AS THIS IS AN EXAMPLE
+// Initiate Class
+$plugin = new HealthChecks();
+// Set logger to CRON Channel
+$plugin->setLoggerChannel('CRON');
+// Check to see if plugin cron job is enabled and check if schedule is set in config value
+if ($plugin->config['HEALTHCHECKS-cron-run-enabled'] && $plugin->config['HEALTHCHECKS-cron-run-schedule'] !== '') {
+	$plugin->logger->debug('Starting cron job for function: HealthChecks run', ['cronJob' => 'HealthChecks']);
+	$plugin->logger->debug('Validating cron job schedule', ['schedule' => $plugin->config['HEALTHCHECKS-cron-run-schedule']]);
+	// Validate if schedule is in correct cron format
+	try {
+		$schedule = new Cron\CronExpression($plugin->config['HEALTHCHECKS-cron-run-schedule']);
+		$plugin->logger->debug('Cron schedule has passed validation', ['schedule' => $plugin->config['HEALTHCHECKS-cron-run-schedule']]);
+	} catch (InvalidArgumentException $e) {
+		$plugin->logger->critical($e->getMessage());
+	}
+	// Setup job for cron
+	$scheduler->call(
+		function ($plugin) {
+			$plugin->logger->debug('Starting cron job for function: HealthChecks run');
+			return $plugin->_healthCheckPluginRun();
+		}, [$plugin])
+		->then(function ($output) use ($plugin) {
+			$plugin->logger->debug('Completed cron job', [
+				'output' => $output,
+			]);
+		})
+		->at($plugin->config['HEALTHCHECKS-cron-run-schedule']);
+} else {
+	$plugin->logger->debug('Cron job is not enabled or is set up incorrectly', ['cronJob' => 'HealthChecks']);
+}
+*/

+ 2 - 0
api/plugins/healthChecks/config.php

@@ -3,6 +3,8 @@ return array(
 	'HEALTHCHECKS-enabled' => false,
 	'HEALTHCHECKS-401-enabled' => false,
 	'HEALTHCHECKS-403-enabled' => false,
+	'HEALTHCHECKS-cron-run-enabled' => false,
+	'HEALTHCHECKS-cron-run-schedule' => '*/5 * * * *',
 	'HEALTHCHECKS-Auth-include' => '1',
 	'HEALTHCHECKS-option2-include' => '',
 	'HEALTHCHECKS-all-items' => '',

+ 10 - 0
api/plugins/healthChecks/cron.php

@@ -0,0 +1,10 @@
+<?php
+/*
+ * Simple Cron job
+ */
+$GLOBALS['cron'][] = [
+	'class' => 'HealthChecks', // Class name of plugin (case-sensitive)
+	'enabled' => 'HEALTHCHECKS-cron-run-enabled', // Config item for job enable
+	'schedule' => 'HEALTHCHECKS-cron-run-schedule', // Config item for job schedule
+	'function' => '_healthCheckPluginRun', // Function to run during job
+];

+ 44 - 45
api/plugins/healthChecks/plugin.php

@@ -21,44 +21,41 @@ class HealthChecks extends Organizr
 	public function _healthCheckPluginGetSettings()
 	{
 		return array(
-			'FYI' => array(
+			'Cron' => array(
 				array(
 					'type' => 'html',
 					'label' => '',
 					'override' => 12,
 					'html' => '
 						<div class="row">
-						    <div class="col-lg-12">
-						        <div class="panel panel-info">
-						            <div class="panel-heading">
-						                <span lang="en">ATTENTION</span>
-						            </div>
-						            <div class="panel-wrapper collapse in" aria-expanded="true">
-						                <div class="panel-body">
-						                	<h4 lang="en">Once this plugin is setup, you will need to setup a CRON job</h4>
-						                    <br/>
-						                    <span>
-						                    	<h4><b lang="en">CRON Job URL</b></h4>
-						                    	<code>' . $this->getServerPath() . 'api/v2/plugins/healthchecks/run</code><br/>
-						                    	<h5><b lang="en">Frequency</b></h5>
-						                    	<span lang="en">As often as you like - i.e. every 1 minute</span>
-						                    </span>
-						                </div>
-						            </div>
-						        </div>
-						    </div>
+							<div class="col-lg-12">
+								<div class="panel panel-info">
+									<div class="panel-heading">
+										<span lang="en">ATTENTION</span>
+									</div>
+									<div class="panel-wrapper collapse in" aria-expanded="true">
+										<div class="panel-body">
+											<h4 lang="en">Once this plugin is setup, you will need to setup a CRON job</h4>
+											<br/>
+											<span>
+												<h4><b lang="en">CRON Job URL</b></h4>
+												<code>' . $this->getServerPath() . 'api/v2/plugins/healthchecks/run</code><br/>
+												<h5><b lang="en">Schedule</b></h5>
+												<span lang="en">As often as you like - i.e. every 1 minute</span>
+											</span>
+										</div>
+									</div>
+								</div>
+							</div>
 						</div>
 						'
-				)
+				),
+				$this->settingsOption('blank'),
+				$this->settingsOption('enable', 'HEALTHCHECKS-cron-run-enabled'),
+				$this->settingsOption('cron', 'HEALTHCHECKS-cron-run-schedule')
 			),
 			'Options' => array(
-				array(
-					'type' => 'select',
-					'name' => 'HEALTHCHECKS-Auth-include',
-					'label' => 'Minimum Authentication',
-					'value' => $this->config['HEALTHCHECKS-Auth-include'],
-					'options' => $this->groupSelect()
-				),
+				$this->settingsOption('auth', 'HEALTHCHECKS-Auth-include'),
 				array(
 					'type' => 'input',
 					'name' => 'HEALTHCHECKS-PingURL',
@@ -101,23 +98,23 @@ class HealthChecks extends Organizr
 					'override' => 12,
 					'html' => '
 						<div class="row">
-						    <div class="col-lg-12">
-						        <div class="panel panel-danger">
-						            <div class="panel-heading">
-						                <span lang="en">ATTENTION</span>
-						            </div>
-						            <div class="panel-wrapper collapse in" aria-expanded="true">
-						                <div class="panel-body">
-						                	<h4 lang="en">Please use a Full Access Token</h4>
-						                    <br/>
-						                    <div>
-						                    	<p lang="en">Do not use a Read-Only Token as that will not give a correct UUID for sending the results to HealthChecks.io</p>
-						                    	<p lang="en">Make sure to save before using the import button on Services tab</p>
-						                    </div>
-						                </div>
-						            </div>
-						        </div>
-						    </div>
+							<div class="col-lg-12">
+								<div class="panel panel-danger">
+									<div class="panel-heading">
+										<span lang="en">ATTENTION</span>
+									</div>
+									<div class="panel-wrapper collapse in" aria-expanded="true">
+										<div class="panel-body">
+											<h4 lang="en">Please use a Full Access Token</h4>
+											<br/>
+											<div>
+												<p lang="en">Do not use a Read-Only Token as that will not give a correct UUID for sending the results to HealthChecks.io</p>
+												<p lang="en">Make sure to save before using the import button on Services tab</p>
+											</div>
+										</div>
+									</div>
+								</div>
+							</div>
 						</div>
 						'
 				)
@@ -264,8 +261,10 @@ class HealthChecks extends Organizr
 				$this->_healthCheckPluginUUID($v['UUID'], $pass);
 			}
 			$this->setAPIResponse('success', null, 200, $allItems);
+			return $allItems;
 		} else {
 			$this->setAPIResponse('error', 'User does not have access', 401);
 		}
+		return false;
 	}
 }