Revision c6dd542b
Von Moritz Bunkus vor fast 9 Jahren hinzugefügt
scripts/task_server.pl | ||
---|---|---|
32 | 32 |
use List::Util qw(first); |
33 | 33 |
use POSIX qw(setuid setgid); |
34 | 34 |
use SL::Auth; |
35 |
use SL::DB::AuthClient; |
|
35 | 36 |
use SL::DB::BackgroundJob; |
36 | 37 |
use SL::BackgroundJob::ALL; |
37 | 38 |
use SL::Form; |
... | ... | |
48 | 49 |
|
49 | 50 |
sub debug { |
50 | 51 |
return if !$lx_office_conf{task_server}->{debug}; |
51 |
$::lxdebug->message(0, @_); |
|
52 |
$::lxdebug->message(LXDebug::DEBUG1(), join(' ', "task server:", @_)); |
|
53 |
} |
|
54 |
|
|
55 |
sub enabled_clients { |
|
56 |
return SL::DB::Manager::AuthClient->get_all(where => [ '!task_server_user_id' => undef ]); |
|
52 | 57 |
} |
53 | 58 |
|
54 | 59 |
sub initialize_kivitendo { |
55 |
chdir $exe_dir;
|
|
60 |
my ($client) = @_;
|
|
56 | 61 |
|
57 |
my $login = $lx_office_conf{task_server}->{login}; |
|
58 |
my $client = $lx_office_conf{task_server}->{client}; |
|
62 |
chdir $exe_dir; |
|
59 | 63 |
|
60 | 64 |
package main; |
61 | 65 |
|
... | ... | |
64 | 68 |
$::locale = Locale->new($::lx_office_conf{system}->{language}); |
65 | 69 |
$::form = Form->new; |
66 | 70 |
$::auth = SL::Auth->new; |
67 |
die "No client configured or no client found with the name/ID '$client'" unless $::auth->set_client($client); |
|
71 |
|
|
72 |
return if !$client; |
|
73 |
|
|
74 |
$::auth->set_client($client->id); |
|
75 |
|
|
76 |
$::form->{__ERROR_HANDLER} = sub { die @_ }; |
|
77 |
|
|
68 | 78 |
$::instance_conf = SL::InstanceConfiguration->new; |
69 | 79 |
$::request = SL::Request->new( |
70 | 80 |
cgi => CGI->new({}), |
... | ... | |
76 | 86 |
$::auth->restore_session; |
77 | 87 |
$::auth->create_or_refresh_session; |
78 | 88 |
|
89 |
my $login = $client->task_server_user->login; |
|
90 |
|
|
79 | 91 |
die "cannot find user $login" unless %::myconfig = $::auth->read_user(login => $login); |
80 | 92 |
die "cannot find locale for user $login" unless $::locale = Locale->new($::myconfig{countrycode} || $::lx_office_conf{system}->{language}); |
81 |
|
|
82 |
$::form->{__ERROR_HANDLER} = sub { die @_ }; |
|
83 | 93 |
} |
84 | 94 |
|
85 | 95 |
sub cleanup_kivitendo { |
... | ... | |
93 | 103 |
$::form = undef; |
94 | 104 |
$::myconfig = (); |
95 | 105 |
$::request = undef; |
106 |
$::auth = undef; |
|
107 |
} |
|
108 |
|
|
109 |
sub clean_before_sleeping { |
|
96 | 110 |
Form::disconnect_standard_dbh; |
111 |
SL::DBConnect::Cache->disconnect_all_and_clear; |
|
112 |
SL::DB->db_cache->clear; |
|
97 | 113 |
} |
98 | 114 |
|
99 | 115 |
sub drop_privileges { |
... | ... | |
155 | 171 |
|
156 | 172 |
$params{client} = $::auth->client; |
157 | 173 |
|
158 |
my $body; |
|
159 |
$template->process($cfg->{email_template}, \%params, \$body); |
|
160 |
|
|
161 |
Mailer->new( |
|
162 |
from => $cfg->{email_from}, |
|
163 |
to => $email_to, |
|
164 |
subject => $cfg->{email_subject}, |
|
165 |
content_type => 'text/plain', |
|
166 |
charset => 'utf-8', |
|
167 |
message => Encode::decode('utf-8', $body), |
|
168 |
)->send; |
|
174 |
eval { |
|
175 |
my $body; |
|
176 |
$template->process($cfg->{email_template}, \%params, \$body); |
|
177 |
|
|
178 |
Mailer->new( |
|
179 |
from => $cfg->{email_from}, |
|
180 |
to => $email_to, |
|
181 |
subject => $cfg->{email_subject}, |
|
182 |
content_type => 'text/plain', |
|
183 |
charset => 'utf-8', |
|
184 |
message => Encode::decode('utf-8', $body), |
|
185 |
)->send; |
|
186 |
|
|
187 |
1; |
|
188 |
} or do { |
|
189 |
debug("Sending a failure notification failed with an exception: $@"); |
|
190 |
}; |
|
169 | 191 |
} |
170 | 192 |
|
171 | 193 |
sub gd_preconfig { |
... | ... | |
173 | 195 |
|
174 | 196 |
SL::LxOfficeConf->read($self->{configfile}); |
175 | 197 |
|
176 |
die "Missing section [task_server] in config file" unless $lx_office_conf{task_server}; |
|
177 |
die "Missing key 'login' in section [task_server] in config file" unless $lx_office_conf{task_server}->{login}; |
|
178 |
die "Missing key 'client' in section [task_server] in config file" unless $lx_office_conf{task_server}->{client}; |
|
198 |
die "Missing section [task_server] in config file" unless $lx_office_conf{task_server}; |
|
199 |
|
|
200 |
if ($lx_office_conf{task_server}->{login} || $lx_office_conf{task_server}->{client}) { |
|
201 |
print STDERR <<EOT; |
|
202 |
ERROR: The keys 'login' and/or 'client' are still present in the |
|
203 |
section [task_server] in the configuration file. These keys are |
|
204 |
deprecated. You have to configure the clients for which to run the |
|
205 |
task server in the web admin interface. |
|
206 |
|
|
207 |
The task server will refuse to start until the keys have been removed from |
|
208 |
the configuration file. |
|
209 |
EOT |
|
210 |
exit 2; |
|
211 |
} |
|
179 | 212 |
|
180 | 213 |
drop_privileges(); |
181 |
initialize_kivitendo(); |
|
182 | 214 |
|
183 | 215 |
return (); |
184 | 216 |
} |
185 | 217 |
|
186 |
sub gd_run { |
|
187 |
while (1) { |
|
188 |
my $ok = eval { |
|
189 |
initialize_kivitendo(); |
|
218 |
sub run_once_for_all_clients { |
|
219 |
initialize_kivitendo(); |
|
220 |
|
|
221 |
my $clients = enabled_clients(); |
|
222 |
|
|
223 |
foreach my $client (@{ $clients }) { |
|
224 |
debug("Running for client ID " . $client->id . " (" . $client->name . ")"); |
|
190 | 225 |
|
191 |
debug("Retrieving jobs"); |
|
226 |
my $ok = eval { |
|
227 |
initialize_kivitendo($client); |
|
192 | 228 |
|
193 | 229 |
my $jobs = SL::DB::Manager::BackgroundJob->get_all_need_to_run; |
194 | 230 |
|
195 |
debug(" Found: " . join(' ', map { $_->package_name } @{ $jobs })) if @{ $jobs }; |
|
231 |
if (@{ $jobs }) { |
|
232 |
debug(" Executing the following jobs: " . join(' ', map { $_->package_name } @{ $jobs })); |
|
233 |
} else { |
|
234 |
debug(" No jobs to execute found"); |
|
235 |
} |
|
196 | 236 |
|
197 | 237 |
foreach my $job (@{ $jobs }) { |
198 | 238 |
# Provide fresh global variables in case legacy code modifies |
199 | 239 |
# them somehow. |
200 |
initialize_kivitendo(); |
|
240 |
initialize_kivitendo($client);
|
|
201 | 241 |
|
202 | 242 |
my $history = $job->run; |
203 | 243 |
|
... | ... | |
214 | 254 |
} |
215 | 255 |
|
216 | 256 |
cleanup_kivitendo(); |
257 |
} |
|
258 |
} |
|
259 |
|
|
260 |
sub gd_run { |
|
261 |
while (1) { |
|
262 |
run_once_for_all_clients(); |
|
217 | 263 |
|
218 | 264 |
debug("Sleeping"); |
219 | 265 |
|
266 |
clean_before_sleeping(); |
|
267 |
|
|
220 | 268 |
my $seconds = 60 - (localtime)[0]; |
221 | 269 |
if (!eval { |
222 | 270 |
local $SIG{'ALRM'} = sub { |
Auch abrufbar als: Unified diff
Task-Server: Mandantenfähigkeit