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::Controller::Helper::Paginated;
 
  10 use SL::Controller::Helper::Sorted;
 
  11 use SL::DB::BackgroundJob;
 
  12 use SL::Helper::Flash;
 
  13 use SL::Locale::String;
 
  14 use SL::System::TaskServer;
 
  16 use Rose::Object::MakeMethods::Generic
 
  18   scalar                  => [ qw(background_job) ],
 
  19   'scalar --get_set_init' => [ qw(task_server back_to) ],
 
  22 __PACKAGE__->run_before('check_auth');
 
  23 __PACKAGE__->run_before('check_task_server');
 
  24 __PACKAGE__->run_before('load_background_job', only => [ qw(edit update destroy execute) ]);
 
  26 __PACKAGE__->make_paginated(ONLY => [ qw(list) ]);
 
  28 __PACKAGE__->make_sorted(
 
  31   package_name => t8('Package name'),
 
  32   type         => t8('Execution type'),
 
  33   active       => t8('Active'),
 
  34   cron_spec    => t8('Execution schedule'),
 
  35   last_run_at  => t8('Last run at'),
 
  36   next_run_at  => t8('Next run at'),
 
  46   $self->render('background_job/list',
 
  47                 title           => $::locale->text('Background jobs'),
 
  48                 BACKGROUND_JOBS => $self->get_models);
 
  54   $self->background_job(SL::DB::BackgroundJob->new(cron_spec => '* * * * *',  package_name => 'Test'));
 
  55   $self->render('background_job/form',
 
  56                 title       => $::locale->text('Create a new background job'),
 
  57                 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
 
  63   $self->render('background_job/form',
 
  64                 title       => $::locale->text('Edit background job'),
 
  65                 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
 
  71   $self->background_job(SL::DB::BackgroundJob->new);
 
  72   $self->create_or_update;
 
  77   $self->create_or_update;
 
  83   if (eval { $self->background_job->delete; 1; }) {
 
  84     flash_later('info',  $::locale->text('The background job has been deleted.'));
 
  86     flash_later('error', $::locale->text('The background job could not be destroyed.'));
 
  89   $self->redirect_to($self->back_to);
 
  92 sub action_save_and_execute {
 
  95   return unless $self->create_or_update;
 
  96   $self->action_execute;
 
 102   my $history = $self->background_job->run;
 
 103   if ($history->status eq 'success') {
 
 104     flash_later('info', $::locale->text('The background job was executed successfully.'));
 
 106     flash_later('error', $::locale->text('There was an error executing the background job.'));
 
 109   $self->redirect_to(controller => 'BackgroundJobHistory',
 
 112                      back_to    => $self->url_for(action => 'edit', id => $self->background_job->id));
 
 120   $::auth->assert('admin');
 
 127 sub create_or_update {
 
 130   my $is_new = !$self->background_job->id;
 
 131   my $params = delete($::form->{background_job}) || { };
 
 133   $self->background_job->assign_attributes(%{ $params });
 
 135   my @errors = $self->background_job->validate;
 
 138     flash('error', @errors);
 
 139     $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
 
 143   $self->background_job->update_next_run_at;
 
 144   $self->background_job->save;
 
 146   flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
 
 149   $self->redirect_to($self->back_to);
 
 152 sub load_background_job {
 
 154   $self->background_job(SL::DB::BackgroundJob->new(id => $::form->{id})->load);
 
 157 sub init_task_server {
 
 158   return SL::System::TaskServer->new;
 
 161 sub check_task_server {
 
 163   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
 
 168   return $::form->{back_to} || $self->url_for(action => 'list');