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

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

Make la-sync-manager simplier, better granularity about when running modules

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