Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / Controller / BackgroundJob.pm
1 package SL::Controller::BackgroundJob;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::BackgroundJob::Base;
8 use SL::Controller::Helper::GetModels;
9 use SL::DB::BackgroundJob;
10 use SL::Helper::Flash;
11 use SL::JSON;
12 use SL::Locale::String;
13 use SL::System::TaskServer;
14
15 use Rose::Object::MakeMethods::Generic
16 (
17   'scalar --get_set_init' => [ qw(task_server back_to models background_job) ],
18 );
19
20 __PACKAGE__->run_before('check_auth');
21 __PACKAGE__->run_before('check_task_server');
22
23 #
24 # actions
25 #
26
27 sub action_list {
28   my ($self) = @_;
29
30   $self->setup_list_action_bar;
31   $self->render('background_job/list',
32                 title           => $::locale->text('Background jobs'),
33                 BACKGROUND_JOBS => $self->models->get,
34                 MODELS          => $self->models);
35 }
36
37 sub action_new {
38   my ($self) = @_;
39
40   $self->background_job(SL::DB::BackgroundJob->new(cron_spec => '* * * * *',  package_name => 'Test')) unless $self->background_job;
41   $self->setup_form_action_bar;
42   $self->render('background_job/form',
43                 title       => $::locale->text('Create a new background job'),
44                 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
45 }
46
47 sub action_edit {
48   my ($self) = @_;
49
50   $self->setup_form_action_bar;
51   $self->render('background_job/form',
52                 title       => $::locale->text('Edit background job'),
53                 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
54 }
55
56 sub action_edit_as_new {
57   my ($self) = @_;
58
59   delete $::form->{background_job}->{id};
60   $self->background_job(SL::DB::BackgroundJob->new(%{ $::form->{background_job} }));
61   $self->action_new;
62 }
63
64 sub action_show {
65   my ($self) = @_;
66
67   if ($::request->type eq 'json') {
68     $self->render(\ SL::JSON::to_json($self->background_job->as_tree), { type => 'json' });
69   } else {
70     $self->action_edit;
71   }
72 }
73
74 sub action_create {
75   my ($self) = @_;
76
77   $self->background_job(SL::DB::BackgroundJob->new);
78   $self->create_or_update;
79 }
80
81 sub action_update {
82   my ($self) = @_;
83   $self->create_or_update;
84 }
85
86 sub action_destroy {
87   my ($self) = @_;
88
89   if (eval { $self->background_job->delete; 1; }) {
90     flash_later('info',  $::locale->text('The background job has been deleted.'));
91   } else {
92     flash_later('error', $::locale->text('The background job could not be destroyed.'));
93   }
94
95   $self->redirect_to($self->back_to);
96 }
97
98 sub action_save_and_execute {
99   my ($self) = @_;
100
101   $self->background_job(SL::DB::BackgroundJob->new) if !$self->background_job;
102   return unless $self->create_or_update;
103   $self->action_execute;
104 }
105
106 sub action_execute {
107   my ($self) = @_;
108
109   my $history = $self->background_job->run;
110   if ($history->status eq 'success') {
111     flash_later('info', $::locale->text('The background job was executed successfully.'));
112   } else {
113     flash_later('error', $::locale->text('There was an error executing the background job.'));
114   }
115
116   $self->redirect_to(controller => 'BackgroundJobHistory',
117                      action     => 'show',
118                      id         => $history->id,
119                      back_to    => $self->url_for(action => 'edit', id => $self->background_job->id));
120 }
121
122 sub action_execute_class {
123   my ($self) = @_;
124
125   my $result;
126
127   my $ok = eval {
128     die "no class name given in parameter 'class'" if !$::form->{class} || ($::form->{class} =~ m{[^a-z0-9]}i);
129     die "invalid class"                            if ! -f "SL/BackgroundJob/" . $::form->{class} . ".pm";
130
131     my $package = "SL::BackgroundJob::" . $::form->{class};
132
133     eval "require $package" or die $@;
134     my $job = SL::DB::BackgroundJob->new(data => $::form->{data});
135     $job->data(decode_json($::form->{json_data})) if $::form->{json_data};
136     $result = $package->new->run($job);
137
138     1;
139   };
140
141   my $reply = {
142     status => $ok ? 'succeeded' : 'failed',
143     result => $ok ? $result     : $@,
144   };
145
146   $self->render(\to_json($reply), { type => 'json' });
147 }
148
149 #
150 # filters
151 #
152
153 sub check_auth {
154   $::auth->assert('admin');
155 }
156
157 #
158 # helpers
159 #
160
161 sub create_or_update {
162   my $self   = shift;
163   my $return = shift;
164   my $is_new = !$self->background_job->id;
165   my $params = delete($::form->{background_job}) || { };
166
167   $self->background_job->assign_attributes(%{ $params });
168
169   my @errors = $self->background_job->validate;
170
171   if (@errors) {
172     flash('error', @errors);
173     $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
174     return;
175   }
176
177   $self->background_job->update_next_run_at;
178   $self->background_job->save;
179
180   flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
181   return if $return;
182
183   $self->redirect_to($self->back_to);
184 }
185
186 sub init_background_job {
187   return $::form->{id} ? SL::DB::BackgroundJob->new(id => $::form->{id})->load : undef;
188 }
189
190 sub init_task_server {
191   return SL::System::TaskServer->new;
192 }
193
194 sub check_task_server {
195   my ($self) = @_;
196   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
197 }
198
199 sub init_back_to {
200   my ($self) = @_;
201   return $::form->{back_to} || $self->url_for(action => 'list');
202 }
203
204 sub init_models {
205   SL::Controller::Helper::GetModels->new(
206     controller => $_[0],
207     filtered => 0,
208     sorted => {
209       package_name => t8('Package name'),
210       type         => t8('Execution type'),
211       active       => t8('Active'),
212       cron_spec    => t8('Execution schedule'),
213       last_run_at  => t8('Last run at'),
214       next_run_at  => t8('Next run at'),
215     },
216     query => [
217       package_name => [ SL::BackgroundJob::Base->get_known_job_classes ],
218     ],
219   );
220 }
221
222 sub setup_list_action_bar {
223   my ($self) = @_;
224
225   for my $bar ($::request->layout->get('actionbar')) {
226     $bar->add(
227       link => [
228         t8('Add'),
229         link      => $self->url_for(action => 'new'),
230         accesskey => 'enter',
231       ],
232       link => [
233         t8('Server control'),
234         link => $self->url_for(controller => 'TaskServer', action => 'show'),
235       ],
236       link => [
237         t8('Job history'),
238         link => $self->url_for(controller => 'BackgroundJobHistory', action => 'list'),
239       ],
240     );
241   }
242 }
243
244 sub setup_form_action_bar {
245   my ($self) = @_;
246
247   my $is_new = !$self->background_job->id;
248
249   for my $bar ($::request->layout->get('actionbar')) {
250     $bar->add(
251       combobox => [
252         action => [
253           t8('Save'),
254           submit    => [ '#form', { action => 'BackgroundJob/' . ($is_new ? 'create' : 'update') } ],
255           accesskey => 'enter',
256         ],
257         action => [
258           t8('Save and execute'),
259           submit => [ '#form', { action => 'BackgroundJob/save_and_execute' } ],
260         ],
261         action => [
262           t8('Use as new'),
263           submit   => [ '#form', { action => 'BackgroundJob/edit_as_new' } ],
264           disabled => $is_new ? t8('The object has not been saved yet.') : undef,
265         ],
266       ], # end of combobox "Save"
267
268       action => [
269         t8('Delete'),
270         submit   => [ '#form', { action => 'BackgroundJob/destroy' } ],
271         confirm  => t8('Do you really want to delete this object?'),
272         disabled => $is_new ? t8('This object has not been saved yet.') : undef,
273       ],
274
275       link => [
276         t8('Abort'),
277         link => $self->url_for(action => 'list'),
278       ],
279
280       link => [
281         t8('Job history'),
282         link     => $self->url_for(controller => 'BackgroundJobHistory', action => 'list', 'filter.package_name:substr::ilike' => $self->background_job->package_name),
283         disabled => $is_new ? t8('This object has not been saved yet.') : undef,
284       ],
285     );
286   }
287 }
288
289 1;