Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision ee71ba33

Von Moritz Bunkus vor fast 14 Jahren hinzugefügt

  • ID ee71ba3372b47b7de1e30d56db92c1315ae1e104
  • Vorgänger 03fc848d
  • Nachfolger 23d89499

Hintergrundjobs: DB-Model und allgemeine Modelimplementation mit ersten Test-Jobs

Unterschiede anzeigen:

SL/BackgroundJob/Base.pm
1
package SL::BackgroundJob::Base;
2

  
3
use strict;
4

  
5
use parent qw(Rose::Object);
6

  
7
use SL::DB::BackgroundJob;
8

  
9
sub create_standard_job {
10
  my $self_or_class = shift;
11
  my $cron_spec     = shift;
12

  
13
  my $package       = ref($self_or_class) || $self_or_class;
14
  $package          =~ s/SL::BackgroundJob:://;
15

  
16
  my %params        = (cron_spec    => $cron_spec || '* * * * *',
17
                       type         => 'interval',
18
                       active       => 1,
19
                       package_name => $package);
20

  
21
  my $job = SL::DB::Manager::BackgroundJob->find_by(package_name => $params{package_name});
22
  if (!$job) {
23
    $job = SL::DB::BackgroundJob->new(%params)->update_next_run_at;
24
  } else {
25
    $job->assign_attributes(%params)->update_next_run_at;
26
  }
27

  
28
  return $job;
29
}
30

  
31
1;
SL/BackgroundJob/CleanBackgroundJobHistory.pm
1
package SL::BackgroundJob::CleanBackgroundJobHistory;
2

  
3
use parent qw(SL::BackgroundJob::Base);
4

  
5
use SL::DB::BackgroundJobHistory;
6

  
7
sub create_job {
8
  $_[0]->create_standard_job('0 3 * * *'); # daily at 3:00 am
9
}
10

  
11
sub run {
12
  my $self    = shift;
13
  my $db_obj  = shift;
14

  
15
  my $options = $db_obj->data_as_hash;
16
  $options->{retention_success} ||= 14;
17
  $options->{retention_failure} ||= 3 * 30;
18

  
19
  my $today = DateTime->today_local;
20

  
21
  for my $status (qw(success failure)) {
22
    SL::DB::Manager::BackgroundJobHistory->delete_all(where =>  [ status => $status,
23
                                                                  run_at => { lt => $today->clone->subtract(days => $options->{"retention_${status}"}) } ]);
24
  }
25

  
26
  return 1;
27
}
28

  
29
1;
SL/BackgroundJob/Test.pm
1
package SL::BackgroundJob::Test;
2

  
3
use parent qw(SL::BackgroundJob::Base);
4

  
5
sub run {
6
  my $self   = shift;
7
  my $db_obj = shift;
8

  
9
  $::lxdebug->message(0, "Test job is being executed.");
10
}
11

  
12

  
13
1;
SL/DB/BackgroundJob.pm
3 3
use strict;
4 4

  
5 5
use DateTime::Event::Cron;
6
use English qw(-no_match_vars);
6 7

  
7 8
use SL::DB::MetaSetup::BackgroundJob;
8 9
use SL::DB::Manager::BackgroundJob;
9 10

  
11
use SL::DB::BackgroundJobHistory;
12

  
13
use SL::BackgroundJob::Test;
14

  
10 15
sub update_next_run_at {
11 16
  my $self = shift;
12 17

  
13 18
  my $cron = DateTime::Event::Cron->new_from_cron($self->cron_spec || '* * * * *');
14
  $self->update_attributes(next_run_at => $cron->next->set_time_zone($::locale->get_local_time_zone));
19
  $self->update_attributes(next_run_at => $cron->next(DateTime->now_local));
15 20
  return $self;
16 21
}
17 22

  
23
sub run {
24
  my $self = shift;
25

  
26
  my $package = "SL::BackgroundJob::" . $self->package_name;
27
  my $run_at  = DateTime->now_local;
28
  my $history;
29

  
30
  eval {
31
    my $result = $package->new->run($self);
32

  
33
    $history = SL::DB::BackgroundJobHistory
34
      ->new(package_name => $self->package_name,
35
            run_at       => $run_at,
36
            status       => 'success',
37
            result       => $result,
38
            data         => $self->data);
39
    $history->save;
40

  
41
    1;
42
  };
43

  
44
  if ($EVAL_ERROR) {
45
    $history = SL::DB::BackgroundJobHistory
46
      ->new(package_name => $self->package_name,
47
            run_at       => $run_at,
48
            status       => 'failure',
49
            error        => $EVAL_ERROR,
50
            data         => $self->data);
51
    $history->save;
52
  }
53

  
54
  $self->assign_attributes(last_run_at => $run_at)->update_next_run_at;
55

  
56
  return $history;
57
}
58

  
59
sub data_as_hash {
60
  my $self = shift;
61
  return {}                        if !$self->data;
62
  return $self->data               if ref($self->{data}) eq 'HASH';
63
  return YAML::Load($self->{data}) if !ref($self->{data});
64
  return {};
65
}
66

  
18 67
1;
SL/DB/Manager/BackgroundJob.pm
15 15
}
16 16

  
17 17
sub get_all_need_to_run {
18
  my $class = shift;
19
  return $class->get_all(where => [ and => [ active => 1, next_run_at => { le => DateTime->now_local } ] ]);
18
  my $class         = shift;
19

  
20
  my $now           = DateTime->now_local;
21
  my @interval_args = (and => [ type        => 'interval',
22
                                active      => 1,
23
                                next_run_at => { le => $now } ]);
24
  my @once_args     = (and => [ type        => 'once',
25
                                active      => 1,
26
                                last_run_at => undef,
27
                                or          => [ cron_spec   => undef,
28
                                                 cron_spec   => '',
29
                                                 next_run_at => undef,
30
                                                 next_run_at => { le => $now } ] ]);
31

  
32
  return $class->get_all(where => [ or => [ @interval_args, @once_args ] ]);
20 33
}
21 34

  
22 35
1;

Auch abrufbar als: Unified diff