<?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"; } }
/** * Lists the plugins * * @param string $search Search query * @param bool $showDeleted Show deleted domains * @param string $strOrderby OrderBy Column * @param string $strOrderMode Order mode (asc or desc) * @return array */ public function do_list($search = null, $showInactive = false, $entityName = null, $eventName = null, $strOrderby = 'Name', $strOrderMode = 'asc') { $user = $this->requireUser(); $account = $user->getAccount(); $query = new PluginQuery(); $query->filterByAccount($account); if ($strOrderMode !== 'asc') { $strOrderMode = 'desc'; } switch ($strOrderby) { case 'Identifier': break; case 'Entity': $query->orderByEntity($strOrderMode); break; case 'Event': $query->orderByEvent($strOrderMode); break; case 'Priority': $query->orderByPriority($strOrderMode); break; default: // Name $query->orderByName($strOrderMode); break; } $query->orderByIdentifier($strOrderMode); if ((string) $search !== '') { $query->filterByName('%' . $search . '%', Criteria::LIKE)->_or()->filterByIdentifier('%' . $search . '%', Criteria::LIKE); } if (!$showInactive) { $query->filterByActive(0, Criteria::NOT_EQUAL); } if ((string) $entityName !== '') { $query->filterByEntity($entityName); } if ((string) $eventName !== '') { $query->filterByEvent($eventName); } $result = array(); foreach ($query->find() as $plugin) { /* @var $plugin Plugin */ $result[] = array('Id' => $plugin->getId(), 'AccountId' => $plugin->getAccountId(), 'Entity' => $plugin->getEntity(), 'Event' => $plugin->getEvent(), 'Priority' => $plugin->getPriority(), 'Identifier' => $plugin->getIdentifier(), 'Name' => $plugin->getName(), 'Active' => $plugin->getActive()); } return $result; }