1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\QueuedJobs\Controllers; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use SilverStripe\Admin\ModelAdmin; |
7
|
|
|
use SilverStripe\Control\HTTPResponse; |
8
|
|
|
use SilverStripe\Core\ClassInfo; |
9
|
|
|
use SilverStripe\Forms\DatetimeField; |
10
|
|
|
use SilverStripe\Forms\DropdownField; |
11
|
|
|
use SilverStripe\Forms\FieldList; |
12
|
|
|
use SilverStripe\Forms\Form; |
13
|
|
|
use SilverStripe\Forms\FormAction; |
14
|
|
|
use SilverStripe\Forms\GridField\GridField; |
15
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
16
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; |
17
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns; |
18
|
|
|
use SilverStripe\Forms\TextareaField; |
19
|
|
|
use SilverStripe\ORM\DataList; |
20
|
|
|
use SilverStripe\Security\Permission; |
21
|
|
|
use SilverStripe\Security\Security; |
22
|
|
|
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; |
23
|
|
|
use Symbiote\QueuedJobs\Forms\GridFieldQueuedJobExecute; |
24
|
|
|
use Symbiote\QueuedJobs\Services\AbstractQueuedJob; |
25
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJob; |
26
|
|
|
use Symbiote\QueuedJobs\Services\QueuedJobService; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @author Marcus Nyeholt <[email protected]> |
30
|
|
|
* @license BSD http://silverstripe.org/bsd-license/ |
31
|
|
|
*/ |
32
|
|
|
class QueuedJobsAdmin extends ModelAdmin |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private static $url_segment = 'queuedjobs'; |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private static $menu_title = 'Jobs'; |
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private static $menu_icon_class = 'font-icon-checklist'; |
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private static $managed_models = [ |
|
|
|
|
53
|
|
|
QueuedJobDescriptor::class |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
private static $dependencies = [ |
|
|
|
|
60
|
|
|
'jobQueue' => '%$' . QueuedJobService::class, |
61
|
|
|
]; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
private static $allowed_actions = [ |
|
|
|
|
67
|
|
|
'EditForm' |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* European date format |
72
|
|
|
* @var string |
73
|
|
|
*/ |
74
|
|
|
private static $date_format_european = 'dd/MM/yyyy'; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var QueuedJobService |
78
|
|
|
*/ |
79
|
|
|
public $jobQueue; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @config The number of seconds to include jobs that have finished |
83
|
|
|
* default: 300 (5 minutes), examples: 3600(1h), 86400(1d) |
84
|
|
|
*/ |
85
|
|
|
private static $max_finished_jobs_age = 300; |
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param int $id |
89
|
|
|
* @param FieldList $fields |
90
|
|
|
* @return Form |
91
|
|
|
*/ |
92
|
|
|
public function getEditForm($id = null, $fields = null) |
93
|
|
|
{ |
94
|
|
|
$form = parent::getEditForm($id, $fields); |
95
|
|
|
|
96
|
|
|
$filter = $this->jobQueue->getJobListFilter(null, self::config()->max_finished_jobs_age); |
97
|
|
|
|
98
|
|
|
$list = DataList::create(QueuedJobDescriptor::class); |
99
|
|
|
$list = $list->where($filter)->sort('Created', 'DESC'); |
100
|
|
|
|
101
|
|
|
$gridFieldConfig = GridFieldConfig_RecordEditor::create() |
102
|
|
|
->addComponent(new GridFieldQueuedJobExecute('execute')) |
103
|
|
|
->addComponent(new GridFieldQueuedJobExecute('pause', function ($record) { |
104
|
|
|
return $record->JobStatus == QueuedJob::STATUS_WAIT || $record->JobStatus == QueuedJob::STATUS_RUN; |
105
|
|
|
})) |
106
|
|
|
->addComponent(new GridFieldQueuedJobExecute('resume', function ($record) { |
107
|
|
|
return $record->JobStatus == QueuedJob::STATUS_PAUSED || $record->JobStatus == QueuedJob::STATUS_BROKEN; |
108
|
|
|
})) |
109
|
|
|
->removeComponentsByType(GridFieldAddNewButton::class); |
110
|
|
|
|
111
|
|
|
// Set messages to HTML display format |
112
|
|
|
$formatting = array( |
113
|
|
|
'Messages' => function ($val, $obj) { |
114
|
|
|
return "<div style='max-width: 300px; max-height: 200px; overflow: auto;'>$obj->Messages</div>"; |
115
|
|
|
}, |
116
|
|
|
); |
117
|
|
|
$gridFieldConfig->getComponentByType(GridFieldDataColumns::class) |
|
|
|
|
118
|
|
|
->setFieldFormatting($formatting); |
119
|
|
|
|
120
|
|
|
// Replace gridfield |
121
|
|
|
/** @skipUpgrade */ |
122
|
|
|
$grid = GridField::create( |
123
|
|
|
'QueuedJobDescriptor', |
124
|
|
|
_t(__CLASS__ . '.JobsFieldTitle', 'Jobs'), |
125
|
|
|
$list, |
126
|
|
|
$gridFieldConfig |
127
|
|
|
); |
128
|
|
|
$grid->setForm($form); |
129
|
|
|
/** @skipUpgrade */ |
130
|
|
|
$form->Fields()->replaceField($this->sanitiseClassName(QueuedJobDescriptor::class), $grid); |
131
|
|
|
|
132
|
|
|
if (Permission::check('ADMIN')) { |
133
|
|
|
$types = ClassInfo::subclassesFor(AbstractQueuedJob::class); |
134
|
|
|
$types = array_combine($types, $types); |
135
|
|
|
foreach ($types as $class) { |
136
|
|
|
$reflection = new ReflectionClass($class); |
137
|
|
|
if (!$reflection->isInstantiable()) { |
138
|
|
|
unset($types[$class]); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
$jobType = DropdownField::create('JobType', _t(__CLASS__ . '.CREATE_JOB_TYPE', 'Create job of type'), $types); |
142
|
|
|
$jobType->setEmptyString('(select job to create)'); |
143
|
|
|
$form->Fields()->push($jobType); |
144
|
|
|
|
145
|
|
|
$jobParams = TextareaField::create( |
146
|
|
|
'JobParams', |
147
|
|
|
_t(__CLASS__ . '.JOB_TYPE_PARAMS', 'Constructor parameters for job creation (one per line)') |
148
|
|
|
); |
149
|
|
|
$form->Fields()->push($jobParams); |
150
|
|
|
|
151
|
|
|
$form->Fields()->push( |
152
|
|
|
$dt = DatetimeField::create('JobStart', _t(__CLASS__ . '.START_JOB_TIME', 'Start job at')) |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$actions = $form->Actions(); |
156
|
|
|
$actions->push( |
157
|
|
|
FormAction::create('createjob', _t(__CLASS__ . '.CREATE_NEW_JOB', 'Create new job')) |
158
|
|
|
->addExtraClass('btn btn-primary') |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$this->extend('updateEditForm', $form); |
163
|
|
|
|
164
|
|
|
return $form; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
public function Tools() |
171
|
|
|
{ |
172
|
|
|
return ''; |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param array $data |
177
|
|
|
* @param Form $form |
178
|
|
|
* @return HTTPResponse |
179
|
|
|
*/ |
180
|
|
|
public function createjob($data, Form $form) |
181
|
|
|
{ |
182
|
|
|
if (Permission::check('ADMIN')) { |
183
|
|
|
$jobType = isset($data['JobType']) ? $data['JobType'] : ''; |
184
|
|
|
$params = isset($data['JobParams']) ? explode(PHP_EOL, $data['JobParams']) : array(); |
185
|
|
|
$time = isset($data['JobStart']) && is_array($data['JobStart']) ? implode(" ", $data['JobStart']) : null; |
186
|
|
|
|
187
|
|
|
// If the user has select the European date format as their setting then replace '/' with '-' in the date string so PHP |
188
|
|
|
// treats the date as this format. |
189
|
|
|
if (Security::getCurrentUser()->DateFormat == self::$date_format_european) { |
190
|
|
|
$time = str_replace('/', '-', $time); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if ($jobType && class_exists($jobType) && is_subclass_of($jobType, QueuedJob::class)) { |
|
|
|
|
194
|
|
|
$jobClass = new ReflectionClass($jobType); |
195
|
|
|
$job = $jobClass->newInstanceArgs($params); |
196
|
|
|
if ($this->jobQueue->queueJob($job, $time)) { |
197
|
|
|
$form->sessionMessage(_t(__CLASS__ . '.QueuedJobSuccess', 'Successfully queued job'), 'success'); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
return $this->redirectBack(); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|