1 package SL::Controller::BackgroundJob;
 
   5 use parent qw(SL::Controller::Base);
 
   7 use SL::Controller::Helper::GetModels;
 
   8 use SL::Controller::Helper::Paginated;
 
   9 use SL::Controller::Helper::Sorted;
 
  10 use SL::DB::BackgroundJob;
 
  11 use SL::Helper::Flash;
 
  12 use SL::Locale::String;
 
  13 use SL::System::TaskServer;
 
  15 use Rose::Object::MakeMethods::Generic
 
  17   scalar                  => [ qw(background_job) ],
 
  18   'scalar --get_set_init' => [ qw(task_server back_to) ],
 
  21 __PACKAGE__->run_before('check_auth');
 
  22 __PACKAGE__->run_before('check_task_server');
 
  23 __PACKAGE__->run_before('load_background_job', only => [ qw(edit update destroy execute) ]);
 
  25 __PACKAGE__->make_paginated(ONLY => [ qw(list) ]);
 
  27 __PACKAGE__->make_sorted(
 
  30   package_name => t8('Package name'),
 
  31   type         => t8('Execution type'),
 
  32   active       => t8('Active'),
 
  33   cron_spec    => t8('Execution schedule'),
 
  34   last_run_at  => t8('Last run at'),
 
  35   next_run_at  => t8('Next run at'),
 
  45   $self->render('background_job/list',
 
  46                 title           => $::locale->text('Background jobs'),
 
  47                 BACKGROUND_JOBS => $self->get_models);
 
  53   $self->background_job(SL::DB::BackgroundJob->new(cron_spec => '* * * * *'));
 
  54   $self->render('background_job/form', title => $::locale->text('Create a new background job'));
 
  59   $self->render('background_job/form', title => $::locale->text('Edit background job'));
 
  65   $self->background_job(SL::DB::BackgroundJob->new);
 
  66   $self->create_or_update;
 
  71   $self->create_or_update;
 
  77   if (eval { $self->background_job->delete; 1; }) {
 
  78     flash_later('info',  $::locale->text('The background job has been deleted.'));
 
  80     flash_later('error', $::locale->text('The background job could not be destroyed.'));
 
  83   $self->redirect_to($self->back_to);
 
  86 sub action_save_and_execute {
 
  89   return unless $self->create_or_update;
 
  90   $self->action_execute;
 
  96   my $history = $self->background_job->run;
 
  97   if ($history->status eq 'success') {
 
  98     flash_later('info', $::locale->text('The background job was executed successfully.'));
 
 100     flash_later('error', $::locale->text('There was an error executing the background job.'));
 
 103   $self->redirect_to(controller => 'BackgroundJobHistory',
 
 106                      back_to    => $self->url_for(action => 'edit', id => $self->background_job->id));
 
 114   $::auth->assert('admin');
 
 121 sub create_or_update {
 
 124   my $is_new = !$self->background_job->id;
 
 125   my $params = delete($::form->{background_job}) || { };
 
 127   $self->background_job->assign_attributes(%{ $params });
 
 129   my @errors = $self->background_job->validate;
 
 132     flash('error', @errors);
 
 133     $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
 
 137   $self->background_job->update_next_run_at;
 
 138   $self->background_job->save;
 
 140   flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
 
 143   $self->redirect_to($self->back_to);
 
 146 sub load_background_job {
 
 148   $self->background_job(SL::DB::BackgroundJob->new(id => $::form->{id})->load);
 
 151 sub init_task_server {
 
 152   return SL::System::TaskServer->new;
 
 155 sub check_task_server {
 
 157   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
 
 162   return $::form->{back_to} || $self->url_for(action => 'list');