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;
 
  12 use SL::Locale::String;
 
  13 use SL::System::TaskServer;
 
  15 use Rose::Object::MakeMethods::Generic
 
  17   'scalar --get_set_init' => [ qw(task_server back_to models background_job) ],
 
  20 __PACKAGE__->run_before('check_auth');
 
  21 __PACKAGE__->run_before('check_task_server');
 
  30   $self->setup_list_action_bar;
 
  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')) unless $self->background_job;
 
  41   $self->setup_form_action_bar;
 
  42   $self->render('background_job/form',
 
  43                 title       => $::locale->text('Create a new background job'),
 
  44                 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
 
  50   $self->setup_form_action_bar;
 
  51   $self->render('background_job/form',
 
  52                 title       => $::locale->text('Edit background job'),
 
  53                 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
 
  56 sub action_edit_as_new {
 
  59   delete $::form->{background_job}->{id};
 
  60   $self->background_job(SL::DB::BackgroundJob->new(%{ $::form->{background_job} }));
 
  67   if ($::request->type eq 'json') {
 
  68     $self->render(\ SL::JSON::to_json($self->background_job->as_tree), { type => 'json' });
 
  77   $self->background_job(SL::DB::BackgroundJob->new);
 
  78   $self->create_or_update;
 
  83   $self->create_or_update;
 
  89   if (eval { $self->background_job->delete; 1; }) {
 
  90     flash_later('info',  $::locale->text('The background job has been deleted.'));
 
  92     flash_later('error', $::locale->text('The background job could not be destroyed.'));
 
  95   $self->redirect_to($self->back_to);
 
  98 sub action_save_and_execute {
 
 101   $self->background_job(SL::DB::BackgroundJob->new) if !$self->background_job;
 
 102   return unless $self->create_or_update;
 
 103   $self->action_execute;
 
 109   my $history = $self->background_job->run;
 
 110   if ($history->status eq 'success') {
 
 111     flash_later('info', $::locale->text('The background job was executed successfully.'));
 
 113     flash_later('error', $::locale->text('There was an error executing the background job.'));
 
 116   $self->redirect_to(controller => 'BackgroundJobHistory',
 
 119                      back_to    => $self->url_for(action => 'edit', id => $self->background_job->id));
 
 122 sub action_execute_class {
 
 128     die "no class name given in parameter 'class'" if !$::form->{class} || ($::form->{class} =~ m{[^a-z0-9]}i);
 
 129     die "invalid class"                            if ! -f "SL/BackgroundJob/" . $::form->{class} . ".pm";
 
 131     my $package = "SL::BackgroundJob::" . $::form->{class};
 
 133     eval "require $package" or die $@;
 
 134     $result = $package->new->run(SL::DB::BackgroundJob->new);
 
 140     status => $ok ? 'succeeded' : 'failed',
 
 141     result => $ok ? $result     : $@,
 
 144   $self->render(\to_json($reply), { type => 'json' });
 
 152   $::auth->assert('admin');
 
 159 sub create_or_update {
 
 162   my $is_new = !$self->background_job->id;
 
 163   my $params = delete($::form->{background_job}) || { };
 
 165   $self->background_job->assign_attributes(%{ $params });
 
 167   my @errors = $self->background_job->validate;
 
 170     flash('error', @errors);
 
 171     $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
 
 175   $self->background_job->update_next_run_at;
 
 176   $self->background_job->save;
 
 178   flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
 
 181   $self->redirect_to($self->back_to);
 
 184 sub init_background_job {
 
 185   return $::form->{id} ? SL::DB::BackgroundJob->new(id => $::form->{id})->load : undef;
 
 188 sub init_task_server {
 
 189   return SL::System::TaskServer->new;
 
 192 sub check_task_server {
 
 194   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
 
 199   return $::form->{back_to} || $self->url_for(action => 'list');
 
 203   SL::Controller::Helper::GetModels->new(
 
 207       package_name => t8('Package name'),
 
 208       type         => t8('Execution type'),
 
 209       active       => t8('Active'),
 
 210       cron_spec    => t8('Execution schedule'),
 
 211       last_run_at  => t8('Last run at'),
 
 212       next_run_at  => t8('Next run at'),
 
 215       package_name => [ SL::BackgroundJob::Base->get_known_job_classes ],
 
 220 sub setup_list_action_bar {
 
 223   for my $bar ($::request->layout->get('actionbar')) {
 
 227         link      => $self->url_for(action => 'new'),
 
 228         accesskey => 'enter',
 
 231         t8('Server control'),
 
 232         link => $self->url_for(controller => 'TaskServer', action => 'show'),
 
 236         link => $self->url_for(controller => 'BackgroundJobHistory', action => 'list'),
 
 242 sub setup_form_action_bar {
 
 245   my $is_new = !$self->background_job->id;
 
 247   for my $bar ($::request->layout->get('actionbar')) {
 
 252           submit    => [ '#form', { action => 'BackgroundJob/' . ($is_new ? 'create' : 'update') } ],
 
 253           accesskey => 'enter',
 
 256           t8('Save and execute'),
 
 257           submit => [ '#form', { action => 'BackgroundJob/save_and_execute' } ],
 
 261           submit   => [ '#form', { action => 'BackgroundJob/edit_as_new' } ],
 
 262           disabled => $is_new ? t8('The object has not been saved yet.') : undef,
 
 264       ], # end of combobox "Save"
 
 268         submit   => [ '#form', { action => 'BackgroundJob/destroy' } ],
 
 269         confirm  => t8('Do you really want to delete this object?'),
 
 270         disabled => $is_new ? t8('This object has not been saved yet.') : undef,
 
 275         link => $self->url_for(action => 'list'),
 
 280         link     => $self->url_for(controller => 'BackgroundJobHistory', action => 'list', 'filter.package_name:substr::ilike' => $self->background_job->package_name),
 
 281         disabled => $is_new ? t8('This object has not been saved yet.') : undef,