1 package SL::Controller::BackgroundJob;
5 use parent qw(SL::Controller::Base);
7 use SL::Controller::Helper::GetModels;
8 use SL::Controller::Helper::Sorted;
9 use SL::DB::BackgroundJob;
10 use SL::Helper::Flash;
11 use SL::System::TaskServer;
13 use Rose::Object::MakeMethods::Generic
15 scalar => [ qw(background_job) ],
16 'scalar --get_set_init' => [ qw(task_server back_to) ],
19 __PACKAGE__->run_before('check_auth');
20 __PACKAGE__->run_before('check_task_server');
21 __PACKAGE__->run_before('load_background_job', only => [ qw(edit update destroy execute) ]);
23 __PACKAGE__->make_sorted(
26 package_name => $::locale->text('Package name'),
27 type => $::locale->text('Execution type'),
28 active => $::locale->text('Active'),
29 cron_spec => $::locale->text('Execution schedule'),
30 last_run_at => $::locale->text('Last run at'),
31 next_run_at => $::locale->text('Next run at'),
41 $self->render('background_job/list',
42 title => $::locale->text('Background jobs'),
43 BACKGROUND_JOBS => $self->get_models);
49 $self->background_job(SL::DB::BackgroundJob->new(cron_spec => '* * * * *'));
50 $self->render('background_job/form', title => $::locale->text('Create a new background job'));
55 $self->render('background_job/form', title => $::locale->text('Edit background job'));
61 $self->background_job(SL::DB::BackgroundJob->new);
62 $self->create_or_update;
67 $self->create_or_update;
73 if (eval { $self->background_job->delete; 1; }) {
74 flash_later('info', $::locale->text('The background job has been deleted.'));
76 flash_later('error', $::locale->text('The background job could not be destroyed.'));
79 $self->redirect_to($self->back_to);
82 sub action_save_and_execute {
85 return unless $self->create_or_update;
86 $self->action_execute;
92 my $history = $self->background_job->run;
93 if ($history->status eq 'success') {
94 flash_later('info', $::locale->text('The background job was executed successfully.'));
96 flash_later('error', $::locale->text('There was an error executing the background job.'));
99 $self->redirect_to(controller => 'BackgroundJobHistory',
102 back_to => $self->url_for(action => 'edit', id => $self->background_job->id));
110 $::auth->assert('admin');
117 sub create_or_update {
120 my $is_new = !$self->background_job->id;
121 my $params = delete($::form->{background_job}) || { };
123 $self->background_job->assign_attributes(%{ $params });
125 my @errors = $self->background_job->validate;
128 flash('error', @errors);
129 $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
133 $self->background_job->update_next_run_at;
134 $self->background_job->save;
136 flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
139 $self->redirect_to($self->back_to);
142 sub load_background_job {
144 $self->background_job(SL::DB::BackgroundJob->new(id => $::form->{id})->load);
147 sub init_task_server {
148 return SL::System::TaskServer->new;
151 sub check_task_server {
153 flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
158 return $::form->{back_to} || $self->url_for(action => 'list');