]> wagnertech.de Git - mfinanz.git/blob - SL/Controller/BackgroundJob.pm
restart apache2 in postinst
[mfinanz.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(1);
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->setup_form_action_bar;
174     $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
175     return;
176   }
177
178   $self->background_job->update_next_run_at;
179   $self->background_job->save;
180
181   flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
182   return 1 if $return;
183
184   $self->redirect_to($self->back_to);
185 }
186
187 sub init_background_job {
188   return $::form->{id} ? SL::DB::BackgroundJob->new(id => $::form->{id})->load : undef;
189 }
190
191 sub init_task_server {
192   return SL::System::TaskServer->new;
193 }
194
195 sub check_task_server {
196   my ($self) = @_;
197   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
198 }
199
200 sub init_back_to {
201   my ($self) = @_;
202   return $::form->{back_to} || $self->url_for(action => 'list');
203 }
204
205 sub init_models {
206   SL::Controller::Helper::GetModels->new(
207     controller => $_[0],
208     filtered => 0,
209     sorted => {
210       package_name => t8('Package name'),
211       description  => t8('Description'),
212       type         => t8('Execution type'),
213       active       => t8('Active'),
214       cron_spec    => t8('Execution schedule'),
215       last_run_at  => t8('Last run at'),
216       next_run_at  => t8('Next run at'),
217     },
218     query => [
219       package_name => [ SL::BackgroundJob::Base->get_known_job_classes ],
220     ],
221   );
222 }
223
224 sub setup_list_action_bar {
225   my ($self) = @_;
226
227   for my $bar ($::request->layout->get('actionbar')) {
228     $bar->add(
229       link => [
230         t8('Add'),
231         link      => $self->url_for(action => 'new'),
232         accesskey => 'enter',
233       ],
234       link => [
235         t8('Server control'),
236         link => $self->url_for(controller => 'TaskServer', action => 'show'),
237       ],
238       link => [
239         t8('Job history'),
240         link => $self->url_for(controller => 'BackgroundJobHistory', action => 'list'),
241       ],
242     );
243   }
244 }
245
246 sub setup_form_action_bar {
247   my ($self) = @_;
248
249   my $is_new = !$self->background_job->id;
250
251   for my $bar ($::request->layout->get('actionbar')) {
252     $bar->add(
253       combobox => [
254         action => [
255           t8('Save'),
256           submit    => [ '#form', { action => 'BackgroundJob/' . ($is_new ? 'create' : 'update') } ],
257           accesskey => 'enter',
258         ],
259         action => [
260           t8('Save and execute'),
261           submit => [ '#form', { action => 'BackgroundJob/save_and_execute' } ],
262         ],
263         action => [
264           t8('Use as new'),
265           submit   => [ '#form', { action => 'BackgroundJob/edit_as_new' } ],
266           disabled => $is_new ? t8('The object has not been saved yet.') : undef,
267         ],
268       ], # end of combobox "Save"
269
270       action => [
271         t8('Delete'),
272         submit   => [ '#form', { action => 'BackgroundJob/destroy' } ],
273         confirm  => t8('Do you really want to delete this object?'),
274         disabled => $is_new ? t8('This object has not been saved yet.') : undef,
275       ],
276
277       link => [
278         t8('Abort'),
279         link => $self->url_for(action => 'list'),
280       ],
281
282       link => [
283         t8('Job history'),
284         link     => $self->url_for(controller => 'BackgroundJobHistory', action => 'list', 'filter.package_name:substr::ilike' => $self->background_job->package_name),
285         disabled => $is_new ? t8('This object has not been saved yet.') : undef,
286       ],
287     );
288   }
289 }
290
291 1;