schedul_task.anubis 2.75 KB

           Scheduling a repetitive task. 
   
   
   
   
   This file provides a tool for scheduling a repetitive task. 
   
   The task presents itself in the form of a function from One to One. In other words, you
   should define it as follows:
   
   define One
     my_task
       (
         One x
       ) =
     <anything of type One, producing a side effect>.
   
   
   If, for example, you want to execute this task every one hour starting on 2003 aug 4 at
   8 PM, just write this:
   
   schedul_task(my_task,
                date_and_time(2003,7,4,20,0,0,0,0),
                3600,
                shutdown_required)
             
   
   Below is the declaration of the function 'schedul_task': 
   
public define One
   schedul_task
     (
       One -> One                 task,     // the task to be executed
       Date_and_Time             start,     // the time at which the first execution should take place
       Word32                 interval,     // interval between two executions (in seconds)
       Var(Bool)     shutdown_required      // exit the loop when it becomes 'true'
     ). 
   
   Notice that this function returns immediately. Indeed, it delegates the work of waiting
   for the right time to another virtual machine.
   
   
   
   
   
   --- That's all for the public part ! --------------------------------------------------
   
read basis.anubis
   
   
   The starting time given may be past.  In that case, we must compute the first scheduled
   time in the future.
   
define Word32
   compute_start_time
     (
       Word32 start,
       Word32 interval, 
     ) =
   if to_Int(start) > now
   then start
   else compute_start_time(start+interval,interval). 
   
 
define One
   schedul_task
     (
       One -> One        task, 
       Word32             start, 
       Word32             interval,
       Var(Bool)         shutdown_required
     ) =
   sleep(1000); 
   if *shutdown_required then unique else
   if now >= to_Int(start) 
   then (
        delegate task(unique),
        schedul_task(task,start+interval,interval,shutdown_required)
        )
   else schedul_task(task,start,interval,shutdown_required). 
   

   
public define One
   schedul_task
     (
       One -> One                 task,     // the task to be executed
       Date_and_Time             start,     // the time at which the first execution should take place
       Word32                  interval,     // interval between two executions (in seconds)
       Var(Bool)     shutdown_required      // exit the loop when it becomes 'true'
     ) =
   delegate schedul_task(task,
                         compute_start_time(truncate_to_Word32(convert_time(start)),interval),
                         interval,
                         shutdown_required), 
   unique.