1 package SL::Controller::BackgroundJob;
5 use parent qw(SL::Controller::Base);
7 use SL::BackgroundJob::Base;
8 use SL::Controller::Helper::GetModels;
9 use SL::DB::BackgroundJob;
10 use SL::Helper::Flash;
12 use SL::Locale::String;
13 use SL::System::TaskServer;
15 use Rose::Object::MakeMethods::Generic
17 'scalar --get_set_init' => [ qw(task_server back_to models background_job) ],
20 __PACKAGE__->run_before('check_auth');
21 __PACKAGE__->run_before('check_task_server');
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);
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 ]);
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 ]);
56 sub action_edit_as_new {
59 delete $::form->{background_job}->{id};
60 $self->background_job(SL::DB::BackgroundJob->new(%{ $::form->{background_job} }));
67 if ($::request->type eq 'json') {
68 $self->render(\ SL::JSON::to_json($self->background_job->as_tree), { type => 'json' });
77 $self->background_job(SL::DB::BackgroundJob->new);
78 $self->create_or_update;
83 $self->create_or_update;
89 if (eval { $self->background_job->delete; 1; }) {
90 flash_later('info', $::locale->text('The background job has been deleted.'));
92 flash_later('error', $::locale->text('The background job could not be destroyed.'));
95 $self->redirect_to($self->back_to);
98 sub action_save_and_execute {
101 $self->background_job(SL::DB::BackgroundJob->new) if !$self->background_job;
102 return unless $self->create_or_update(1);
103 $self->action_execute;
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.'));
113 flash_later('error', $::locale->text('There was an error executing the background job.'));
116 $self->redirect_to(controller => 'BackgroundJobHistory',
119 back_to => $self->url_for(action => 'edit', id => $self->background_job->id));
122 sub action_execute_class {
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";
131 my $package = "SL::BackgroundJob::" . $::form->{class};
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);
142 status => $ok ? 'succeeded' : 'failed',
143 result => $ok ? $result : $@,
146 $self->render(\to_json($reply), { type => 'json' });
154 $::auth->assert('admin');
161 sub create_or_update {
164 my $is_new = !$self->background_job->id;
165 my $params = delete($::form->{background_job}) || { };
167 $self->background_job->assign_attributes(%{ $params });
169 my @errors = $self->background_job->validate;
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'));
178 $self->background_job->update_next_run_at;
179 $self->background_job->save;
181 flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
184 $self->redirect_to($self->back_to);
187 sub init_background_job {
188 return $::form->{id} ? SL::DB::BackgroundJob->new(id => $::form->{id})->load : undef;
191 sub init_task_server {
192 return SL::System::TaskServer->new;
195 sub check_task_server {
197 flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
202 return $::form->{back_to} || $self->url_for(action => 'list');
206 SL::Controller::Helper::GetModels->new(
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'),
219 package_name => [ SL::BackgroundJob::Base->get_known_job_classes ],
224 sub setup_list_action_bar {
227 for my $bar ($::request->layout->get('actionbar')) {
231 link => $self->url_for(action => 'new'),
232 accesskey => 'enter',
235 t8('Server control'),
236 link => $self->url_for(controller => 'TaskServer', action => 'show'),
240 link => $self->url_for(controller => 'BackgroundJobHistory', action => 'list'),
246 sub setup_form_action_bar {
249 my $is_new = !$self->background_job->id;
251 for my $bar ($::request->layout->get('actionbar')) {
256 submit => [ '#form', { action => 'BackgroundJob/' . ($is_new ? 'create' : 'update') } ],
257 accesskey => 'enter',
260 t8('Save and execute'),
261 submit => [ '#form', { action => 'BackgroundJob/save_and_execute' } ],
265 submit => [ '#form', { action => 'BackgroundJob/edit_as_new' } ],
266 disabled => $is_new ? t8('The object has not been saved yet.') : undef,
268 ], # end of combobox "Save"
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,
279 link => $self->url_for(action => 'list'),
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,