Skip to content

Commit 22c682e

Browse files
committed
Docs.
1 parent 4e044e0 commit 22c682e

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

docs/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,42 @@ It will use your custom APP `QueueEmailTask` to send out emails via CLI.
209209

210210
Important: Do not forget to set your [domain](https://book.cakephp.org/3.0/en/core-libraries/email.html#sending-emails-from-cli) when sending from CLI.
211211

212+
### Avoid re-queuing
213+
214+
For some background-tasks you will want to make sure only a single instance of this type is currently run.
215+
In your logic you can check on this using `isQueued()` and a unique reference:
216+
```php
217+
/**
218+
* @return \Cake\Http\Response|null
219+
*/
220+
public function triggerImport()
221+
{
222+
$this->request->allowMethod('post');
223+
224+
$this->loadModel('Queue.QueuedJobs');
225+
if ($this->QueuedJobs->isQueued('my-import')) {
226+
$this->Flash->error('Job already running');
227+
228+
return $this->redirect($this->referer(['action' => 'index']));
229+
}
230+
231+
$this->QueuedJobs->createJob(
232+
'Execute',
233+
[
234+
'command' => 'bin/cake importer run',
235+
],
236+
['reference' => 'my-import', 'priority' => 2]
237+
);
238+
239+
$this->Flash->success('Job triggered, will only take few seconds :)');
240+
241+
return $this->redirect($this->referer(['action' => 'index']));
242+
}
243+
```
244+
So if someone clicks on the button again before the job is finished, he will not be able to trigger a new run:
245+
```php
246+
<?= $this->Form->postLink(__('Trigger Import'), ['action' => 'triggerImport'], ['confirm' => 'Sure?']) ?>
247+
```
212248

213249
### Updating status
214250

0 commit comments

Comments
 (0)