1 | package LATMOS::Accounts::Task; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts::Log; |
---|
6 | |
---|
7 | =head1 NAME |
---|
8 | |
---|
9 | LATMOS::Accounts::Task |
---|
10 | |
---|
11 | =head1 DESCRIPTION |
---|
12 | |
---|
13 | Parent class for regular task run by L<la-sync-manager> |
---|
14 | |
---|
15 | =head1 FUNCTIONS |
---|
16 | |
---|
17 | =head2 new($module, %options) |
---|
18 | |
---|
19 | Instanciate a new task of type C<$module> |
---|
20 | |
---|
21 | The module must provide a C<_new()> function. |
---|
22 | |
---|
23 | =cut |
---|
24 | |
---|
25 | sub new { |
---|
26 | my ($class, $module, %options) = @_; |
---|
27 | my $pclass = ucfirst(lc($module)); |
---|
28 | my $me = __PACKAGE__; |
---|
29 | eval "require ${me}::$pclass;"; |
---|
30 | if ($@) { return } # error message ? |
---|
31 | return "${me}::$pclass"->_new(%options); |
---|
32 | } |
---|
33 | |
---|
34 | sub _new { |
---|
35 | my ($class, %options) = @_; |
---|
36 | bless { %options }, $class; |
---|
37 | } |
---|
38 | |
---|
39 | =head2 config($value, $default) |
---|
40 | |
---|
41 | Return config value for the module |
---|
42 | |
---|
43 | =cut |
---|
44 | |
---|
45 | sub config { |
---|
46 | my ($self, $value, $default) = @_; |
---|
47 | |
---|
48 | return $self->{syncm}->ini->val($self->{name}, $value, $default) |
---|
49 | } |
---|
50 | |
---|
51 | =head2 order |
---|
52 | |
---|
53 | Return the priority for the module: 0, 1 or 3 (0 = first) |
---|
54 | |
---|
55 | =cut |
---|
56 | |
---|
57 | sub order { 1 } |
---|
58 | |
---|
59 | =head2 init |
---|
60 | |
---|
61 | Call at task startup, can be overload |
---|
62 | |
---|
63 | =cut |
---|
64 | |
---|
65 | sub init { |
---|
66 | return 1; |
---|
67 | } |
---|
68 | |
---|
69 | =head2 needupd($baserev, $syncm) |
---|
70 | |
---|
71 | This function is to call to check if the module have to run or not. |
---|
72 | |
---|
73 | C<$baserev> is the current base revision en C<$syncm> the revision when |
---|
74 | la-sync-manager was run. |
---|
75 | |
---|
76 | By default return true only if base has changed, overload for different |
---|
77 | behavior. |
---|
78 | |
---|
79 | =cut |
---|
80 | |
---|
81 | sub needupd { |
---|
82 | my ($self, $lastrev, $currentrev) = @_; |
---|
83 | |
---|
84 | la_log LA_DEBUG, "Comparing db: %d <=> %d", $currentrev, $lastrev; |
---|
85 | if ($currentrev > $lastrev) { |
---|
86 | return 1; |
---|
87 | } else { |
---|
88 | la_log LA_DEBUG, "No change in DB, doing nothing"; |
---|
89 | return; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | =head2 runDelay |
---|
94 | |
---|
95 | Return the minumum of second to wait to have the module running again. |
---|
96 | |
---|
97 | Return 0 but can be override by Task module. |
---|
98 | |
---|
99 | =cut |
---|
100 | |
---|
101 | sub runDelay { 0; } |
---|
102 | |
---|
103 | =head2 waitDelay |
---|
104 | |
---|
105 | Return the number of second to wait before next run even there is a change in database. |
---|
106 | |
---|
107 | Return 0 but can be override by Task module. |
---|
108 | |
---|
109 | =cut |
---|
110 | |
---|
111 | sub waitDelay { 0; } |
---|
112 | |
---|
113 | =head2 needToBeRun |
---|
114 | |
---|
115 | Return TRUE is the module has to be run or not. |
---|
116 | |
---|
117 | =cut |
---|
118 | |
---|
119 | sub needToBeRun { |
---|
120 | my ($self, $Sync, $module) = @_; |
---|
121 | |
---|
122 | my ($lastrev, $lastRunTime) = $Sync->get_last_rev($module); |
---|
123 | |
---|
124 | my $currentrev = $Sync->dbrev; |
---|
125 | my $currentTime = time; |
---|
126 | |
---|
127 | if ($currentTime < $lastRunTime + $self->waitDelay) { |
---|
128 | la_log LA_DEBUG, "Too early to run this module now"; |
---|
129 | return 0; |
---|
130 | } |
---|
131 | |
---|
132 | if ($self->needupd($lastrev, $currentrev, $Sync)) { |
---|
133 | return 1; |
---|
134 | } else { |
---|
135 | la_log LA_DEBUG, "No change on main base, cheking delay"; |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | if (my $delay = $self->runDelay) { |
---|
140 | if ($currentTime >= $lastRunTime + $self->runDelay) { |
---|
141 | return 1; |
---|
142 | } else { |
---|
143 | la_log LA_DEBUG, "Too short delay to run self, waiting"; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | return 0; |
---|
148 | } |
---|
149 | |
---|
150 | =head2 run |
---|
151 | |
---|
152 | Must be provided by module, do the desired work. |
---|
153 | |
---|
154 | =cut |
---|
155 | |
---|
156 | sub run { |
---|
157 | return 1; |
---|
158 | } |
---|
159 | |
---|
160 | =head2 post |
---|
161 | |
---|
162 | Call after C<run()> |
---|
163 | |
---|
164 | =cut |
---|
165 | |
---|
166 | sub post { |
---|
167 | } |
---|
168 | |
---|
169 | =head2 reset_savepoint |
---|
170 | |
---|
171 | Reset the savepoint to 0 to force the module to be run again |
---|
172 | |
---|
173 | =cut |
---|
174 | |
---|
175 | sub reset_savepoint { |
---|
176 | return 1; |
---|
177 | } |
---|
178 | |
---|
179 | 1; |
---|
180 | |
---|
181 | __END__ |
---|
182 | |
---|
183 | =head1 SEE ALSO |
---|
184 | |
---|
185 | L<LATMOS::Accounts::SyncManager> |
---|
186 | |
---|
187 | =head1 AUTHOR |
---|
188 | |
---|
189 | Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
190 | |
---|
191 | =head1 COPYRIGHT AND LICENSE |
---|
192 | |
---|
193 | Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier |
---|
194 | |
---|
195 | This library is free software; you can redistribute it and/or modify |
---|
196 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
197 | at your option, any later version of Perl 5 you may have available. |
---|
198 | |
---|
199 | =cut |
---|