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 => [ qw(background_job) ],
17 'scalar --get_set_init' => [ qw(task_server back_to models) ],
20 __PACKAGE__->run_before('check_auth');
21 __PACKAGE__->run_before('check_task_server');
22 __PACKAGE__->run_before('load_background_job', only => [ qw(edit update destroy execute show) ]);
31 $self->setup_list_action_bar;
32 $self->render('background_job/list',
33 title => $::locale->text('Background jobs'),
34 BACKGROUND_JOBS => $self->models->get,
35 MODELS => $self->models);
41 $self->background_job(SL::DB::BackgroundJob->new(cron_spec => '* * * * *', package_name => 'Test'));
42 $self->setup_form_action_bar;
43 $self->render('background_job/form',
44 title => $::locale->text('Create a new background job'),
45 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
51 $self->setup_form_action_bar;
52 $self->render('background_job/form',
53 title => $::locale->text('Edit background job'),
54 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
60 if ($::request->type eq 'json') {
61 $self->render(\ SL::JSON::to_json($self->background_job->as_tree), { type => 'json' });
70 $self->background_job(SL::DB::BackgroundJob->new);
71 $self->create_or_update;
76 $self->create_or_update;
82 if (eval { $self->background_job->delete; 1; }) {
83 flash_later('info', $::locale->text('The background job has been deleted.'));
85 flash_later('error', $::locale->text('The background job could not be destroyed.'));
88 $self->redirect_to($self->back_to);
91 sub action_save_and_execute {
94 $self->background_job(SL::DB::BackgroundJob->new) if !$self->background_job;
95 return unless $self->create_or_update;
96 $self->action_execute;
102 my $history = $self->background_job->run;
103 if ($history->status eq 'success') {
104 flash_later('info', $::locale->text('The background job was executed successfully.'));
106 flash_later('error', $::locale->text('There was an error executing the background job.'));
109 $self->redirect_to(controller => 'BackgroundJobHistory',
112 back_to => $self->url_for(action => 'edit', id => $self->background_job->id));
120 $::auth->assert('admin');
127 sub create_or_update {
130 my $is_new = !$self->background_job->id;
131 my $params = delete($::form->{background_job}) || { };
133 $self->background_job->assign_attributes(%{ $params });
135 my @errors = $self->background_job->validate;
138 flash('error', @errors);
139 $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
143 $self->background_job->update_next_run_at;
144 $self->background_job->save;
146 flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
149 $self->redirect_to($self->back_to);
152 sub load_background_job {
154 $self->background_job(SL::DB::BackgroundJob->new(id => $::form->{id})->load);
157 sub init_task_server {
158 return SL::System::TaskServer->new;
161 sub check_task_server {
163 flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
168 return $::form->{back_to} || $self->url_for(action => 'list');
172 SL::Controller::Helper::GetModels->new(
176 package_name => t8('Package name'),
177 type => t8('Execution type'),
178 active => t8('Active'),
179 cron_spec => t8('Execution schedule'),
180 last_run_at => t8('Last run at'),
181 next_run_at => t8('Next run at'),
184 package_name => [ SL::BackgroundJob::Base->get_known_job_classes ],
189 sub setup_list_action_bar {
192 for my $bar ($::request->layout->get('actionbar')) {
196 link => $self->url_for(action => 'new'),
197 accesskey => 'enter',
200 t8('Server control'),
201 link => $self->url_for(controller => 'TaskServer', action => 'show'),
205 link => $self->url_for(controller => 'BackgroundJobHistory', action => 'list'),
211 sub setup_form_action_bar {
214 my $is_new = !$self->background_job->id;
216 for my $bar ($::request->layout->get('actionbar')) {
221 submit => [ '#form', { action => 'BackgroundJob/' . ($is_new ? 'create' : 'update') } ],
222 accesskey => 'enter',
225 t8('Save and execute'),
226 submit => [ '#form', { action => 'BackgroundJob/save_and_execute' } ],
228 ], # end of combobox "Save"
232 submit => [ '#form', { action => 'BackgroundJob/delete' } ],
233 confirm => t8('Do you really want to delete this object?'),
234 disabled => $is_new ? t8('This object has not been saved yet.') : undef,
239 link => $self->url_for(action => 'list'),
244 link => $self->url_for(controller => 'BackgroundJobHistory', action => 'list', 'filter.package_name:substr::ilike' => $self->background_job->package_name),
245 disabled => $is_new ? t8('This object has not been saved yet.') : undef,