Skip to content

Commit 1c84005

Browse files
committed
Migration guide.
1 parent 722bafb commit 1c84005

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

docs/sections/queueing_jobs.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ to do this.
2525

2626
The `createJob()` function takes three arguments.
2727
- The first argument is the name of the type of job that you are creating.
28-
- The second argument is optional, but if set must be an array of data and will be passed as a parameter to the `run()` function of the worker.
29-
- The third argument is options (`'notBefore'`, `'priority'`, `'group'`).
28+
- The second argument is optional, but if set must be an array of data and will be passed as a payload parameter to the `run()` function of the worker.
29+
It can also be a (DTO) object that implements `CakeDto\Dto\FromArrayToArrayInterface` or provides `toArray()` method.
30+
- The third argument is options (`'notBefore'`, `'priority'`, `'group'`, `'reference'`). Either as array or `Queue\Config\JobConfig` class.
3031

3132
> `priority` is sorted ascending, therefore a task with priority 1 will be executed before a task with priority 5
3233
@@ -76,6 +77,44 @@ $queuedJobsTable->createJob($taskName, ['to' => '[email protected]', ...]);
7677
```
7778
This can be useful when more dynamically adding certain jobs of different types.
7879

80+
## Creating using objects
81+
*new in v8*
82+
83+
It allows a more speaking API, which already has maximum IDE and Stan support:
84+
```php
85+
$dataDto = OrderUpdateNotificationQueueDataDto::createFromArray([
86+
OrderUpdateNotificationQueueDataDto::FIELD_ORDER_ID => $order->id,
87+
OrderUpdateNotificationQueueDataDto::FIELD_MAILER => 'CustomerRequest',
88+
OrderUpdateNotificationQueueDataDto::FIELD_TYPE => 'request',
89+
]);
90+
$config = $queuedJobsTable->newConfig()
91+
->setPriority(2)
92+
->setReference('foo')
93+
->setNotBefore('+1 hour');
94+
$queuedJobsTable->createJob('OrderUpdateNotification', $dataDto, $config);
95+
```
96+
Such DTOs can easily be created using the [CakeDto plugin](https://github.com/dereuromark/cakephp-dto), e.g:
97+
```xml
98+
<dto name="EventUpdateNotificationQueueData" immutable="true">
99+
<field name="eventId" type="int" required="true"/>
100+
<field name="mailer" type="string" required="true"/>
101+
<field name="type" type="string" required="true"/>
102+
</dto>
103+
```
104+
105+
Inside your task you can then re-build it for a speaking API:
106+
```php
107+
public function run(array $data, int $jobId): void {
108+
$dataDto = OrderUpdateNotificationQueueDataDto::createFromArray($data);
109+
110+
$order = $this->fetchTable('Orders')->get($dataDto->getOrderId());
111+
$this->getMailer($queueData->getMailer())->send($dataDto->getType(), [$order]);
112+
}
113+
```
114+
No more magic strings and unclear associative arrays.
115+
116+
Note: Using speaking API is recommended but fully optional. You can still use arrays as usual.
117+
79118
## Running only specific tasks per worker
80119
You can filter "running" by group or even type:
81120
```

docs/sections/upgrading.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Upgrading from older versions
22

3-
## Coming from v6 to v7?
4-
- Run `bin/cake migrations migrate -p Queue` to migrate DB schema for table `queued_jbos` from `failed` to `attempts`.
5-
- The `config/app_queue.php` file is not loaded by default anymore. Either load it yourself or transfer the config to your `config/app_local.php`.
3+
## Coming from v7 to v8?
4+
- Make sure you ran `bin/cake migrations migrate -p Queue` to migrate DB schema for all previous migrations before upgrading to v8.
5+
- Once upgraded also run it once more, there should be now only 1 migration left.
6+
- Make sure you are not using PHP serialize anymore, it is now all JSON. It is also happening automatically behind the scenes, so remove your
7+
manual calls where they are not needed anymore.
8+
9+
Enjoy!

0 commit comments

Comments
 (0)