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) ]);
 
  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   $self->background_job(SL::DB::BackgroundJob->new);
 
  58   $self->create_or_update;
 
  63   $self->create_or_update;
 
  69   if (eval { $self->background_job->delete; 1; }) {
 
  70     flash_later('info',  $::locale->text('The background job has been deleted.'));
 
  72     flash_later('error', $::locale->text('The background job could not be destroyed.'));
 
  75   $self->redirect_to($self->back_to);
 
  78 sub action_save_and_execute {
 
  81   $self->background_job(SL::DB::BackgroundJob->new) if !$self->background_job;
 
  82   return unless $self->create_or_update;
 
  83   $self->action_execute;
 
  89   my $history = $self->background_job->run;
 
  90   if ($history->status eq 'success') {
 
  91     flash_later('info', $::locale->text('The background job was executed successfully.'));
 
  93     flash_later('error', $::locale->text('There was an error executing the background job.'));
 
  96   $self->redirect_to(controller => 'BackgroundJobHistory',
 
  99                      back_to    => $self->url_for(action => 'edit', id => $self->background_job->id));
 
 107   $::auth->assert('admin');
 
 114 sub create_or_update {
 
 117   my $is_new = !$self->background_job->id;
 
 118   my $params = delete($::form->{background_job}) || { };
 
 120   $self->background_job->assign_attributes(%{ $params });
 
 122   my @errors = $self->background_job->validate;
 
 125     flash('error', @errors);
 
 126     $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
 
 130   $self->background_job->update_next_run_at;
 
 131   $self->background_job->save;
 
 133   flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
 
 136   $self->redirect_to($self->back_to);
 
 139 sub load_background_job {
 
 141   $self->background_job(SL::DB::BackgroundJob->new(id => $::form->{id})->load);
 
 144 sub init_task_server {
 
 145   return SL::System::TaskServer->new;
 
 148 sub check_task_server {
 
 150   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
 
 155   return $::form->{back_to} || $self->url_for(action => 'list');
 
 159   SL::Controller::Helper::GetModels->new(
 
 163       package_name => t8('Package name'),
 
 164       type         => t8('Execution type'),
 
 165       active       => t8('Active'),
 
 166       cron_spec    => t8('Execution schedule'),
 
 167       last_run_at  => t8('Last run at'),
 
 168       next_run_at  => t8('Next run at'),