You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+165Lines changed: 165 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -150,6 +150,171 @@ class SendWelcomeNotifications extends Task implements ShouldQueue
150
150
}
151
151
```
152
152
153
+
#### Setting a Specific Queue with `#[OnQueue]`
154
+
155
+
> [!WARNING]
156
+
> Do **not** declare `public string $queue = 'my-queue'` on a Task — this causes a PHP fatal error because Laravel's `Queueable` trait already declares `$queue` without a type hint.
157
+
158
+
Use the `#[OnQueue('queue-name')]` attribute to assign a specific queue to a Task or Process:
159
+
160
+
```php
161
+
use Brain\Attributes\OnQueue;
162
+
use Brain\Task;
163
+
use Illuminate\Contracts\Queue\ShouldQueue;
164
+
165
+
#[OnQueue('emails')]
166
+
class SendWelcomeNotifications extends Task implements ShouldQueue
167
+
{
168
+
public function handle(): self
169
+
{
170
+
// This task will run on the "emails" queue
171
+
return $this;
172
+
}
173
+
}
174
+
```
175
+
176
+
When applied to a **Process**, the attribute does two things:
177
+
178
+
1. The Process itself is dispatched to that queue (if it implements `ShouldQueue`)
179
+
2. All queued child tasks **inherit** the Process queue — unless the task defines its own `#[OnQueue]`
180
+
181
+
```php
182
+
use Brain\Attributes\OnQueue;
183
+
use Brain\Process;
184
+
use Illuminate\Contracts\Queue\ShouldQueue;
185
+
186
+
#[OnQueue('strava')]
187
+
class SyncActivitiesProcess extends Process implements ShouldQueue
188
+
{
189
+
protected array $tasks = [
190
+
FetchActivities::class, // ShouldQueue → runs on "strava" (inherited)
191
+
SaveActivities::class, // sync task → unaffected
192
+
NotifyUser::class, // has #[OnQueue('emails')] → runs on "emails" (own queue wins)
193
+
];
194
+
}
195
+
```
196
+
197
+
#### Queue Execution Flows
198
+
199
+
Below are the three most common queue configurations.
200
+
201
+
**1. Process + all Tasks on the same queue**
202
+
203
+
The process and every queued task run on `strava`:
204
+
205
+
```mermaid
206
+
flowchart LR
207
+
D((Dispatch)) --> Q[strava queue]
208
+
209
+
subgraph Q[strava queue]
210
+
direction LR
211
+
P[SyncActivitiesProcess] --> T1[FetchActivities]
212
+
T1 --> T2[TransformData]
213
+
T2 --> T3[SaveActivities]
214
+
end
215
+
216
+
style Q fill:#1a1a2e,stroke:#e94560,color:#eee
217
+
style P fill:#0f3460,stroke:#e94560,color:#eee
218
+
style T1 fill:#16213e,stroke:#0f3460,color:#eee
219
+
style T2 fill:#16213e,stroke:#0f3460,color:#eee
220
+
style T3 fill:#16213e,stroke:#0f3460,color:#eee
221
+
```
222
+
223
+
```php
224
+
#[OnQueue('strava')]
225
+
class SyncActivitiesProcess extends Process implements ShouldQueue
226
+
{
227
+
protected array $tasks = [
228
+
FetchActivities::class, // implements ShouldQueue
229
+
TransformData::class, // implements ShouldQueue
230
+
SaveActivities::class, // implements ShouldQueue
231
+
];
232
+
}
233
+
```
234
+
235
+
**2. Process on a queue, Tasks run synchronously inside it**
236
+
237
+
The process is queued, but internally its tasks run one after another in the same job:
0 commit comments