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;
11 use SL::Locale::String;
12 use SL::System::TaskServer;
14 use Rose::Object::MakeMethods::Generic
16 'scalar --get_set_init' => [ qw(task_server back_to models background_job) ],
19 __PACKAGE__->run_before('check_auth');
20 __PACKAGE__->run_before('check_task_server');
29 $self->setup_list_action_bar;
30 $self->render('background_job/list',
31 title => $::locale->text('Background jobs'),
32 BACKGROUND_JOBS => $self->models->get,
33 MODELS => $self->models);
39 $self->background_job(SL::DB::BackgroundJob->new(cron_spec => '* * * * *', package_name => 'Test')) unless $self->background_job;
40 $self->setup_form_action_bar;
41 $self->render('background_job/form',
42 title => $::locale->text('Create a new background job'),
43 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
49 $self->setup_form_action_bar;
50 $self->render('background_job/form',
51 title => $::locale->text('Edit background job'),
52 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
55 sub action_edit_as_new {
58 $self->background_job($self->background_job->clone_and_reset);
65 if ($::request->type eq 'json') {
66 $self->render(\ SL::JSON::to_json($self->background_job->as_tree), { type => 'json' });
75 $self->background_job(SL::DB::BackgroundJob->new);
76 $self->create_or_update;
81 $self->create_or_update;
87 if (eval { $self->background_job->delete; 1; }) {
88 flash_later('info', $::locale->text('The background job has been deleted.'));
90 flash_later('error', $::locale->text('The background job could not be destroyed.'));
93 $self->redirect_to($self->back_to);
96 sub action_save_and_execute {
99 $self->background_job(SL::DB::BackgroundJob->new) if !$self->background_job;
100 return unless $self->create_or_update;
101 $self->action_execute;
107 my $history = $self->background_job->run;
108 if ($history->status eq 'success') {
109 flash_later('info', $::locale->text('The background job was executed successfully.'));
111 flash_later('error', $::locale->text('There was an error executing the background job.'));
114 $self->redirect_to(controller => 'BackgroundJobHistory',
117 back_to => $self->url_for(action => 'edit', id => $self->background_job->id));
125 $::auth->assert('admin');
132 sub create_or_update {
135 my $is_new = !$self->background_job->id;
136 my $params = delete($::form->{background_job}) || { };
138 $self->background_job->assign_attributes(%{ $params });
140 my @errors = $self->background_job->validate;
143 flash('error', @errors);
144 $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
148 $self->background_job->update_next_run_at;
149 $self->background_job->save;
151 flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
154 $self->redirect_to($self->back_to);
157 sub init_background_job {
158 return $::form->{id} ? SL::DB::BackgroundJob->new(id => $::form->{id})->load : undef;
161 sub init_task_server {
162 return SL::System::TaskServer->new;
165 sub check_task_server {
167 flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
172 return $::form->{back_to} || $self->url_for(action => 'list');
176 SL::Controller::Helper::GetModels->new(
180 package_name => t8('Package name'),
181 type => t8('Execution type'),
182 active => t8('Active'),
183 cron_spec => t8('Execution schedule'),
184 last_run_at => t8('Last run at'),
185 next_run_at => t8('Next run at'),
188 package_name => [ SL::BackgroundJob::Base->get_known_job_classes ],
193 sub setup_list_action_bar {
196 for my $bar ($::request->layout->get('actionbar')) {
200 link => $self->url_for(action => 'new'),
201 accesskey => 'enter',
204 t8('Server control'),
205 link => $self->url_for(controller => 'TaskServer', action => 'show'),
209 link => $self->url_for(controller => 'BackgroundJobHistory', action => 'list'),
215 sub setup_form_action_bar {
218 my $is_new = !$self->background_job->id;
220 for my $bar ($::request->layout->get('actionbar')) {
225 submit => [ '#form', { action => 'BackgroundJob/' . ($is_new ? 'create' : 'update') } ],
226 accesskey => 'enter',
229 t8('Save and execute'),
230 submit => [ '#form', { action => 'BackgroundJob/save_and_execute' } ],
234 submit => [ '#form', { action => 'BackgroundJob/edit_as_new' } ],
235 disabled => $is_new ? t8('The object has not been saved yet.') : undef,
237 ], # end of combobox "Save"
241 submit => [ '#form', { action => 'BackgroundJob/destroy' } ],
242 confirm => t8('Do you really want to delete this object?'),
243 disabled => $is_new ? t8('This object has not been saved yet.') : undef,
248 link => $self->url_for(action => 'list'),
253 link => $self->url_for(controller => 'BackgroundJobHistory', action => 'list', 'filter.package_name:substr::ilike' => $self->background_job->package_name),
254 disabled => $is_new ? t8('This object has not been saved yet.') : undef,