57ec5c377bac1876bdcbbddacd12665037ca430b
[kivitendo-erp.git] / SL / Controller / BackgroundJob.pm
1 package SL::Controller::BackgroundJob;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::DB::BackgroundJob;
8 use SL::Helper::Flash;
9 use SL::System::TaskServer;
10
11 use Rose::Object::MakeMethods::Generic
12 (
13   scalar                  => [ qw(background_job) ],
14   'scalar --get_set_init' => [ qw(task_server) ],
15 );
16
17 __PACKAGE__->run_before('check_auth');
18 __PACKAGE__->run_before('check_task_server');
19 __PACKAGE__->run_before('load_background_job', only => [ qw(edit update destroy execute) ]);
20
21 #
22 # actions
23 #
24
25 sub action_list {
26   my ($self) = @_;
27
28   $self->render('background_job/list',
29                 title           => $::locale->text('Background jobs'),
30                 BACKGROUND_JOBS => SL::DB::Manager::BackgroundJob->get_all_sorted);
31 }
32
33 sub action_new {
34   my ($self) = @_;
35
36   $self->background_job(SL::DB::BackgroundJob->new(cron_spec => '* * * * *'));
37   $self->render('background_job/form', title => $::locale->text('Create a new background job'));
38 }
39
40 sub action_edit {
41   my ($self) = @_;
42   $self->render('background_job/form', title => $::locale->text('Edit background job'));
43 }
44
45 sub action_create {
46   my ($self) = @_;
47
48   $self->background_job(SL::DB::BackgroundJob->new);
49   $self->create_or_update;
50 }
51
52 sub action_update {
53   my ($self) = @_;
54   $self->create_or_update;
55 }
56
57 sub action_destroy {
58   my ($self) = @_;
59
60   if (eval { $self->background_job->delete; 1; }) {
61     flash_later('info',  $::locale->text('The background job has been deleted.'));
62   } else {
63     flash_later('error', $::locale->text('The background job could not be destroyed.'));
64   }
65
66   $self->redirect_to(action => 'list');
67 }
68
69 sub action_save_and_execute {
70   my ($self) = @_;
71
72   return unless $self->create_or_update;
73   $self->action_execute;
74 }
75
76 sub action_execute {
77   my ($self) = @_;
78
79   my $history = $self->background_job->run;
80   if ($history->status eq 'success') {
81     flash_later('info', $::locale->text('The background job was executed successfully.'));
82   } else {
83     flash_later('error', $::locale->text('There was an error executing the background job.'));
84   }
85
86   $self->redirect_to(controller => 'BackgroundJobHistory',
87                      action     => 'show',
88                      id         => $history->id,
89                      back_to    => $self->url_for(action => 'edit', id => $self->background_job->id));
90 }
91
92 #
93 # filters
94 #
95
96 sub check_auth {
97   $::auth->assert('admin');
98 }
99
100 #
101 # helpers
102 #
103
104 sub create_or_update {
105   my $self   = shift;
106   my $return = shift;
107   my $is_new = !$self->background_job->id;
108   my $params = delete($::form->{background_job}) || { };
109
110   $self->background_job->assign_attributes(%{ $params });
111
112   my @errors = $self->background_job->validate;
113
114   if (@errors) {
115     flash('error', @errors);
116     $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
117     return;
118   }
119
120   $self->background_job->update_next_run_at;
121   $self->background_job->save;
122
123   flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
124   return if $return;
125
126   $self->redirect_to(action => 'list');
127 }
128
129 sub load_background_job {
130   my ($self) = @_;
131   $self->background_job(SL::DB::BackgroundJob->new(id => $::form->{id})->load);
132 }
133
134 sub init_task_server {
135   return SL::System::TaskServer->new;
136 }
137
138 sub check_task_server {
139   my ($self) = @_;
140   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
141 }
142
143 1;