[861] | 1 | package LATMOS::Accounts::Task; |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use LATMOS::Accounts::Log; |
---|
| 6 | |
---|
[1018] | 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 | |
---|
[861] | 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 | |
---|
[1427] | 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 | |
---|
[1824] | 51 | =head2 order |
---|
| 52 | |
---|
[2015] | 53 | Return the priority for the module: 0, 1 or 2 (0 = first) |
---|
[1824] | 54 | |
---|
| 55 | =cut |
---|
| 56 | |
---|
| 57 | sub order { 1 } |
---|
| 58 | |
---|
[1018] | 59 | =head2 init |
---|
| 60 | |
---|
| 61 | Call at task startup, can be overload |
---|
| 62 | |
---|
| 63 | =cut |
---|
| 64 | |
---|
[861] | 65 | sub init { |
---|
| 66 | return 1; |
---|
| 67 | } |
---|
| 68 | |
---|
[1018] | 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 | |
---|
[861] | 81 | sub needupd { |
---|
[1200] | 82 | my ($self, $lastrev, $currentrev) = @_; |
---|
[861] | 83 | |
---|
[1200] | 84 | la_log LA_DEBUG, "Comparing db: %d <=> %d", $currentrev, $lastrev; |
---|
| 85 | if ($currentrev > $lastrev) { |
---|
[861] | 86 | return 1; |
---|
| 87 | } else { |
---|
| 88 | la_log LA_DEBUG, "No change in DB, doing nothing"; |
---|
| 89 | return; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
[1737] | 93 | =head2 runDelay |
---|
| 94 | |
---|
[2129] | 95 | Return the minimum of second to wait to have the module running again. |
---|
[1737] | 96 | |
---|
| 97 | Return 0 but can be override by Task module. |
---|
| 98 | |
---|
| 99 | =cut |
---|
| 100 | |
---|
| 101 | sub runDelay { 0; } |
---|
| 102 | |
---|
[1740] | 103 | =head2 waitDelay |
---|
| 104 | |
---|
[2129] | 105 | Return the number of second to wait before next run even there is changes in database. |
---|
[1740] | 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 | |
---|
[2246] | 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 | |
---|
[1740] | 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 | |
---|
[1018] | 155 | =head2 run |
---|
| 156 | |
---|
| 157 | Must be provided by module, do the desired work. |
---|
| 158 | |
---|
| 159 | =cut |
---|
| 160 | |
---|
[861] | 161 | sub run { |
---|
| 162 | return 1; |
---|
| 163 | } |
---|
| 164 | |
---|
[1018] | 165 | =head2 post |
---|
| 166 | |
---|
| 167 | Call after C<run()> |
---|
| 168 | |
---|
| 169 | =cut |
---|
| 170 | |
---|
[861] | 171 | sub post { |
---|
| 172 | } |
---|
| 173 | |
---|
[1206] | 174 | =head2 reset_savepoint |
---|
[1201] | 175 | |
---|
[1206] | 176 | Reset the savepoint to 0 to force the module to be run again |
---|
[1201] | 177 | |
---|
| 178 | =cut |
---|
| 179 | |
---|
[1206] | 180 | sub reset_savepoint { |
---|
[1201] | 181 | return 1; |
---|
| 182 | } |
---|
| 183 | |
---|
[861] | 184 | 1; |
---|
[1018] | 185 | |
---|
| 186 | __END__ |
---|
| 187 | |
---|
| 188 | =head1 SEE ALSO |
---|
| 189 | |
---|
[1019] | 190 | L<LATMOS::Accounts::SyncManager> |
---|
[1018] | 191 | |
---|
| 192 | =head1 AUTHOR |
---|
| 193 | |
---|
| 194 | Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
| 195 | |
---|
| 196 | =head1 COPYRIGHT AND LICENSE |
---|
| 197 | |
---|
| 198 | Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier |
---|
| 199 | |
---|
| 200 | This library is free software; you can redistribute it and/or modify |
---|
| 201 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 202 | at your option, any later version of Perl 5 you may have available. |
---|
| 203 | |
---|
| 204 | =cut |
---|