FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Worker.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 class Worker extends Component{
6 
7  public $classNameAlias = []; //!< Job class name aliasses (key = alias, value = class name).
8  public $timeLimit = 60; //!< Default time limit for a job (seconds).
9  public $breakCount = 100; //!< Halt the processig of job after this amount (even when there are more jobs scheduled).
10  public $breakTime = 300; //!< Halt the processig of job after this time (seconds; even when there are more jobs scheduled).
11  public $defaultOverduePeriod = '+1 hour'; //!< Default overdue period for jobs that do not have a rescheduling interval.
12  public $logTimerPrio = null; //!< Log the begin and end times of a job with this (custom) prio.
13 
14  /**
15  * Translate a job class name.
16  * @param string $class_name Class name for the job, or an alias.
17  * @return string Class name.
18  */
19  protected function transClassName($class_name){
20  return $this->classNameAlias[$class_name] ?? $class_name;
21  }
22  /**
23  * Add a job to the queue.
24  * @param string $class_name Class name for the job.
25  * @param array $config Configuration options (key-value pairs) for the job.
26  * @param int $time Time to run the job (null for as soon as possible).
27  * @param string $interval String that is understood by PHP's strtotime() to reschedule the job after it is finished.
28  * @return string Job ID when successful.
29  */
30  public function schedule($class_name,$config = null,$time = null,$interval = null){
31  return false;
32  }
33  /**
34  * Delete a scheduled job from the queue.
35  * @param string $id Job ID.
36  * @return bool True when successful.
37  */
38  public function delete($id){
39  return false;
40  }
41  /**
42  * Create a job object.
43  * @param string $class_name Class name for the job.
44  * @param string $id Unique ID for the job.
45  * @param array $config Configuration options (key-value pairs) for the job.
46  * @param int $time Time to run the job (null for as soon as possible).
47  * @param string $interval String that is understood by PHP's strtotime() to reschedule the job after it is finished.
48  * @return \\Rsi\\Fred\\Worker\\Job
49  */
50  protected function create($class_name,$id,$config = null,$time = null,$interval = null){
51  return new $class_name($this,$id,$config,$time,$interval);
52  }
53  /**
54  * Run a job instantly.
55  * @param string $class_name Class name for the job.
56  * @param array $config Configuration options (key-value pairs) for the job.
57  */
58  public function instant($class_name,$config = null){
59  $this->create($class_name,null,$config)->execute();
60  }
61  /**
62  * Next scheduled job to execute.
63  * @param string $id Specific job ID to execute (whether scheduled time has come or not).
64  * @return \\Rsi\\Fred\\Worker\\Job (empty if there is no job to be executed).
65  */
66  protected function next($id = null){
67  return null;
68  }
69  /**
70  * Lock a job.
71  * @param \\Rsi\\Fred\\Worker\\Job $job Job to lock.
72  * @return bool True if the job has been locked.
73  */
74  protected function lock($job){
75  return false;
76  }
77  /**
78  * Unlock a job.
79  * @param \\Rsi\\Fred\\Worker\\Job $job Job to unlock.
80  * @return bool True if the job has been unlocked.
81  */
82  protected function unlock($job){
83  return true;
84  }
85  /**
86  * Reschedule a job.
87  * @param \\Rsi\\Fred\\Worker\\Job $job Job to reschedule.
88  * @param int $time Time to reschedule at (null for as soon as possible).
89  * @return bool True if the job has been rescheduled.
90  */
91  protected function reschedule($job,$time = null){
92  return false;
93  }
94  /**
95  * Mark a job as done.
96  * @param \\Rsi\\Fred\\Worker\\Job $job Job.
97  */
98  protected function ready($job){
99  $this->delete($job->id);
100  }
101  /**
102  * Overdue jobs.
103  * Jobs that have been locked longer than twice their interval or the default overdue interval.
104  * @return array Of \\Rsi\\Fred\\Worker\\Job items.
105  */
106  protected function overdue(){
107  return [];
108  }
109  /**
110  * Execute the worker / process the scheduled jobs.
111  * Call his function as often as possible.
112  * @param string $id Specific job ID to execute (whether scheduled time has come or not).
113  */
114  public function execute($id = null){
115  $log = $this->component('log');
116  $count = 0;
117  $start = time();
118  while(($job = $this->next($id)) && $this->lock($job)) try{
119  $this->_fred->timeLimit = $this->timeLimit;
120  $timer_id = $this->logTimerPrio ? $log->start($this->logTimerPrio,"Executing job '{$job->id}'",__FILE__,__LINE__) : null;
121  $job->execute();
122  if($timer_id) $log->stop($timer_id);
123  if($job->interval){
124  $diff = ($time = $job->nextTime) - time();
125  if(!$time) $log->alert("Invalid interval for job '{$job->id}'");
126  elseif($diff < 0) $log->warning("Job '{$job->id}' rescheduled in past",['diff' => $diff]);
127  $this->reschedule($job,$time);
128  }
129  else $this->ready($job);
130  $this->unlock($job);
131  if((++$count >= $this->breakCount) || (time() - $start >= $this->breakTime)) break;
132  }
133  catch(\Exception $e){
134  $log->critical($e);
135  }
136  foreach($this->overdue() as $job)
137  $log->critical("Job '{$job->id}' is overdue",['time' => $job->time,'interval' => $job->interval]);
138  }
139 
140 }
instant($class_name, $config=null)
Run a job instantly.
Definition: Worker.php:58
$defaultOverduePeriod
Default overdue period for jobs that do not have a rescheduling interval.
Definition: Worker.php:11
overdue()
Overdue jobs.
Definition: Worker.php:106
lock($job)
Lock a job.
Definition: Worker.php:74
schedule($class_name, $config=null, $time=null, $interval=null)
Add a job to the queue.
Definition: Worker.php:30
$logTimerPrio
Log the begin and end times of a job with this (custom) prio.
Definition: Worker.php:12
ready($job)
Mark a job as done.
Definition: Worker.php:98
$classNameAlias
Job class name aliasses (key = alias, value = class name).
Definition: Worker.php:7
$timeLimit
Default time limit for a job (seconds).
Definition: Worker.php:8
next($id=null)
Next scheduled job to execute.
Definition: Worker.php:66
Basic component class.
Definition: Component.php:8
reschedule($job, $time=null)
Reschedule a job.
Definition: Worker.php:91
create($class_name, $id, $config=null, $time=null, $interval=null)
Create a job object.
Definition: Worker.php:50
transClassName($class_name)
Translate a job class name.
Definition: Worker.php:19
execute($id=null)
Execute the worker / process the scheduled jobs.
Definition: Worker.php:114
$breakCount
Halt the processig of job after this amount (even when there are more jobs scheduled).
Definition: Worker.php:9
component($name)
Get a component (local or default).
Definition: Component.php:80
$breakTime
Halt the processig of job after this time (seconds; even when there are more jobs scheduled)...
Definition: Worker.php:10
unlock($job)
Unlock a job.
Definition: Worker.php:82