Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
[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', action => 'show', id => $history->id);
87 }
88
89 #
90 # filters
91 #
92
93 sub check_auth {
94   $::auth->assert('admin');
95 }
96
97 #
98 # helpers
99 #
100
101 sub create_or_update {
102   my $self   = shift;
103   my $return = shift;
104   my $is_new = !$self->background_job->id;
105   my $params = delete($::form->{background_job}) || { };
106
107   $self->background_job->assign_attributes(%{ $params });
108
109   my @errors = $self->background_job->validate;
110
111   if (@errors) {
112     flash('error', @errors);
113     $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
114     return;
115   }
116
117   $self->background_job->update_next_run_at;
118   $self->background_job->save;
119
120   flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
121   return if $return;
122
123   $self->redirect_to(action => 'list');
124 }
125
126 sub load_background_job {
127   my ($self) = @_;
128   $self->background_job(SL::DB::BackgroundJob->new(id => $::form->{id})->load);
129 }
130
131 sub init_task_server {
132   return SL::System::TaskServer->new;
133 }
134
135 sub check_task_server {
136   my ($self) = @_;
137   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
138 }
139
140 1;