CronExampleForm.php
7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* @file
* Contains \Drupal\cron_example\Form\CronExampleForm.
*/
namespace Drupal\cron_example\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\CronInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form with examples on how to use cron.
*/
class CronExampleForm extends ConfigFormBase {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The cron service.
*
* @var \Drupal\Core\CronInterface
*/
protected $cron;
/**
* The queue object.
*
* @var \Drupal\Core\Queue\QueueFactory
*/
protected $queue;
/**
* The state keyvalue collection.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* {@inheritdoc}
*/
public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $current_user, CronInterface $cron, QueueFactory $queue, StateInterface $state) {
parent::__construct($config_factory);
$this->currentUser = $current_user;
$this->cron = $cron;
$this->queue = $queue;
$this->state = $state;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('current_user'),
$container->get('cron'),
$container->get('queue'),
$container->get('state')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'cron_example';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->configFactory->get('examples.cron');
$form['status'] = [
'#type' => 'details',
'#title' => $this->t('Cron status information'),
'#open' => TRUE,
];
$form['status']['intro'] = [
'#type' => 'item',
'#markup' => $this->t('The cron example demonstrates hook_cron() and hook_queue_info() processing. If you have administrative privileges you can run cron from this page and see the results.'),
];
$next_execution = $config->get('next_execution');
$next_execution = !empty($next_execution) ? $next_execution : REQUEST_TIME;
$args = [
'%time' => date_iso8601($config->get('next_execution')),
'%seconds' => $next_execution - REQUEST_TIME,
];
$form['status']['last'] = [
'#type' => 'item',
'#markup' => $this->t('cron_example_cron() will next execute the first time cron runs after %time (%seconds seconds from now)', $args),
];
if ($this->currentUser->hasPermission('administer site configuration')) {
$form['cron_run'] = [
'#type' => 'details',
'#title' => $this->t('Run cron manually'),
'#open' => TRUE,
];
$form['cron_run']['cron_reset'] = [
'#type' => 'checkbox',
'#title' => $this->t('Run cron_example\'s cron regardless of whether interval has expired.'),
'#default_value' => FALSE,
];
$form['cron_run']['cron_trigger']['actions'] = ['#type' => 'actions'];
$form['cron_run']['cron_trigger']['actions']['sumbit'] = [
'#type' => 'submit',
'#value' => $this->t('Run cron now'),
'#submit' => [[$this, 'cronRun']],
];
}
$form['cron_queue_setup'] = [
'#type' => 'details',
'#title' => $this->t('Cron queue setup (for hook_cron_queue_info(), etc.)'),
'#open' => TRUE,
];
$queue_1 = $this->queue->get('cron_example_queue_1');
$queue_2 = $this->queue->get('cron_example_queue_2');
$args = [
'%queue_1' => $queue_1->numberOfItems(),
'%queue_2' => $queue_2->numberOfItems(),
];
$form['cron_queue_setup']['current_cron_queue_status'] = [
'#type' => 'item',
'#markup' => $this->t('There are currently %queue_1 items in queue 1 and %queue_2 items in queue 2', $args),
];
$form['cron_queue_setup']['num_items'] = [
'#type' => 'select',
'#title' => $this->t('Number of items to add to queue'),
'#options' => array_combine([1, 5, 10, 100, 1000], [1, 5, 10, 100, 1000]),
'#default_value' => 5,
];
$form['cron_queue_setup']['queue'] = [
'#type' => 'radios',
'#title' => $this->t('Queue to add items to'),
'#options' => [
'cron_example_queue_1' => $this->t('Queue 1'),
'cron_example_queue_2' => $this->t('Queue 2'),
],
'#default_value' => 'cron_example_queue_1',
];
$form['cron_queue_setup']['actions'] = ['#type' => 'actions'];
$form['cron_queue_setup']['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Add jobs to queue'),
'#submit' => [[$this, 'addItems']],
];
$form['configuration'] = [
'#type' => 'details',
'#title' => $this->t('Configuration of cron_example_cron()'),
'#open' => TRUE,
];
$form['configuration']['cron_example_interval'] = [
'#type' => 'select',
'#title' => $this->t('Cron interval'),
'#description' => $this->t('Time after which cron_example_cron will respond to a processing request.'),
'#default_value' => $config->get('interval'),
'#options' => [
60 => $this->t('1 minute'),
300 => $this->t('5 minutes'),
3600 => $this->t('1 hour'),
86400 => $this->t('1 day'),
],
];
return parent::buildForm($form, $form_state);
}
/**
* Allow user to directly execute cron, optionally forcing it.
*/
public function cronRun(array &$form, FormStateInterface &$form_state) {
$config = $this->configFactory->getEditable('examples.cron');
$cron_reset = $form_state->getValue('cron_reset');
if (!empty($cron_reset)) {
$config->set('next_execution', 0);
}
// Use a state variable to signal that cron was run manually from this form.
$this->state->set('cron_example_show_status_message', TRUE);
if ($this->cron->run()) {
drupal_set_message($this->t('Cron ran successfully.'));
}
else {
drupal_set_message($this->t('Cron run failed.'), 'error');
}
}
/**
* Add the items to the queue when signaled by the form.
*/
public function addItems(array &$form, FormStateInterface &$form_state) {
$values = $form_state->getValues();
$queue_name = $form['cron_queue_setup']['queue'][$values['queue']]['#title'];
$num_items = $form_state->getValue('num_items');
// Queues are defined by a QueueWorker Plugin which are selected by their
// id attritbute.
// @see \Drupal\cron_example\Plugin\QueueWorker\ReportWorkerOne
$queue = $this->queue->get($values['queue']);
for ($i = 1; $i <= $num_items; $i++) {
// Create a new item, a new data object, which is passed to the
// QueueWorker's processItem() method.
$item = new \stdClass();
$item->created = REQUEST_TIME;
$item->sequence = $i;
$queue->createItem($item);
}
$args = [
'%num' => $num_items,
'%queue' => $queue_name,
];
drupal_set_message($this->t('Added %num items to %queue', $args));
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Update the interval as stored in configuration. This will be read when
// this modules hook_cron function fires and will be used to ensure that
// action is taken only after the appropiate time has elapsed.
$this->configFactory->getEditable('examples.cron')
->set('interval', $form_state->getValue('cron_example_interval'))
->save();
parent::submitForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['examples.cron'];
}
}