source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Task.pm @ 2419

Last change on this file since 2419 was 2246, checked in by nanardon, 5 years ago

Add variable to force module to run

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