epic-s6g
[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 IO::Dir;
8 use SL::DB::BackgroundJob;
9 use SL::System::Process;
10
11 sub get_known_job_classes {
12   tie my %dir_h, 'IO::Dir', File::Spec->catdir(File::Spec->splitdir(SL::System::Process->exe_dir), 'SL', 'BackgroundJob');
13   return sort map { s/\.pm$//; $_ } grep { m/\.pm$/ && !m/(?: ALL | Base) \.pm$/x } keys %dir_h;
14 }
15
16 sub create_standard_job {
17   my $self_or_class = shift;
18   my $cron_spec     = shift;
19
20   my $package       = ref($self_or_class) || $self_or_class;
21   $package          =~ s/SL::BackgroundJob:://;
22
23   my %params        = (cron_spec    => $cron_spec || '* * * * *',
24                        type         => 'interval',
25                        active       => 1,
26                        package_name => $package);
27
28   my $job = SL::DB::Manager::BackgroundJob->find_by(package_name => $params{package_name});
29   if (!$job) {
30     $job = SL::DB::BackgroundJob->new(%params)->update_next_run_at;
31   } else {
32     $job->assign_attributes(%params)->update_next_run_at;
33   }
34
35   return $job;
36 }
37
38 1;
39
40 __END__
41
42 =encoding utf8
43
44 =head1 NAME
45
46 SL::BackgroundJob::Base - Base class for all background jobs
47
48 =head1 SYNOPSIS
49
50 All background jobs are derived from this class. Each job gets its own
51 class which must implement the C<run> method.
52
53 There are two types of background jobs: periodic jobs and jobs that
54 are run once. Periodic jobs have a CRON spec associated with them that
55 determines the points in time when the job is supposed to be run.
56
57 =head1 FUNCTIONS
58
59 =over 4
60
61 =item C<create_standard_job $cron_spec>
62
63 Creates or updates an entry in the database for the current job. If
64 the C<background_jobs> table contains an entry for the current class
65 (as determined by C<ref($self)>) then that entry is updated and
66 re-activated if it was disabled. Otherwise a new entry is created.
67
68 This function can be called both as a member or as a class function.
69
70 =back
71
72 =head1 BUGS
73
74 Nothing here yet.
75
76 =head1 AUTHOR
77
78 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
79
80 =cut