line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package X10::Scheduler; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# this package implements a scheduler |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
5
|
use Data::Dumper; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
45
|
|
6
|
1
|
|
|
1
|
|
5
|
use POSIX; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
5
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
1742
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
548
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new |
11
|
|
|
|
|
|
|
{ |
12
|
0
|
|
|
0
|
0
|
|
my $type = shift; |
13
|
|
|
|
|
|
|
|
14
|
0
|
|
|
|
|
|
my $self = bless { @_ }, $type; |
15
|
|
|
|
|
|
|
|
16
|
0
|
0
|
|
|
|
|
return undef unless $self->{controller}; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$self->{logger} ||= sub { |
19
|
0
|
|
|
0
|
|
|
shift; |
20
|
0
|
|
|
|
|
|
printf(@_); |
21
|
0
|
|
|
|
|
|
print "\n"; |
22
|
0
|
|
0
|
|
|
|
}; |
23
|
|
|
|
|
|
|
|
24
|
0
|
0
|
|
|
|
|
$self->{verbose} = 1 if $self->{debug}; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# $self->{controller}->register_listener($self->event_callback); |
27
|
0
|
|
|
|
|
|
$self->{schedevents} = []; |
28
|
|
|
|
|
|
|
|
29
|
0
|
0
|
|
|
|
|
if ($self->{configfile}) |
30
|
|
|
|
|
|
|
{ |
31
|
0
|
|
0
|
|
|
|
my $config = eval { require $self->{configfile} } || die $@; |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
foreach (@$config) |
34
|
|
|
|
|
|
|
{ |
35
|
0
|
|
|
|
|
|
$self->add( new X10::SchedEvent( %$_ ) ); |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
return $self; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
### |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub setup |
45
|
|
|
|
|
|
|
{ |
46
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
47
|
|
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
|
if (@_) |
49
|
|
|
|
|
|
|
{ |
50
|
0
|
|
|
|
|
|
while (my $key = shift) |
51
|
|
|
|
|
|
|
{ |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
return $self->{schedevents}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub add |
59
|
|
|
|
|
|
|
{ |
60
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
61
|
0
|
|
|
|
|
|
my $se = shift; |
62
|
|
|
|
|
|
|
|
63
|
0
|
0
|
0
|
|
|
|
return undef unless ($se && $se->isa('X10::SchedEvent')); |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
$se->controller($self->{controller}); |
66
|
0
|
|
|
|
|
|
$se->{logger} = $self->{logger}; |
67
|
0
|
|
0
|
|
|
|
$se->{latitude} = $self->{latitude} || 38.74274; |
68
|
0
|
|
0
|
|
|
|
$se->{longitude} = $self->{longitude} || -90.560143; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
0
|
|
|
|
$self->{logger}->('info', "Queueing %s for %s", |
71
|
|
|
|
|
|
|
$se->description || 'unnamed event', |
72
|
|
|
|
|
|
|
strftime("%a %b %e %H:%M %Y", localtime($se->next_time)), |
73
|
|
|
|
|
|
|
); |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
push @{$self->{schedevents}}, $se; |
|
0
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
@{$self->{schedevents}} = |
|
0
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
sort { $a->next_time <=> $b->next_time } |
79
|
0
|
|
|
|
|
|
@{$self->{schedevents}}; |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
return 1; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub next_event_time |
85
|
|
|
|
|
|
|
{ |
86
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
87
|
|
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
|
return 0 unless (@{$self->{schedevents}} > 0); |
|
0
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
return $self->{schedevents}->[0]->next_time; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub run |
94
|
|
|
|
|
|
|
{ |
95
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
0
|
|
|
|
while ( @{$self->{schedevents}} |
|
0
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
&& $self->{schedevents}->[0]->next_time <= (int(time) + 30) ) |
99
|
|
|
|
|
|
|
{ |
100
|
0
|
|
|
|
|
|
my $se = shift @{$self->{schedevents}}; |
|
0
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
$se->run; |
103
|
|
|
|
|
|
|
|
104
|
0
|
0
|
|
|
|
|
if ($se->reschedule) |
105
|
|
|
|
|
|
|
{ |
106
|
0
|
|
|
|
|
|
$self->add($se); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
else |
109
|
|
|
|
|
|
|
{ |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# nothing to do with events |
115
|
|
|
|
|
|
|
sub event_callback |
116
|
|
|
|
|
|
|
{ |
117
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
118
|
0
|
|
|
0
|
|
|
return sub { }; |
|
0
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# no fds to deal with |
122
|
|
|
|
|
|
|
sub select_fds |
123
|
|
|
|
|
|
|
{ |
124
|
0
|
|
|
0
|
0
|
|
return (); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
1; |
129
|
|
|
|
|
|
|
|