Open Monograph Press  3.3.0
ScheduledTask.inc.php
1 <?php
2 
18 import('lib.pkp.classes.scheduledTask.ScheduledTaskHelper');
19 
20 abstract class ScheduledTask {
21 
23  private $_args;
24 
26  private $_processId = null;
27 
29  private $_executionLogFile;
30 
32  private $_helper;
33 
34 
39  function __construct($args = array()) {
40  $this->_args = $args;
41  $this->_processId = uniqid();
42 
43  // Ensure common locale keys are available
44  AppLocale::requireComponents(LOCALE_COMPONENT_PKP_ADMIN, LOCALE_COMPONENT_APP_ADMIN, LOCALE_COMPONENT_PKP_COMMON);
45 
46  // Check the scheduled task execution log folder.
47  import('lib.pkp.classes.file.PrivateFileManager');
48  $fileMgr = new PrivateFileManager();
49 
50  $scheduledTaskFilesPath = realpath($fileMgr->getBasePath()) . DIRECTORY_SEPARATOR . SCHEDULED_TASK_EXECUTION_LOG_DIR;
51  $this->_executionLogFile = $scheduledTaskFilesPath . DIRECTORY_SEPARATOR . str_replace(' ', '', $this->getName()) .
52  '-' . $this->getProcessId() . '-' . date('Ymd') . '.log';
53  if (!$fileMgr->fileExists($scheduledTaskFilesPath, 'dir')) {
54  $success = $fileMgr->mkdirtree($scheduledTaskFilesPath);
55  if (!$success) {
56  // files directory wrong configuration?
57  assert(false);
58  $this->_executionLogFile = null;
59  }
60  }
61  }
62 
63 
64  //
65  // Protected methods.
66  //
71  function getProcessId() {
72  return $this->_processId;
73  }
74 
79  function getHelper() {
80  if (!$this->_helper) $this->_helper = new ScheduledTaskHelper();
81  return $this->_helper;
82  }
83 
89  function getName() {
90  return __('admin.scheduledTask');
91  }
92 
99  function addExecutionLogEntry($message, $type = null) {
100  $logFile = $this->_executionLogFile;
101 
102  if (!$message) return;
103  $date = '[' . Core::getCurrentDate() . '] ';
104 
105  if ($type) {
106  $log = $date . '[' . __($type) . '] ' . $message;
107  } else {
108  $log = $date . $message;
109  }
110 
111  $fp = fopen($logFile, 'ab');
112  if (flock($fp, LOCK_EX)) {
113  fwrite($fp, $log . PHP_EOL);
114  flock($fp, LOCK_UN);
115  } else {
116  fatalError("Couldn't lock the file.");
117  }
118  fclose($fp);
119  }
120 
121 
122  //
123  // Protected abstract methods.
124  //
129  abstract protected function executeActions();
130 
131 
132  //
133  // Public methods.
134  //
142  function execute() {
143  $this->addExecutionLogEntry(Config::getVar('general', 'base_url'));
144  $this->addExecutionLogEntry(__('admin.scheduledTask.startTime'), SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
145 
146  $result = $this->executeActions();
147 
148  $this->addExecutionLogEntry(__('admin.scheduledTask.stopTime'), SCHEDULED_TASK_MESSAGE_TYPE_NOTICE);
149 
150  $helper = $this->getHelper();
151  $helper->notifyExecutionResult($this->_processId, $this->getName(), $result, $this->_executionLogFile);
152 
153  return $result;
154  }
155 }
156 
157 
PrivateFileManager
Class defining operations for private file management.
Definition: PrivateFileManager.inc.php:18
AppLocale\requireComponents
static requireComponents()
Definition: env1/MockAppLocale.inc.php:56
ScheduledTaskHelper
Helper class for common scheduled tasks operations.
Definition: ScheduledTaskHelper.inc.php:22
ScheduledTask\getHelper
getHelper()
Definition: ScheduledTask.inc.php:91
ScheduledTask\execute
execute()
Definition: ScheduledTask.inc.php:154
ScheduledTask\getProcessId
getProcessId()
Definition: ScheduledTask.inc.php:83
Config\getVar
static getVar($section, $key, $default=null)
Definition: Config.inc.php:35
ScheduledTask
Base class for executing scheduled tasks. All scheduled task classes must extend this class and imple...
Definition: ScheduledTask.inc.php:20
ScheduledTask\addExecutionLogEntry
addExecutionLogEntry($message, $type=null)
Definition: ScheduledTask.inc.php:111
ScheduledTask\executeActions
executeActions()
Core\getCurrentDate
static getCurrentDate($ts=null)
Definition: Core.inc.php:63
ScheduledTask\__construct
__construct($args=array())
Definition: ScheduledTask.inc.php:51
fatalError
if(!function_exists('import')) fatalError($reason)
Definition: functions.inc.php:32
ScheduledTask\getName
getName()
Definition: ScheduledTask.inc.php:101