CMS  Version 3.9
ScheduledTaskManager Class Reference

Manages scheduled task registration and execution. More...

Public Member Functions

 __construct ()
 Creates a ScheduledTaskManager. More...
 
 getTasks ()
 Retrieves all the registered scheduled tasks. More...
 
 executeTask ($component, $task_name)
 Executes the specified scheduled task. More...
 
 runTasks ()
 Runs all the tasks that are scheduled to be run for the current period (day and hour). More...
 

Static Public Member Functions

static registerScheduledTaskWorkers ()
 Registers a simple task that sends an email when triggered. More...
 
static setDefaults ()
 
static registerWorker ($component, $task_name, $handler, $hourly=false)
 Registers a Scheduled Task worker method. More...
 
static getTabs ($key)
 
static testTask ()
 Provides the implementation of the test task, that can be used to validate the scheduled task server configuration (cron job, etc) More...
 
static upgradeComponent ($version)
 

Static Public Attributes

static $workers = array()
 

Detailed Description

Manages scheduled task registration and execution.

Scheduled tasks are tasks that can be run periodically without user initiation, triggered by a cron job or Windows scheduled task. Components can register tasks by subscribing to the RegisterScheduledTaskWorkers event. Registered tasks can then be configured for scheduled execution via the admin screens.

Definition at line 16 of file scheduled_task_manager.inc.

Constructor & Destructor Documentation

◆ __construct()

ScheduledTaskManager::__construct ( )

Creates a ScheduledTaskManager.

Definition at line 23 of file scheduled_task_manager.inc.

24  {
25  ComponentManager::fireEvent("RegisterScheduledTaskWorkers");
26  }
static fireEvent($event, $parameter=null, $mustBeConsumed=false)
Fire an event to all subscribers as detailed in their manifests.

Member Function Documentation

◆ executeTask()

ScheduledTaskManager::executeTask (   $component,
  $task_name 
)

Executes the specified scheduled task.

Parameters
string$componentthe name of the component that owns the task
string$task_namethe name of the task to be executed

Definition at line 93 of file scheduled_task_manager.inc.

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  }
$component
Definition: help.inc:38
static getInstance($component, $task_name)
$output
Definition: generate.inc:9
$result
$timestamp
Definition: rss.inc:63

◆ getTabs()

static ScheduledTaskManager::getTabs (   $key)
static

Definition at line 71 of file scheduled_task_manager.inc.

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  }
$tabs

◆ getTasks()

ScheduledTaskManager::getTasks ( )

Retrieves all the registered scheduled tasks.

Definition at line 83 of file scheduled_task_manager.inc.

84  {
85  return Query::create(ScheduledTask, "ORDER BY component, task_name")->execute();
86  }

◆ registerScheduledTaskWorkers()

static ScheduledTaskManager::registerScheduledTaskWorkers ( )
static

Registers a simple task that sends an email when triggered.

This can be used to validate the configuration of the cron job / scheduled task environment.

Definition at line 32 of file scheduled_task_manager.inc.

33  {
34  ScheduledTaskManager::registerWorker("scheduled_task", "Validate Configuration", array(ScheduledTaskManager, testTask));
35  }
Manages scheduled task registration and execution.
static testTask()
Provides the implementation of the test task, that can be used to validate the scheduled task server ...
static registerWorker($component, $task_name, $handler, $hourly=false)
Registers a Scheduled Task worker method.

◆ registerWorker()

static ScheduledTaskManager::registerWorker (   $component,
  $task_name,
  $handler,
  $hourly = false 
)
static

Registers a Scheduled Task worker method.

This method should be called from within the component's RegisterScheduledTaskWorkers event handler. Components are free to register as many scheduled task workers as they need.

Parameters
string$componentthe name of the component that owns the worker
string$task_namethe display name of the task
callback$handlerthe handler method to call to execute the task
boolean$hourlytrue if this task should always be executed every hour, false if the task should be explicitly scheduled by the user

Definition at line 52 of file scheduled_task_manager.inc.

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  }
$handler
Definition: event_form.inc:62

◆ runTasks()

ScheduledTaskManager::runTasks ( )

Runs all the tasks that are scheduled to be run for the current period (day and hour).

Definition at line 123 of file scheduled_task_manager.inc.

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  }
$tasks
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
executeTask($component, $task_name)
Executes the specified scheduled task.
getTasks()
Retrieves all the registered scheduled tasks.
static calculatePeriod($datetime=null)
global $config
Definition: import.inc:4

◆ setDefaults()

static ScheduledTaskManager::setDefaults ( )
static

Definition at line 37 of file scheduled_task_manager.inc.

38  {
39  Settings::setDefaultValue('scheduled_task', 'test_email', '', 'String', 'Comma-separated list of recipients for the test task');
40  }
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

◆ testTask()

static ScheduledTaskManager::testTask ( )
static

Provides the implementation of the test task, that can be used to validate the scheduled task server configuration (cron job, etc)

Definition at line 154 of file scheduled_task_manager.inc.

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  }
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
$mail
Definition: test.inc:5

◆ upgradeComponent()

static ScheduledTaskManager::upgradeComponent (   $version)
static

Definition at line 184 of file scheduled_task_manager.inc.

185  {
187  $mgr->upgrade($version);
188  }

Member Data Documentation

◆ $workers

ScheduledTaskManager::$workers = array()
static

Definition at line 18 of file scheduled_task_manager.inc.


The documentation for this class was generated from the following file: