Skip to content

Commit 29cf191

Browse files
committed
Improve docs.
1 parent 5a1f127 commit 29cf191

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

docs/sections/custom_tasks.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class YourNameForItTask extends Task {
4141
* @return void
4242
*/
4343
public function run(array $data, int $jobId): void {
44-
$this->loadModel('FooBars');
45-
if (!$this->FooBars->doSth()) {
44+
$fooBarsTable = $this->fetchTable('FooBars');
45+
if (!$fooBarsTable->doSth()) {
4646
throw new RuntimeException('Couldnt do sth.');
4747
}
4848
}

docs/sections/misc.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,19 @@ From CLI you run this to rerun all of a specific job type at once:
3939
```
4040
bin/cake queue rerun FooBar
4141
```
42-
You can add an additional reference to rerun a specific job.
42+
You can add a reference to rerun a specific job.
4343

4444
## Using custom finder
4545
You can use a convenience finder for tasks that are still queued, that means not yet finished.
4646
```php
47-
$query = $this->QueuedJobs->find('queued')->...;
47+
$queuedJobsTable = $this->fetchTable('Queue.QueuedJobs');
48+
$query = $queuedJobsTable->find('queued')->...;
4849
```
4950
This includes also failed ones if not filtered further using `where()` conditions.
5051

5152
## Notes
5253

53-
`<TaskName>` is the complete class name without the Task suffix (eg. Example or PluginName.Example).
54+
`<TaskName>` is the complete class name without the Task suffix (e.g. Example or PluginName.Example).
5455

5556
Custom tasks should be placed in `src/Queue/Task/`.
5657
Tasks should be named `SomethingTask` and implement the Queue "Task".

docs/sections/queueing_jobs.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ The `createJob()` function takes three arguments.
3333
For sending emails, for example:
3434

3535
```php
36-
// In your controller
37-
$this->loadModel('Queue.QueuedJobs');
38-
$this->QueuedJobs->createJob('Queue.Email', ['to' => '[email protected]', ...]);
36+
// In your controller or where TableAwareTrait is used
37+
$queuedJobsTable = $this->fetchTable('Queue.QueuedJobs');
38+
$queuedJobsTable->createJob('Queue.Email', ['to' => '[email protected]', ...]);
3939

4040
// Somewhere in the model or lib
4141
TableRegistry::getTableLocator()->get('Queue.QueuedJobs')
@@ -61,7 +61,7 @@ find the usage more easily, you can also use `<TaskClassName>::class` for `creat
6161
```php
6262
use Queue\Queue\Task\EmailTask;
6363

64-
$this->QueuedJobs->createJob(EmailTask::class, ['to' => '[email protected]', ...]);
64+
$queuedJobsTable->createJob(EmailTask::class, ['to' => '[email protected]', ...]);
6565
```
6666
This does, however, require adding use statements for each such line on top.
6767

@@ -72,7 +72,7 @@ use Queue\Queue\Task\EmailTask;
7272

7373
$taskName = EmailTask::taskName();
7474

75-
$this->QueuedJobs->createJob($taskName, ['to' => '[email protected]', ...]);
75+
$queuedJobsTable->createJob($taskName, ['to' => '[email protected]', ...]);
7676
```
7777
This can be useful when more dynamically adding certain jobs of different types.
7878

@@ -98,14 +98,14 @@ In your logic you can check on this using `isQueued()` and a unique reference:
9898
public function triggerImport() {
9999
$this->request->allowMethod('post');
100100

101-
$this->loadModel('Queue.QueuedJobs');
102-
if ($this->QueuedJobs->isQueued('my-import', 'Queue.Execute')) {
101+
$queuedJobsTable = $this->fetchTable('Queue.QueuedJobs');
102+
if ($queuedJobsTable->isQueued('my-import', 'Queue.Execute')) {
103103
$this->Flash->error('Job already running');
104104

105105
return $this->redirect($this->referer(['action' => 'index']));
106106
}
107107

108-
$this->QueuedJobs->createJob(
108+
$queuedJobsTable->createJob(
109109
'Queue.Execute',
110110
['command' => 'bin/cake importer run'],
111111
['reference' => 'my-import', 'priority' => 2]
@@ -131,15 +131,15 @@ The `createJob()` method returns the entity. So you can store the ID and at any
131131

132132
```php
133133
// Within your regular web application
134-
$job = $this->QueuedJobs->createJob(...);
134+
$job = $queuedJobsTable->createJob(...);
135135
$id = $job->id;
136136
// Store
137137

138138
// Inside your Queue task, if you know the total records:
139139
$totalRecords = count($records);
140140
foreach ($records as $i => $record) {
141141
$this->processImageRendering($record);
142-
$this->QueuedJobs->updateProgress($id, ($i + 1) / $totalRecords);
142+
$queuedJobsTable->updateProgress($id, ($i + 1) / $totalRecords);
143143
}
144144
```
145145

@@ -151,35 +151,35 @@ class FooTask extends Task {
151151

152152
public function run(array $data, int $jobId): void {
153153
// Initializing
154-
$jobsTable = TableRegistry::getTableLocator()->get('Queue.QueuedJobs');
154+
$queuedJobsTable = TableRegistry::getTableLocator()->get('Queue.QueuedJobs');
155155
$foo = new Foo();
156156

157157
// Part one
158-
$jobsTable->updateAll(
158+
$queuedJobsTable->updateAll(
159159
['status' => 'Doing the first thing'],
160160
['id' => $jobId]
161161
);
162162
$foo->doFirstPartOfTask();
163-
$jobsTable->updateProgress($jobId, 33);
163+
$queuedJobsTable->updateProgress($jobId, 33);
164164

165165
// Part two
166-
$jobsTable->updateAll(
166+
$queuedJobsTable->updateAll(
167167
['status' => 'Doing the next thing'],
168168
['id' => $jobId]
169169
);
170170
$foo->doNextPartOfTask();
171-
$jobsTable->updateProgress($jobId, 66);
171+
$queuedJobsTable->updateProgress($jobId, 66);
172172

173173
// Part three
174-
$jobsTable->updateAll(
174+
$queuedJobsTable->updateAll(
175175
['status' => 'Doing the last thing'],
176176
['id' => $jobId]
177177
);
178178
$foo->doLastPartOfTask();
179-
$jobsTable->updateProgress($jobId, 100);
179+
$queuedJobsTable->updateProgress($jobId, 100);
180180

181181
// Done
182-
$jobsTable->updateAll(
182+
$queuedJobsTable->updateAll(
183183
['status' => 'Done doing things'],
184184
['id' => $jobId]
185185
);
@@ -189,7 +189,7 @@ class FooTask extends Task {
189189

190190
Get progress status in website and display:
191191
```php
192-
$job = $this->QueuedJobs->get($id);
192+
$job = $queuedJobsTable->get($id);
193193

194194
$progress = $job->progress; // A float from 0 to 1
195195
echo $this->Number->toPercentage($progress, 0, ['multiply' => true]) . '%'; // Outputs 87% for example

0 commit comments

Comments
 (0)