before = $data['EventManager.beforeRegularEvents']; $this->after = $data['EventManager.afterRegularEvents']; } /** * Gets object data for caching * * @return Array * @access public */ public function getToCache() { return Array ( 'EventManager.beforeRegularEvents' => $this->before, 'EventManager.afterRegularEvents' => $this->after, ); } /** * Returns information about registered scheduled tasks * * @param bool $from_cache * @return Array * @access public */ public function getAll($from_cache = false) { static $scheduled_tasks = null; if ($from_cache) { return Array ( reBEFORE => $this->before, reAFTER => $this->after, ); } if ( !isset($scheduled_tasks) ) { $timeout_clause = 'LastRunStatus = ' . ScheduledTask::LAST_RUN_RUNNING . ' AND Timeout > 0 AND ' . adodb_mktime() . ' - LastRunOn > Timeout'; $sql = 'SELECT * FROM ' . $this->Application->getUnitOption('scheduled-task', 'TableName') . ' WHERE (Status = ' . STATUS_ACTIVE . ') AND ((LastRunStatus != ' . ScheduledTask::LAST_RUN_RUNNING . ') OR (' . $timeout_clause . '))'; $all_scheduled_tasks = $this->Conn->Query($sql); $scheduled_tasks = Array ( reBEFORE => Array (), reAFTER => Array (), ); foreach ($all_scheduled_tasks as $scheduled_task_data) { $scheduled_tasks[ $scheduled_task_data['RunMode'] ][ $scheduled_task_data['Name'] ] = Array ( 'EventName' => $scheduled_task_data['Event'], 'RunInterval' => (int)$scheduled_task_data['RunInterval'], 'LastRunOn' => (int)$scheduled_task_data['LastRunOn'], 'NextRunOn' => (int)$scheduled_task_data['NextRunOn'], 'Status' => $scheduled_task_data['Status'], 'LastRunStatus' => $scheduled_task_data['LastRunStatus'], 'SiteDomainLimitation' => $scheduled_task_data['SiteDomainLimitation'], ); } } return $scheduled_tasks; } /** * Returns scheduled tasks by type * * @param int $type * @return Array * @access protected */ protected function &getByType($type) { if ($type == reBEFORE) { return $this->before; } return $this->after; } /** * Add new scheduled task * * @param string $short_name name to be used to store last maintenance run info * @param string $event_name * @param int $run_interval run interval in seconds * @param int $type before or after scheduled task * @param int $status * @access public */ public function add($short_name, $event_name, $run_interval, $type = reBEFORE, $status = STATUS_ACTIVE) { $scheduled_tasks =& $this->getByType($type); $scheduled_tasks[$short_name] = Array ( 'EventName' => $event_name, 'RunInterval' => $run_interval, 'Status' => $status ); } /** * Run registered scheduled tasks with specified event type * * @param int $event_type * @param bool $from_cron * @access public */ public function runAll($event_type = reBEFORE, $from_cron = false) { if ( defined('IS_INSTALL') ) { return ; } if ( !$from_cron && $this->Application->ConfigValue('RunScheduledTasksFromCron') ) { // if scheduled tasks are set to run from cron return ; } $scheduled_tasks = $this->getAll(); $events_source = $scheduled_tasks[$event_type]; $user_id = $this->Application->RecallVar('user_id'); $this->Application->StoreVar('user_id', USER_ROOT, true); // to prevent permission checking inside events, true for optional storage $site_helper =& $this->Application->recallObject('SiteHelper'); /* @var $site_helper SiteHelper */ $site_domain_id = $site_helper->getDomainByName('DomainName', DOMAIN); foreach ($events_source as $short_name => $event_data) { if ( $site_domain_id && $event_data['SiteDomainLimitation'] != '' ) { $site_domains = explode('|', substr($event_data['SiteDomainLimitation'], 1, -1)); if ( !in_array($site_domain_id, $site_domains) ) { // scheduled task isn't allowed on this site domain continue; } } // remember LastTimeoutOn only for events that are still running and will be reset if ( $event_data['LastRunStatus'] == ScheduledTask::LAST_RUN_RUNNING ) { $this->update($short_name, Array ('LastTimeoutOn' => adodb_mktime())); } $next_run = $event_data['NextRunOn']; if ($next_run && ($next_run > adodb_mktime())) { continue; } $event_data['Name'] = $short_name; $this->run($event_data); } $this->Application->StoreVar('user_id', $user_id, $user_id == USER_GUEST); } /** * Runs scheduled task based on given data * * @param Array $scheduled_task_data * @return bool * @access public */ public function run($scheduled_task_data) { $event = new kEvent($scheduled_task_data['EventName']); if ( !$this->Application->prefixRegistred($event->Prefix) ) { // don't process scheduled tasks, left from disabled modules return false; } $start_time = adodb_mktime(); // remember, when scheduled task execution started $fields_hash = Array ( 'LastRunOn' => $start_time, 'LastRunStatus' => ScheduledTask::LAST_RUN_RUNNING, 'NextRunOn' => $start_time + $scheduled_task_data['RunInterval'], ); $this->update($scheduled_task_data['Name'], $fields_hash); $event->redirect = false; $this->Application->HandleEvent($event); $now = adodb_mktime(); $next_run = $scheduled_task_data['RunInterval'] ? $start_time + $scheduled_task_data['RunInterval'] : $now; while ($next_run < $now) { // in case event execution took longer, then RunInterval (don't use <=, because RunInterval can be 0) $next_run += $scheduled_task_data['RunInterval']; } // remember, when scheduled task execution ended $fields_hash = Array ( 'NextRunOn' => $next_run, 'RunTime' => round(($now - $start_time) / 60), 'LastRunStatus' => $event->status == kEvent::erSUCCESS ? ScheduledTask::LAST_RUN_SUCCEEDED : ScheduledTask::LAST_RUN_FAILED, ); $this->update($scheduled_task_data['Name'], $fields_hash); return true; } /** * Updates scheduled task record with latest changes about it's invocation progress * * @param string $scheduled_task_name * @param Array $fields_hash * @return void * @access protected */ protected function update($scheduled_task_name, $fields_hash) { $this->Conn->doUpdate( $fields_hash, $this->Application->getUnitOption('scheduled-task', 'TableName'), 'Name = ' . $this->Conn->qstr($scheduled_task_name) ); } }