Revision 29458987
Von Moritz Bunkus vor mehr als 10 Jahren hinzugefügt
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]; |
Auch abrufbar als: Unified diff
Task server: Fehlgeschlagene Jobs optional per E-Mail melden