Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 29458987

Von Moritz Bunkus vor mehr als 10 Jahren hinzugefügt

  • ID 29458987fb3e3f2027990a55c7c6e51c68683905
  • Vorgänger 8e115155
  • Nachfolger d6d08d5e

Task server: Fehlgeschlagene Jobs optional per E-Mail melden

Unterschiede anzeigen:

config/kivitendo.conf.default
153 153
# Chose a system user the daemon should run under when started as root.
154 154
run_as =
155 155

  
156
[task_server/notify_on_failure]
157
# If you want email notifications for failed jobs then set this to a
158
# kivitendo user (login) name. The subject can be changed as well.
159
send_email_to  =
160
# The "From:" header for said email.
161
email_from     = kivitendo Daemon <root@localhost>
162
# The subject for said email.
163
email_subject  = kivitendo Task-Server: Hintergrundjob fehlgeschlagen
164
# The template file used for the email's body.
165
email_template = templates/webpages/task_server/failure_notification_email.txt
166

  
156 167
[periodic_invoices]
157 168
# The user name a report about the posted and printed invoices is sent
158 169
# to.
scripts/task_server.pl
1 1
#!/usr/bin/perl
2 2

  
3

  
4
use List::MoreUtils qw(any);
5

  
3 6
use strict;
4 7

  
5 8
my $exe_dir;
......
23 26
use Daemon::Generic;
24 27
use Data::Dumper;
25 28
use DateTime;
29
use Encode qw();
26 30
use English qw(-no_match_vars);
27 31
use File::Spec;
28 32
use List::Util qw(first);
......
36 40
use SL::LXDebug;
37 41
use SL::LxOfficeConf;
38 42
use SL::Locale;
43
use SL::Mailer;
39 44
use SL::System::TaskServer;
45
use Template;
40 46

  
41 47
our %lx_office_conf;
42 48

  
......
97 103
  }
98 104
}
99 105

  
106
sub notify_on_failure {
107
  my (%params) = @_;
108

  
109
  my $cfg = $lx_office_conf{'task_server/notify_on_failure'} || {};
110

  
111
  return if any { !$cfg->{$_} } qw(send_email_to email_from email_subject email_template);
112

  
113
  chdir $exe_dir;
114

  
115
  return debug("Template " . $cfg->{email_template} . " missing!") unless -f $cfg->{email_template};
116

  
117
  my $email_to = $cfg->{send_email_to};
118
  if ($email_to !~ m{\@}) {
119
    my %user = $::auth->read_user(login => $email_to);
120
    return debug("cannot find user for notification $email_to") unless %user;
121

  
122
    $email_to = $user{email};
123
    return debug("user for notification " . $user{login} . " doesn't have a valid email address") unless $email_to =~ m{\@};
124
  }
125

  
126
  my $template  = Template->new({
127
    INTERPOLATE => 0,
128
    EVAL_PERL   => 0,
129
    ABSOLUTE    => 1,
130
    CACHE_SIZE  => 0,
131
  });
132

  
133
  return debug("Could not create Template instance") unless $template;
134

  
135
  $params{client} = $::auth->client;
136

  
137
  my $body;
138
  $template->process($cfg->{email_template}, \%params, \$body);
139

  
140
  Mailer->new(
141
    from         => $cfg->{email_from},
142
    to           => $email_to,
143
    subject      => $cfg->{email_subject},
144
    content_type => 'text/plain',
145
    charset      => 'utf-8',
146
    message      => Encode::decode('utf-8', $body),
147
  )->send;
148
}
149

  
100 150
sub gd_preconfig {
101 151
  my $self = shift;
102 152

  
......
129 179

  
130 180
        chdir $exe_dir;
131 181

  
132
        $job->run;
182
        my $history = $job->run;
183

  
184
        notify_on_failure(history => $history) if $history && $history->has_failed;
133 185
      }
134 186

  
135 187
      1;
136 188
    };
137 189

  
138
    debug("Exception during execution: ${EVAL_ERROR}") if !$ok;
190
    if (!$ok) {
191
      my $error = $EVAL_ERROR;
192
      debug("Exception during execution: ${error}");
193
      notify_on_failure(exception => $error);
194
    }
195

  
139 196
    debug("Sleeping");
140 197

  
141 198
    my $seconds = 60 - (localtime)[0];
templates/webpages/task_server/failure_notification_email.txt
1
Hallo,
2

  
3
[% IF history -%]
4
der folgende Hintergrundjob ist bei kivitendo für den Mandanten [% client.name %] fehlgeschlagen:
5

  
6
Paketname:  [% history.package_name %]
7
Ausgeführt: [% history.run_at_as_timestamp %]
8
Fehler:     [% history.error_col %]
9

  
10
[% ELSE -%]
11
bei der Ausführung des Task-Servers für den Mandanten [% client.name %] trat die folgende Ausnahme auf:
12

  
13
[% exception %]
14
[% END -%]
15
MfG,
16
kivitendo Task-Server

Auch abrufbar als: Unified diff