CMS  Version 3.9
scheduled_task_manager.inc
Go to the documentation of this file.
1 <?php
7 Fakoli::using("email");
8 
17 {
18  static $workers = array();
19 
23  function __construct()
24  {
25  ComponentManager::fireEvent("RegisterScheduledTaskWorkers");
26  }
27 
32  static function registerScheduledTaskWorkers()
33  {
34  ScheduledTaskManager::registerWorker("scheduled_task", "Validate Configuration", array(ScheduledTaskManager, testTask));
35  }
36 
37  static function setDefaults()
38  {
39  Settings::setDefaultValue('scheduled_task', 'test_email', '', 'String', 'Comma-separated list of recipients for the test task');
40  }
41 
52  static function registerWorker($component, $task_name, $handler, $hourly = false)
53  {
55 
56  try
57  {
59  }
60  catch(DataNotFoundException $e)
61  {
62  $task = new ScheduledTask();
63  $task->component = $component;
64  $task->task_name = $task_name;
65  $task->active = false;
66  $task->run_every_hour = $hourly;
67  $task->save();
68  }
69  }
70 
71  static function getTabs($key)
72  {
73  $tabs = array("Task Schedule" => "/admin/scheduled_task_form",
74  "Execution Logs" => "/admin/scheduled_task_logs");
75 
76  $qs = ($key) ? "task_id=$key" : "";
77  return new TabBar("tabs", $tabs, $qs);
78  }
79 
83  function getTasks()
84  {
85  return Query::create(ScheduledTask, "ORDER BY component, task_name")->execute();
86  }
87 
93  function executeTask($component, $task_name)
94  {
95  trace("-- Executing Task '{$component}: $task_name'", 3);
96 
98 
99  if (!$task)
100  {
101  // Report an error
102  }
103 
104  $timestamp = now();
105 
106  $worker = ScheduledTaskManager::$workers[$component.':'.$task_name];
107  ob_start();
108  $result = call_user_func($worker);
109  $output = ob_get_contents();
110  ob_end_clean();
111 
112  $log = new ScheduledTaskLogEntry();
113  $log->task_id = $task->task_id;
114  $log->log_date = $timestamp;
115  $log->log = $output;
116  $log->status = $result;
117  $log->save();
118  }
119 
123  function runTasks()
124  {
125  global $config;
126 
128 
129  $currentPeriod = TaskScheduleHelper::calculatePeriod();
130  trace("** Running tasks for period $currentPeriod", 3);
131 
132  $tasks = $this->getTasks();
133  $count = 0;
134 
135  foreach($tasks as $task)
136  {
137  if (!$task->active) continue;
138  if ($task->run_every_hour || strpos($task->schedule, $currentPeriod) !== false)
139  {
140  $this->executeTask($task->component, $task->task_name);
141  ++$count;
142  }
143  }
144 
145  trace("$count ".pluralize("task", $count)." executed", 3);
146 
148  }
149 
154  static function testTask()
155  {
156  global $config;
157 
158  trace("Running Validation Configuration task", 3);
159 
160  $email = Settings::getValue('scheduled_task', 'test_email');
161  if (!$email)
162  {
163  $email = Settings::getValue("settings", "system_notification_email");
164  }
165 
166  $site_email_from = Settings::getValue('email', 'email_from');
167  $site_email_name = Settings::getValue('email', 'email_name');
168 
169  if (!$email) return;
170 
171  trace("Sending confirmation email to {$email}", 3);
172 
173  $mail = new EmailHandler($email, "Scheduled Task Notification from {$config['site_name']}",
174  "This message confirms that the Validate Configuration schedule task worker was triggered at ".now(),
175  $site_email_from,
176  $site_email_name);
177  $mail->send();
178 
179  echo "Confirmation email sent";
180 
181  return "Success";
182  }
183 
184  static function upgradeComponent($version)
185  {
187  $mgr->upgrade($version);
188  }
189 }
190 ?>
$tabs
$handler
Definition: event_form.inc:62
$component
Definition: help.inc:38
$tasks
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.
static coreTraceLevel()
Change to the configured trace level for Fakoli core code.
Definition: core.inc:1346
static scheduledTaskTraceLevel()
Changed to the configured trace level for scheduled task worker code.
Definition: core.inc:1354
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
static getInstance($component, $task_name)
Manages scheduled task registration and execution.
executeTask($component, $task_name)
Executes the specified scheduled task.
static registerScheduledTaskWorkers()
Registers a simple task that sends an email when triggered.
static testTask()
Provides the implementation of the test task, that can be used to validate the scheduled task server ...
runTasks()
Runs all the tasks that are scheduled to be run for the current period (day and hour).
getTasks()
Retrieves all the registered scheduled tasks.
__construct()
Creates a ScheduledTaskManager.
static registerWorker($component, $task_name, $handler, $hourly=false)
Registers a Scheduled Task worker method.
static upgradeComponent($version)
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
static setDefaultValue($component, $name, $value, $field_type="String", $annotation="", $category="", $options="", $weight=0)
Sets the default value of the given component setting.
Definition: settings.inc:174
static calculatePeriod($datetime=null)
$output
Definition: generate.inc:9
global $config
Definition: import.inc:4
$result
$timestamp
Definition: rss.inc:63
$mail
Definition: test.inc:5