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 delete $::form->{background_job}->{id};
59 $self->background_job(SL::DB::BackgroundJob->new(%{ $::form->{background_job} }));
66 if ($::request->type eq 'json') {
67 $self->render(\ SL::JSON::to_json($self->background_job->as_tree), { type => 'json' });
76 $self->background_job(SL::DB::BackgroundJob->new);
77 $self->create_or_update;
82 $self->create_or_update;
88 if (eval { $self->background_job->delete; 1; }) {
89 flash_later('info', $::locale->text('The background job has been deleted.'));
91 flash_later('error', $::locale->text('The background job could not be destroyed.'));
94 $self->redirect_to($self->back_to);
97 sub action_save_and_execute {
100 $self->background_job(SL::DB::BackgroundJob->new) if !$self->background_job;
101 return unless $self->create_or_update;
102 $self->action_execute;
108 my $history = $self->background_job->run;
109 if ($history->status eq 'success') {
110 flash_later('info', $::locale->text('The background job was executed successfully.'));
112 flash_later('error', $::locale->text('There was an error executing the background job.'));
115 $self->redirect_to(controller => 'BackgroundJobHistory',
118 back_to => $self->url_for(action => 'edit', id => $self->background_job->id));
126 $::auth->assert('admin');
133 sub create_or_update {
136 my $is_new = !$self->background_job->id;
137 my $params = delete($::form->{background_job}) || { };
139 $self->background_job->assign_attributes(%{ $params });
141 my @errors = $self->background_job->validate;
144 flash('error', @errors);
145 $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
149 $self->background_job->update_next_run_at;
150 $self->background_job->save;
152 flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
155 $self->redirect_to($self->back_to);
158 sub init_background_job {
159 return $::form->{id} ? SL::DB::BackgroundJob->new(id => $::form->{id})->load : undef;
162 sub init_task_server {
163 return SL::System::TaskServer->new;
166 sub check_task_server {
168 flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
173 return $::form->{back_to} || $self->url_for(action => 'list');
177 SL::Controller::Helper::GetModels->new(
181 package_name => t8('Package name'),
182 type => t8('Execution type'),
183 active => t8('Active'),
184 cron_spec => t8('Execution schedule'),
185 last_run_at => t8('Last run at'),
186 next_run_at => t8('Next run at'),
189 package_name => [ SL::BackgroundJob::Base->get_known_job_classes ],
194 sub setup_list_action_bar {
197 for my $bar ($::request->layout->get('actionbar')) {
201 link => $self->url_for(action => 'new'),
202 accesskey => 'enter',
205 t8('Server control'),
206 link => $self->url_for(controller => 'TaskServer', action => 'show'),
210 link => $self->url_for(controller => 'BackgroundJobHistory', action => 'list'),
216 sub setup_form_action_bar {
219 my $is_new = !$self->background_job->id;
221 for my $bar ($::request->layout->get('actionbar')) {
226 submit => [ '#form', { action => 'BackgroundJob/' . ($is_new ? 'create' : 'update') } ],
227 accesskey => 'enter',
230 t8('Save and execute'),
231 submit => [ '#form', { action => 'BackgroundJob/save_and_execute' } ],
235 submit => [ '#form', { action => 'BackgroundJob/edit_as_new' } ],
236 disabled => $is_new ? t8('The object has not been saved yet.') : undef,
238 ], # end of combobox "Save"
242 submit => [ '#form', { action => 'BackgroundJob/destroy' } ],
243 confirm => t8('Do you really want to delete this object?'),
244 disabled => $is_new ? t8('This object has not been saved yet.') : undef,
249 link => $self->url_for(action => 'list'),
254 link => $self->url_for(controller => 'BackgroundJobHistory', action => 'list', 'filter.package_name:substr::ilike' => $self->background_job->package_name),
255 disabled => $is_new ? t8('This object has not been saved yet.') : undef,