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

Last change on this file since 2286 was 2032, checked in by nanardon, 7 years ago

Fix: avoid useless work when applying to unexported users

  • Property svn:executable set to *
File size: 2.9 KB
Line 
1package LATMOS::Accounts::Task::Employment;
2
3use strict;
4use warnings;
5use base qw(LATMOS::Accounts::Task);
6use LATMOS::Accounts;
7use LATMOS::Accounts::Log;
8use LATMOS::Accounts::Utils;
9use LATMOS::Accounts::Bases::Sql::Employment;
10use DateTime;
11use DateTime::TimeZone;
12
13=head1 NAME
14
15LATMOS::Accounts::Task::Stats - Store value to perform some statistics
16
17=head1 DESCRIPTION
18
19=cut
20
21sub order { 0 }
22
23sub init {
24    my ($self) = @_;
25    my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1);
26    my $labase = $self->{base} ? $LA->base($self->{base}) : $LA->base;
27    $labase && $labase->load or die "Cannot load base";
28
29    $self->{_la} = $LA;
30    $self->{_base} = $labase;
31
32    1;
33}
34
35# Run it every 10 minutes, not more often
36sub runDelay { 10 * 60 }
37
38sub run {
39    my ($self) = @_;
40
41    my %users = map { $_ => undef } $self->{_base}->search_objects('user', 'exported=true');
42    my %old = $self->{_base}->attributes_summary_by_object('user', 'appliedEmployement');
43    foreach (keys %old) {
44        exists($users{$_}) or next;
45        $users{$_} = $old{$_}->[0] || '';
46    }
47
48    my $now = DateTime->now()->iso8601 . 'Z';
49
50    my $listempl = $self->{_base}->db->prepare_cached(q{
51        SELECT * FROM employment where firstday <= ?::timestamp and (lastday is null or lastday >= ?::timestamp - '1 days'::interval)
52            and exported = true
53            order by lastday, firstday
54    });
55    $listempl->execute($now, $now);
56    my %new  = ();
57    while (my $res = $listempl->fetchrow_hashref) {
58        my $employment = $res->{name};
59        my $user = $res->{user};
60        exists($users{$user}) or next;
61        # avoid undef:
62        $users{$user} ||= '';
63        if ($employment ne $users{$user}) {
64            my $ouser = $self->{_base}->get_object('user', $user) or next;
65
66            $self->{_base}->log(LA_NOTICE, "Updating user $user to match employment `%s'", $employment || '');
67            $ouser->applyCurrentEmployment;
68            $self->{_base}->commit;
69        }
70        delete($users{$user});
71    }
72
73    # Change has old employment => nothing
74    # if user is still active, expired account remains untouched
75    foreach my $user ($self->{_base}->search_objects('user', 'exported=true')) {
76        if ($users{$user}) {
77            my $ouser = $self->{_base}->get_object('user', $user);
78            if ($ouser->_resetEmployment) {
79                $self->{_base}->commit;
80            } else {
81                $self->{_base}->rollback;
82            }
83        }
84    }
85
86    return 1;
87}
88
891;
90
91__END__
92
93=head1 SEE ALSO
94
95L<LATMOS::Accounts>, L<LATMOS::Accounts::Task>
96
97=head1 AUTHOR
98
99Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
100
101=head1 COPYRIGHT AND LICENSE
102
103Copyright (C) 2012 CNRS SA/CETP/LATMOS
104
105This library is free software; you can redistribute it and/or modify
106it under the same terms as Perl itself, either Perl version 5.10.0 or,
107at your option, any later version of Perl 5 you may have available.
108
109=cut
110
Note: See TracBrowser for help on using the repository browser.