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;
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->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
177 $self->background_job->update_next_run_at;
178 $self->background_job->save;
180 flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
183 $self->redirect_to($self->back_to);
186 sub init_background_job {
187 return $::form->{id} ? SL::DB::BackgroundJob->new(id => $::form->{id})->load : undef;
190 sub init_task_server {
191 return SL::System::TaskServer->new;
194 sub check_task_server {
196 flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
201 return $::form->{back_to} || $self->url_for(action => 'list');
205 SL::Controller::Helper::GetModels->new(
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'),
217 package_name => [ SL::BackgroundJob::Base->get_known_job_classes ],
222 sub setup_list_action_bar {
225 for my $bar ($::request->layout->get('actionbar')) {
229 link => $self->url_for(action => 'new'),
230 accesskey => 'enter',
233 t8('Server control'),
234 link => $self->url_for(controller => 'TaskServer', action => 'show'),
238 link => $self->url_for(controller => 'BackgroundJobHistory', action => 'list'),
244 sub setup_form_action_bar {
247 my $is_new = !$self->background_job->id;
249 for my $bar ($::request->layout->get('actionbar')) {
254 submit => [ '#form', { action => 'BackgroundJob/' . ($is_new ? 'create' : 'update') } ],
255 accesskey => 'enter',
258 t8('Save and execute'),
259 submit => [ '#form', { action => 'BackgroundJob/save_and_execute' } ],
263 submit => [ '#form', { action => 'BackgroundJob/edit_as_new' } ],
264 disabled => $is_new ? t8('The object has not been saved yet.') : undef,
266 ], # end of combobox "Save"
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,
277 link => $self->url_for(action => 'list'),
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,