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::System::TaskServer;
14 use Rose::Object::MakeMethods::Generic
16 scalar => [ qw(background_job) ],
17 'scalar --get_set_init' => [ qw(task_server back_to) ],
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) ]);
24 __PACKAGE__->make_paginated(ONLY => [ qw(list) ]);
26 __PACKAGE__->make_sorted(
29 package_name => $::locale->text('Package name'),
30 type => $::locale->text('Execution type'),
31 active => $::locale->text('Active'),
32 cron_spec => $::locale->text('Execution schedule'),
33 last_run_at => $::locale->text('Last run at'),
34 next_run_at => $::locale->text('Next run at'),
44 $self->render('background_job/list',
45 title => $::locale->text('Background jobs'),
46 BACKGROUND_JOBS => $self->get_models);
52 $self->background_job(SL::DB::BackgroundJob->new(cron_spec => '* * * * *'));
53 $self->render('background_job/form', title => $::locale->text('Create a new background job'));
58 $self->render('background_job/form', title => $::locale->text('Edit background job'));
64 $self->background_job(SL::DB::BackgroundJob->new);
65 $self->create_or_update;
70 $self->create_or_update;
76 if (eval { $self->background_job->delete; 1; }) {
77 flash_later('info', $::locale->text('The background job has been deleted.'));
79 flash_later('error', $::locale->text('The background job could not be destroyed.'));
82 $self->redirect_to($self->back_to);
85 sub action_save_and_execute {
88 return unless $self->create_or_update;
89 $self->action_execute;
95 my $history = $self->background_job->run;
96 if ($history->status eq 'success') {
97 flash_later('info', $::locale->text('The background job was executed successfully.'));
99 flash_later('error', $::locale->text('There was an error executing the background job.'));
102 $self->redirect_to(controller => 'BackgroundJobHistory',
105 back_to => $self->url_for(action => 'edit', id => $self->background_job->id));
113 $::auth->assert('admin');
120 sub create_or_update {
123 my $is_new = !$self->background_job->id;
124 my $params = delete($::form->{background_job}) || { };
126 $self->background_job->assign_attributes(%{ $params });
128 my @errors = $self->background_job->validate;
131 flash('error', @errors);
132 $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
136 $self->background_job->update_next_run_at;
137 $self->background_job->save;
139 flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
142 $self->redirect_to($self->back_to);
145 sub load_background_job {
147 $self->background_job(SL::DB::BackgroundJob->new(id => $::form->{id})->load);
150 sub init_task_server {
151 return SL::System::TaskServer->new;
154 sub check_task_server {
156 flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
161 return $::form->{back_to} || $self->url_for(action => 'list');