1 package SL::BackgroundJob::FailedBackgroundJobsReport;
 
   6 use parent qw(SL::BackgroundJob::Base);
 
   8 use SL::DB::BackgroundJobHistory;
 
   9 use SL::Locale::String;
 
  12 use Rose::Object::MakeMethods::Generic (
 
  13   scalar => [ qw(data start_time entries) ],
 
  18   # Don't create this job by default
 
  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};
 
  35   return 1 unless @{ $self->entries };
 
  37   my $template  = Template->new({
 
  42   }) || die("Could not create Template instance");
 
  44   my $file_name = $self->data->{template} || 'templates/webpages/failed_background_jobs_report/email.txt';
 
  46   $template->process($file_name, { SELF => $self }, \$body);
 
  47   $body = Encode::decode('utf-8', $body);
 
  50     from         => $self->data->{from},
 
  51     to           => join(', ', @{ $self->data->{recipients} }),
 
  52     subject      => $self->data->{subject},
 
  53     content_type => 'text/plain',
 
  61 sub load_failed_entries {
 
  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',
 
  68       status => SL::DB::BackgroundJobHistory::FAILURE(),
 
  69       run_at => { ge => $self->start_time },
 
  77   my ($self, $db_obj) = @_;
 
  79   $self->data($db_obj->data_as_hash);
 
  98 SL::BackgroundJob::FailedBackgroundJobsReport - A background job
 
  99 checking for failed jobs and reporting them via email
 
 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.
 
 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.
 
 113 This background job is not active by default. You have to add and
 
 114 configure it manually.
 
 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
 
 124 =item * C<from> – required; the sender's email address used in the
 
 127 =item * C<recipients> – required; an array of email addresses for the
 
 130 =item * C<subject> – required; the email's subject
 
 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>.
 
 138 Here's an example of how this data looks like:
 
 141   from: kivitendo@meine.firma
 
 143     - johanna.admin@meine.firma
 
 144   subject: Fehlgeschlagene kivitendo-Jobs der letzten 24h
 
 145   template: templates/mycompany/faileed_background_jobs_email.txt
 
 153 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>