Revision ee71ba33
Von Moritz Bunkus vor fast 14 Jahren hinzugefügt
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; |
Auch abrufbar als: Unified diff
Hintergrundjobs: DB-Model und allgemeine Modelimplementation mit ersten Test-Jobs