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->render('background_job/list',
 
  32                 title           => $::locale->text('Background jobs'),
 
  33                 BACKGROUND_JOBS => $self->models->get,
 
  34                 MODELS          => $self->models);
 
  40   $self->background_job(SL::DB::BackgroundJob->new(cron_spec => '* * * * *',  package_name => 'Test'));
 
  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->render('background_job/form',
 
  50                 title       => $::locale->text('Edit background job'),
 
  51                 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
 
  57   if ($::request->type eq 'json') {
 
  58     $self->render(\ SL::JSON::to_json($self->background_job->as_tree), { type => 'json' });
 
  67   $self->background_job(SL::DB::BackgroundJob->new);
 
  68   $self->create_or_update;
 
  73   $self->create_or_update;
 
  79   if (eval { $self->background_job->delete; 1; }) {
 
  80     flash_later('info',  $::locale->text('The background job has been deleted.'));
 
  82     flash_later('error', $::locale->text('The background job could not be destroyed.'));
 
  85   $self->redirect_to($self->back_to);
 
  88 sub action_save_and_execute {
 
  91   $self->background_job(SL::DB::BackgroundJob->new) if !$self->background_job;
 
  92   return unless $self->create_or_update;
 
  93   $self->action_execute;
 
  99   my $history = $self->background_job->run;
 
 100   if ($history->status eq 'success') {
 
 101     flash_later('info', $::locale->text('The background job was executed successfully.'));
 
 103     flash_later('error', $::locale->text('There was an error executing the background job.'));
 
 106   $self->redirect_to(controller => 'BackgroundJobHistory',
 
 109                      back_to    => $self->url_for(action => 'edit', id => $self->background_job->id));
 
 117   $::auth->assert('admin');
 
 124 sub create_or_update {
 
 127   my $is_new = !$self->background_job->id;
 
 128   my $params = delete($::form->{background_job}) || { };
 
 130   $self->background_job->assign_attributes(%{ $params });
 
 132   my @errors = $self->background_job->validate;
 
 135     flash('error', @errors);
 
 136     $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
 
 140   $self->background_job->update_next_run_at;
 
 141   $self->background_job->save;
 
 143   flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
 
 146   $self->redirect_to($self->back_to);
 
 149 sub load_background_job {
 
 151   $self->background_job(SL::DB::BackgroundJob->new(id => $::form->{id})->load);
 
 154 sub init_task_server {
 
 155   return SL::System::TaskServer->new;
 
 158 sub check_task_server {
 
 160   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
 
 165   return $::form->{back_to} || $self->url_for(action => 'list');
 
 169   SL::Controller::Helper::GetModels->new(
 
 173       package_name => t8('Package name'),
 
 174       type         => t8('Execution type'),
 
 175       active       => t8('Active'),
 
 176       cron_spec    => t8('Execution schedule'),
 
 177       last_run_at  => t8('Last run at'),
 
 178       next_run_at  => t8('Next run at'),
 
 181       package_name => [ SL::BackgroundJob::Base->get_known_job_classes ],