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

Last change on this file since 1950 was 1824, checked in by nanardon, 8 years ago

Order module to optimize data update

File size: 3.4 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 3 (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 minumum 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 a change 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    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
152Must be provided by module, do the desired work.
153
154=cut
155
156sub run {
157    return 1;
158}
159
160=head2 post
161
162Call after C<run()>
163
164=cut
165
166sub post {
167}
168
169=head2 reset_savepoint
170
171Reset the savepoint to 0 to force the module to be run again
172
173=cut
174
175sub reset_savepoint {
176    return 1;
177}
178
1791;
180
181__END__
182
183=head1 SEE ALSO
184
185L<LATMOS::Accounts::SyncManager>
186
187=head1 AUTHOR
188
189Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
190
191=head1 COPYRIGHT AND LICENSE
192
193Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier
194
195This library is free software; you can redistribute it and/or modify
196it under the same terms as Perl itself, either Perl version 5.10.0 or,
197at your option, any later version of Perl 5 you may have available.
198
199=cut
Note: See TracBrowser for help on using the repository browser.