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', action => 'show', id => $history->id);
94 $::auth->assert('admin');
101 sub create_or_update {
104 my $is_new = !$self->background_job->id;
105 my $params = delete($::form->{background_job}) || { };
107 $self->background_job->assign_attributes(%{ $params });
109 my @errors = $self->background_job->validate;
112 flash('error', @errors);
113 $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
117 $self->background_job->update_next_run_at;
118 $self->background_job->save;
120 flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
123 $self->redirect_to(action => 'list');
126 sub load_background_job {
128 $self->background_job(SL::DB::BackgroundJob->new(id => $::form->{id})->load);
131 sub init_task_server {
132 return SL::System::TaskServer->new;
135 sub check_task_server {
137 flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;