From: Moritz Bunkus Date: Thu, 19 Feb 2015 15:47:30 +0000 (+0100) Subject: Hintergrundjob für E-Mail-Bericht über fehlgeschlagene Jobs X-Git-Tag: release-3.2.1~51^2~1 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=8c4f656c35da0888f5c19438064bd5c76b41c875;p=kivitendo-erp.git Hintergrundjob für E-Mail-Bericht über fehlgeschlagene Jobs --- diff --git a/SL/BackgroundJob/ALL.pm b/SL/BackgroundJob/ALL.pm index 84c55ee00..ba9ce831d 100644 --- a/SL/BackgroundJob/ALL.pm +++ b/SL/BackgroundJob/ALL.pm @@ -6,6 +6,7 @@ use SL::BackgroundJob::Base; use SL::BackgroundJob::BackgroundJobCleanup; use SL::BackgroundJob::CleanBackgroundJobHistory; use SL::BackgroundJob::CreatePeriodicInvoices; +use SL::BackgroundJob::FailedBackgroundJobsReport; 1; diff --git a/SL/BackgroundJob/FailedBackgroundJobsReport.pm b/SL/BackgroundJob/FailedBackgroundJobsReport.pm new file mode 100644 index 000000000..e0bea8b67 --- /dev/null +++ b/SL/BackgroundJob/FailedBackgroundJobsReport.pm @@ -0,0 +1,155 @@ +package SL::BackgroundJob::FailedBackgroundJobsReport; + +use strict; +use utf8; + +use parent qw(SL::BackgroundJob::Base); + +use SL::DB::BackgroundJobHistory; +use SL::Locale::String; +use SL::Mailer; + +use Rose::Object::MakeMethods::Generic ( + scalar => [ qw(data start_time entries) ], +); + + +sub create_job { + # Don't create this job by default + 1; +} + +sub check_config { + my ($self) = @_; + + die "No »recipients« specified" unless @{ $self->data->{recipients} || [] }; + die "No »from« specified" unless $self->data->{from}; + die "No »subject« specified" unless $self->data->{subject}; + + return $self; +} + +sub send_email { + my ($self) = @_; + + return 1 unless @{ $self->entries }; + + my $template = Template->new({ + INTERPOLATE => 0, + EVAL_PERL => 0, + ABSOLUTE => 1, + CACHE_SIZE => 0, + }) || die("Could not create Template instance"); + + my $file_name = $self->data->{template} || 'templates/webpages/failed_background_jobs_report/email.txt'; + my $body; + $template->process($file_name, { SELF => $self }, \$body); + $body = Encode::decode('utf-8', $body); + + Mailer->new( + from => $self->data->{from}, + to => join(', ', @{ $self->data->{recipients} }), + subject => $self->data->{subject}, + content_type => 'text/plain', + charset => 'utf-8', + message => $body, + )->send; + + return $self; +} + +sub load_failed_entries { + my ($self) = @_; + + $self->start_time(DateTime->now_local->subtract(days => 1)); + $self->entries([ @{ SL::DB::Manager::BackgroundJobHistory->get_all( + sort_by => 'run_at ASC', + where => [ + status => SL::DB::BackgroundJobHistory::FAILURE(), + run_at => { ge => $self->start_time }, + ], + )}]); + + return $self; +} + +sub run { + my ($self, $db_obj) = @_; + + $self->data($db_obj->data_as_hash); + + $self + ->check_config + ->load_failed_entries + ->send_email; + + return 1; +} + +1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::BackgroundJob::FailedBackgroundJobsReport - A background job +checking for failed jobs and reporting them via email + +=head1 OVERVIEW + +This background's job is to watch over other background jobs. It will +determine when it has run last and look for job history entries that +have failed between the last run and the current time. + +If that search yields results then an email will be sent listing the +jobs that failed and the error messages they produced. The template +used for the email's body defaults to the file +C but can +be overridden in the configuration. + +This background job is not active by default. You have to add and +configure it manually. + +=head1 CONFIGURATION + +This background job requires configuration data stored in its data +member. This is supposed to be a YAML-encoded hash of the following +options: + +=over 4 + +=item * C – required; the sender's email address used in the +mail headers + +=item * C – required; an array of email addresses for the +recipients + +=item * C – required; the email's subject + +=item * C