ultimix
schedule.php
Go to the documentation of this file.
1 <?php
2 
3  /*
4  * This source code is a part of the Ultimix Project.
5  * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6  * it's own license wich could be found from the corresponding files or sources.
7  * This source code is provided "as is" without any warranties or garanties.
8  *
9  * Have a nice day!
10  *
11  * @url http://ultimix.sorceforge.net
12  *
13  * @author Alexey "gdever" Dodonov
14  */
15 
16  /*
17  * type - 2
18  * time_step - Time step in seconds.
19  * archived - 0
20  * processing - 0
21  * count - 1
22  * next_iteration - 0
23  * iteration_step - 1
24  *
25  * This parameters means that this task will be run once in time_step seconds
26  */
27 
39 
50  var $Database = false;
51  var $String = false;
52  var $Settings = false;
53  var $ScheduleAccess = false;
54  var $ScheduleAlgorithms = false;
55 
70  function __construct()
71  {
72  try
73  {
74  $this->Database = get_package( 'database' , 'last' , __FILE__ );
75  $this->String = get_package( 'string' , 'last' , __FILE__ );
76  $this->Settings = get_package_object( 'settings::settings' , 'last' , __FILE__ );
77  $this->ScheduleAccess = get_package( 'schedule::schedule_access' , 'last' , __FILE__ );
78  $this->ScheduleAlgorithms = get_package( 'schedule::schedule_algorithms' , 'last' , __FILE__ );
79  }
80  catch( Exception $e )
81  {
82  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
83  }
84  }
85 
112  function run_task( $Parameters , $CurrentIteration )
113  {
114  try
115  {
116  $this->Settings->load_settings( $Parameters );
117  if( $this->Settings->get_setting( 'cron_ping' , '0' ) == 1 )
118  {
119  print( 'Cron ping' );
120 
121  return( $CurrentIteration + 1 );
122  }
123  }
124  catch( Exception $e )
125  {
126  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
127  }
128  }
129 
152  private function call_run_task_function( $Task )
153  {
154  try
155  {
156  $Package = get_package( $Task->package_name , $Task->package_version , __FILE__ );
157 
158  if( $Package === false )
159  {
160  throw( new Exception( 'The package "'.$Task->package_name.'" has no script' ) );
161  }
162 
163  $Delegate = array( $Package , 'schedule_task' );
164 
165  return( intval( call_user_func( $Delegate , $Task->parameters , $Task->next_iteration ) ) );
166  }
167  catch( Exception $e )
168  {
169  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
170  }
171  }
172 
195  private function finish_task_processing( $Task , $NextIteration )
196  {
197  try
198  {
199  $IntervalField = $NextIteration == 0 ? 'time_step' : 'iteration_step';
200 
201  $Fields = array( 'processing' , 'next_processing_time' , '`count`' , 'archived' , 'next_iteration' );
202  $Values = array(
203  0 , "DATE_ADD( NOW() , INTERVAL $IntervalField SECOND )" , 'IF( `count` > 1 , `count` - 1 , 1 )' ,
204  'IF( type = 1 AND `count` = 1 , 1 , 0 )' , $NextIteration
205  );
206 
207  $this->Database->update( 'umx_schedule' , $Fields , $Values , 'id = '.$Task->id );
208 
209  $this->Database->commit();
210  }
211  catch( Exception $e )
212  {
213  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
214  }
215  }
216 
235  function run_single_task( $Task )
236  {
237  try
238  {
239  $NextIteration = $this->call_run_task_function( $Task );
240 
241  $this->finish_task_processing( $Task , $NextIteration );
242  }
243  catch( Exception $e )
244  {
245  $this->Database->unlock();/* clear all locks if the exception was caught */
246  $this->ScheduleAccess->update( $Task->id , array( 'processing' => 0 ) );
247  $this->Database->commit();
248  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
249  }
250  }
251 
270  function run_all_tasks( $Tasks )
271  {
272  try
273  {
274  $Ids = implode_ex( ',' , $Tasks , 'id' );
275  $this->ScheduleAccess->update( $Ids , array( 'processing' => 1 ) );
276  $this->Database->unlock();
277 
278  foreach( $Tasks as $k => $Task )
279  {
280  $this->run_single_task( $Task );
281  }
282  }
283  catch( Exception $e )
284  {
285  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
286  }
287  }
288 
303  function run_tasks()
304  {
305  try
306  {
307  $this->Database->lock( array( 'umx_schedule' ) , array( 'WRITE' ) );
308 
309  $this->Settings->load_file( dirname( __FILE__ ).'/conf/cf_schedule_settings' );
310  $Tasks = $this->ScheduleAlgorithms->get_tasks(
311  $this->Settings->get_setting( 'processing_tasks_count' , 10 )
312  );
313 
314  if( isset( $Tasks[ 0 ] ) )
315  {
316  $this->run_all_tasks( $Tasks );
317  }
318  else
319  {
320  $this->Database->unlock();
321  }
322  }
323  catch( Exception $e )
324  {
325  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
326  }
327  }
328  }
329 
330 ?>