Merge branch 'rb-wiederkehrende-rechnungen' into 263
[kivitendo-erp.git] / 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;
32
33 __END__
34
35 =encoding utf8
36
37 =head1 NAME
38
39 SL::BackgroundJob::Base - Base class for all background jobs
40
41 =head1 SYNOPSIS
42
43 All background jobs are derived from this class. Each job gets its own
44 class which must implement the C<run> method.
45
46 There are two types of background jobs: periodic jobs and jobs that
47 are run once. Periodic jobs have a CRON spec associated with them that
48 determines the points in time when the job is supposed to be run.
49
50 =head1 FUNCTIONS
51
52 =over 4
53
54 =item C<create_standard_job $cron_spec>
55
56 Creates or updates an entry in the database for the current job. If
57 the C<background_jobs> table contains an entry for the current class
58 (as determined by C<ref($self)>) then that entry is updated and
59 re-activated if it was disabled. Otherwise a new entry is created.
60
61 This function can be called both as a member or as a class function.
62
63 =back
64
65 =head1 BUGS
66
67 Nothing here yet.
68
69 =head1 AUTHOR
70
71 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
72
73 =cut