BackgroundJobs: "speichern und ausführen" für neue Jobs gefixt
[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::BackgroundJob::Base;
8 use SL::Controller::Helper::GetModels;
9 use SL::Controller::Helper::Paginated;
10 use SL::Controller::Helper::Sorted;
11 use SL::DB::BackgroundJob;
12 use SL::Helper::Flash;
13 use SL::Locale::String;
14 use SL::System::TaskServer;
15
16 use Rose::Object::MakeMethods::Generic
17 (
18   scalar                  => [ qw(background_job) ],
19   'scalar --get_set_init' => [ qw(task_server back_to) ],
20 );
21
22 __PACKAGE__->run_before('check_auth');
23 __PACKAGE__->run_before('check_task_server');
24 __PACKAGE__->run_before('load_background_job', only => [ qw(edit update destroy execute) ]);
25
26 __PACKAGE__->make_paginated(ONLY => [ qw(list) ]);
27
28 __PACKAGE__->make_sorted(
29   ONLY         => [ qw(list) ],
30
31   package_name => t8('Package name'),
32   type         => t8('Execution type'),
33   active       => t8('Active'),
34   cron_spec    => t8('Execution schedule'),
35   last_run_at  => t8('Last run at'),
36   next_run_at  => t8('Next run at'),
37 );
38
39 #
40 # actions
41 #
42
43 sub action_list {
44   my ($self) = @_;
45
46   $self->render('background_job/list',
47                 title           => $::locale->text('Background jobs'),
48                 BACKGROUND_JOBS => $self->get_models);
49 }
50
51 sub action_new {
52   my ($self) = @_;
53
54   $self->background_job(SL::DB::BackgroundJob->new(cron_spec => '* * * * *',  package_name => 'Test'));
55   $self->render('background_job/form',
56                 title       => $::locale->text('Create a new background job'),
57                 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
58 }
59
60 sub action_edit {
61   my ($self) = @_;
62
63   $self->render('background_job/form',
64                 title       => $::locale->text('Edit background job'),
65                 JOB_CLASSES => [ SL::BackgroundJob::Base->get_known_job_classes ]);
66 }
67
68 sub action_create {
69   my ($self) = @_;
70
71   $self->background_job(SL::DB::BackgroundJob->new);
72   $self->create_or_update;
73 }
74
75 sub action_update {
76   my ($self) = @_;
77   $self->create_or_update;
78 }
79
80 sub action_destroy {
81   my ($self) = @_;
82
83   if (eval { $self->background_job->delete; 1; }) {
84     flash_later('info',  $::locale->text('The background job has been deleted.'));
85   } else {
86     flash_later('error', $::locale->text('The background job could not be destroyed.'));
87   }
88
89   $self->redirect_to($self->back_to);
90 }
91
92 sub action_save_and_execute {
93   my ($self) = @_;
94
95   $self->background_job(SL::DB::BackgroundJob->new) if !$self->background_job;
96   return unless $self->create_or_update;
97   $self->action_execute;
98 }
99
100 sub action_execute {
101   my ($self) = @_;
102
103   my $history = $self->background_job->run;
104   if ($history->status eq 'success') {
105     flash_later('info', $::locale->text('The background job was executed successfully.'));
106   } else {
107     flash_later('error', $::locale->text('There was an error executing the background job.'));
108   }
109
110   $self->redirect_to(controller => 'BackgroundJobHistory',
111                      action     => 'show',
112                      id         => $history->id,
113                      back_to    => $self->url_for(action => 'edit', id => $self->background_job->id));
114 }
115
116 #
117 # filters
118 #
119
120 sub check_auth {
121   $::auth->assert('admin');
122 }
123
124 #
125 # helpers
126 #
127
128 sub create_or_update {
129   my $self   = shift;
130   my $return = shift;
131   my $is_new = !$self->background_job->id;
132   my $params = delete($::form->{background_job}) || { };
133
134   $self->background_job->assign_attributes(%{ $params });
135
136   my @errors = $self->background_job->validate;
137
138   if (@errors) {
139     flash('error', @errors);
140     $self->render('background_job/form', title => $is_new ? $::locale->text('Create a new background job') : $::locale->text('Edit background job'));
141     return;
142   }
143
144   $self->background_job->update_next_run_at;
145   $self->background_job->save;
146
147   flash_later('info', $is_new ? $::locale->text('The background job has been created.') : $::locale->text('The background job has been saved.'));
148   return if $return;
149
150   $self->redirect_to($self->back_to);
151 }
152
153 sub load_background_job {
154   my ($self) = @_;
155   $self->background_job(SL::DB::BackgroundJob->new(id => $::form->{id})->load);
156 }
157
158 sub init_task_server {
159   return SL::System::TaskServer->new;
160 }
161
162 sub check_task_server {
163   my ($self) = @_;
164   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
165 }
166
167 sub init_back_to {
168   my ($self) = @_;
169   return $::form->{back_to} || $self->url_for(action => 'list');
170 }
171
172 1;