<?php require_once dirname(__FILE__) . '/../lib/tymio/common.php'; // Run timed plugins if either their last execution time is further in the past // than their specified execution interval or where the time of day minus the // start time offset is a divisor of the interval. $now = time(); $timeOfDay = $now - strtotime('0:00', $now); $query = new PluginQuery(); $plugins = $query->filterByActive(0, Criteria::NOT_EQUAL)->filterByEntity(PluginPeer::ENTITY_SYSTEM)->filterByEvent(PluginPeer::EVENT_TIMED)->add($query->getNewCriterion(PluginPeer::LAST_EXECUTION_TIME, '(' . $now . ' - ' . PluginPeer::LAST_EXECUTION_TIME . ' > ' . PluginPeer::INTERVAL . ')', Criteria::CUSTOM)->addOr($query->getNewCriterion(PluginPeer::START, '((86400 + ' . $timeOfDay . ' - ' . PluginPeer::START . ') % 86400 % ' . PluginPeer::INTERVAL . ' BETWEEN 0 AND 59)', Criteria::CUSTOM)->addAnd($query->getNewCriterion(PluginPeer::LAST_EXECUTION_TIME, '(' . $now . ' - ' . PluginPeer::LAST_EXECUTION_TIME . ' > 60)', Criteria::CUSTOM))))->addAscendingOrderByColumn(PluginPeer::LAST_EXECUTION_TIME)->addAscendingOrderByColumn(PluginPeer::PRIORITY)->find(); $parameters = PluginPeer::buildParameters(PluginPeer::ENTITY_SYSTEM, PluginPeer::EVENT_TIMED); foreach ($plugins as $plugin) { try { error_log('Plugin #' . $plugin->getId() . ' ' . $plugin->getIdentifier() . ', last exec time ' . ($now - $plugin->getLastExecutionTime()) . ', time of day ' . $timeOfDay . ', interval ' . $plugin->getInterval() . ' => ' . (86400 + $timeOfDay - $plugin->getStart()) % 86400 % $plugin->getInterval()); $sandbox = $plugin->execute(null, $parameters); $plugin->setLastExecutionTime($now)->save(); $exception = $sandbox->getException(); if ($exception !== null) { throw $exception; } } catch (Exception $e) { echo 'Plugin #' . $plugin->getId() . ' ' . $plugin->getIdentifier() . ': ' . $e->getMessage() . "\n"; } }
/** * Executes an iXML script. * * @param string $id The plugin ID or the code to execute. * @param array $parametersJson Optional. Additonal parameters. Default is NULL. * @param bool $debug Optional. If TRUE, the plugin's inputs and outputs * will be encoded in a JSON object. If FALSE, the plugin's raw output * will be printed, and the return value will be {@link RESTvoidResult}. * Default is FALSE. * @return RESTvoidResult|array */ public function do_execute($id, $parametersJson = null, $debug = false) { $user = $this->requireUser(); $plugin = $this->getPluginById($id); if (!$this->executionAllowed($user, $plugin)) { throw new Exception('Non-administrative user "' . $user->getFQN() . '" cannot execute plugin directly.'); } $data = (empty($_POST) or (string) $parametersJson === '') ? array(null) : json_decode('[' . $parametersJson . ']', true); if (!is_array($data)) { throw new Exception('Invalid parameters JSON string.'); } $parameters = PluginPeer::buildParameters($plugin->getEntity(), $plugin->getEvent(), $user, $data[0]); $sandbox = $plugin->execute($user, $parameters); $exception = $sandbox->getException(); if ($debug) { return array('data' => $sandbox->getGlobals(), 'output' => $sandbox->getOutput(), 'debug' => $sandbox->getDebugOutput(), 'log' => $sandbox->getDebugLog(), 'calls' => $sandbox->getCallStack(), 'error' => $exception === null ? null : $exception->getMessage()); } elseif ($exception === null) { echo $sandbox->getOutput(); return new RESTvoidResult(); } else { header('Content-Type: text/plain'); echo $exception->getMessage() . "\n\nCall stack:\n" . implode("\n", $sandbox->getCallStack()); return new RESTvoidResult(); } }