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

Last change on this file since 2569 was 2439, checked in by nanardon, 4 years ago

Flag in log when change are made by SyncManager?

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