@@ -33,9 +33,9 @@ The `createJob()` function takes three arguments.
3333For 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
4141TableRegistry::getTableLocator()->get('Queue.QueuedJobs')
@@ -61,7 +61,7 @@ find the usage more easily, you can also use `<TaskClassName>::class` for `creat
6161``` php
6262use Queue\Queue\Task\EmailTask;
6363
64- $
this->QueuedJobs ->createJob(EmailTask::class, ['to' => '
[email protected] ', ...]);
64+ $
queuedJobsTable ->createJob(EmailTask::class, ['to' => '
[email protected] ', ...]);
6565```
6666This 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```
7777This 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);
140140foreach ($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
190190Get 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
195195echo $this->Number->toPercentage($progress, 0, ['multiply' => true]) . '%'; // Outputs 87% for example
0 commit comments