schedul_task.anubis
2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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.