1 package SL::Controller::BackgroundJob;
5 use parent qw(SL::Controller::Base);
7 use SL::DB::BackgroundJob;
9 use SL::System::TaskServer;
11 use Rose::Object::MakeMethods::Generic
13 scalar => [ qw(background_job) ],
14 'scalar --get_set_init' => [ qw(task_server) ],
17 __PACKAGE__->run_before('check_auth');
18 __PACKAGE__->run_before('check_task_server');
19 __PACKAGE__->run_before('load_background_job', only => [ qw(edit update destroy execute) ]);
28 $self->render('background_job/list',
29 title => $::locale->text('Background jobs'),
30 BACKGROUND_JOBS => SL::DB::Manager::BackgroundJob->get_all_sorted);
36 $self->background_job(SL::DB::BackgroundJob->new(cron_spec => '* * * * *'));
37 $self->render('background_job/form', title => $::locale->text('Create a new background job'));
42 $self->render('background_job/form', title => $::locale->text('Edit background job'));
48 $self->background_job(SL::DB::BackgroundJob->new);
49 $self->create_or_update;
54 $self->create_or_update;
60 if (eval { $self->background_job->delete; 1; }) {
61 flash_later('info', $::locale->text('The background job has been deleted.'));
63 flash_later('error', $::locale->text('The background job could not be destroyed.'));
66 $self->redirect_to(action => 'list');
69 sub action_save_and_execute {
72 return unless $self->create_or_update;
73 $self->action_execute;
79 my $history = $self->background_job->run;
80 if ($history->status eq 'success') {
81 flash_later('info', $::locale->text('The background job was executed successfully.'));
83 flash_later('error', $::locale->text('There was an error executing the background job.'));
86 $self->redirect_to(controller => 'BackgroundJobHistory',
89 back_to => $self->url_for(action => 'edit', id => $self->background_job->id));
97 $::auth->assert('admin');
104 sub create_or_update {
107 my $is_new = !$self->background_job->id;
108 my $params = delete($::form->{background_job}) || { };
110 $self->background_job->assign_attributes(%{ $params });
112 my @errors = $self->background_job->validate;
115 flash('error', @errors);
116 $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
120 $self->background_job->update_next_run_at;
121 $self->background_job->save;
123 flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
126 $self->redirect_to(action => 'list');
129 sub load_background_job {
131 $self->background_job(SL::DB::BackgroundJob->new(id => $::form->{id})->load);
134 sub init_task_server {
135 return SL::System::TaskServer->new;
138 sub check_task_server {
140 flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;