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

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

Order module to optimize data update

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