e0bea8b67b8b5cb0f03f7b72c18a8f9c8964cfec
[kivitendo-erp.git] / SL / BackgroundJob / FailedBackgroundJobsReport.pm
1 package SL::BackgroundJob::FailedBackgroundJobsReport;
2
3 use strict;
4 use utf8;
5
6 use parent qw(SL::BackgroundJob::Base);
7
8 use SL::DB::BackgroundJobHistory;
9 use SL::Locale::String;
10 use SL::Mailer;
11
12 use Rose::Object::MakeMethods::Generic (
13   scalar => [ qw(data start_time entries) ],
14 );
15
16
17 sub create_job {
18   # Don't create this job by default
19   1;
20 }
21
22 sub check_config {
23   my ($self) = @_;
24
25   die "No »recipients« specified" unless @{ $self->data->{recipients} || [] };
26   die "No »from« specified"       unless $self->data->{from};
27   die "No »subject« specified"    unless $self->data->{subject};
28
29   return $self;
30 }
31
32 sub send_email {
33   my ($self) = @_;
34
35   return 1 unless @{ $self->entries };
36
37   my $template  = Template->new({
38     INTERPOLATE => 0,
39     EVAL_PERL   => 0,
40     ABSOLUTE    => 1,
41     CACHE_SIZE  => 0,
42   }) || die("Could not create Template instance");
43
44   my $file_name = $self->data->{template} || 'templates/webpages/failed_background_jobs_report/email.txt';
45   my $body;
46   $template->process($file_name, { SELF => $self }, \$body);
47   $body = Encode::decode('utf-8', $body);
48
49   Mailer->new(
50     from         => $self->data->{from},
51     to           => join(', ', @{ $self->data->{recipients} }),
52     subject      => $self->data->{subject},
53     content_type => 'text/plain',
54     charset      => 'utf-8',
55     message      => $body,
56   )->send;
57
58   return $self;
59 }
60
61 sub load_failed_entries {
62   my ($self) = @_;
63
64   $self->start_time(DateTime->now_local->subtract(days => 1));
65   $self->entries([ @{ SL::DB::Manager::BackgroundJobHistory->get_all(
66     sort_by  => 'run_at ASC',
67     where    => [
68       status => SL::DB::BackgroundJobHistory::FAILURE(),
69       run_at => { ge => $self->start_time },
70     ],
71   )}]);
72
73   return $self;
74 }
75
76 sub run {
77   my ($self, $db_obj) = @_;
78
79   $self->data($db_obj->data_as_hash);
80
81   $self
82     ->check_config
83     ->load_failed_entries
84     ->send_email;
85
86   return 1;
87 }
88
89 1;
90 __END__
91
92 =pod
93
94 =encoding utf8
95
96 =head1 NAME
97
98 SL::BackgroundJob::FailedBackgroundJobsReport - A background job
99 checking for failed jobs and reporting them via email
100
101 =head1 OVERVIEW
102
103 This background's job is to watch over other background jobs. It will
104 determine when it has run last and look for job history entries that
105 have failed between the last run and the current time.
106
107 If that search yields results then an email will be sent listing the
108 jobs that failed and the error messages they produced. The template
109 used for the email's body defaults to the file
110 C<templates/webpages/failed_background_jobs_report/email.txt> but can
111 be overridden in the configuration.
112
113 This background job is not active by default. You have to add and
114 configure it manually.
115
116 =head1 CONFIGURATION
117
118 This background job requires configuration data stored in its data
119 member. This is supposed to be a YAML-encoded hash of the following
120 options:
121
122 =over 4
123
124 =item * C<from> – required; the sender's email address used in the
125 mail headers
126
127 =item * C<recipients> – required; an array of email addresses for the
128 recipients
129
130 =item * C<subject> – required; the email's subject
131
132 =item * C<template> – optional; a file name pointing to the template
133 file used for the email's body. This defaults to
134 C<templates/webpages/failed_background_jobs_report/email.txt>.
135
136 =back
137
138 Here's an example of how this data looks like:
139
140   ---
141   from: kivitendo@meine.firma
142   recipients:
143     - johanna.admin@meine.firma
144   subject: Fehlgeschlagene kivitendo-Jobs der letzten 24h
145   template: templates/mycompany/faileed_background_jobs_email.txt
146
147 =head1 BUGS
148
149 Nothing here yet.
150
151 =head1 AUTHOR
152
153 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
154
155 =cut