Appearance
Tasks
Table of Contents
- Introduction
- Task vs QueuableTask
- Generating Tasks
- Writing Tasks
- Executing Tasks Immediately
- Using QueuableTask
- Error and Cancellation Behavior
- Result API
- CLI Reference
- Task API Reference
Introduction
Phenix tasks are designed to run in a separate worker process (parallel execution).
A standard task (Task) is usually executed directly from your code and returns its Result immediately.
A queuable task (QueuableTask) is pushed to the queue system and processed later by queue workers. When you dispatch it to queue, you do not receive immediate output in the caller.
Task vs QueuableTask
Phenix\Tasks\Task- Use when you want to execute work now and get output now.
- Typical call:
$result = $task->output();
Phenix\Tasks\QueuableTask- Extends
Taskand adds queue metadata + dispatch helpers. - Typical call:
MyTask::dispatch(); - Output is not returned to the caller at dispatch time.
- Extends
For full queue runtime details, see Queues guide.
Generating Tasks
Generate a Standard Task
sh
php phenix make:task ResizeImageTaskWith force:
sh
php phenix make:task ResizeImageTask --force
php phenix make:task ResizeImageTask -fNested namespace path:
sh
php phenix make:task Media/ResizeImageTaskGenerate a Queuable Task
sh
php phenix make:task SendWelcomeEmailTask --queueThis generates a class extending QueuableTask instead of Task.
Writing Tasks
Task Class Example
php
<?php
declare(strict_types=1);
namespace App\Tasks;
use Amp\Cancellation;
use Amp\Sync\Channel;
use Phenix\Tasks\Result;
use Phenix\Tasks\Task;
class ResizeImageTask extends Task
{
protected function handle(Channel $channel, Cancellation $cancellation): Result
{
// Task logic...
return Result::success(
output: 'Image resized successfully',
message: 'Resize operation completed'
);
}
}Returning Results
Task handlers should return a Phenix\Tasks\Result object:
php
return Result::success(output: $data, message: 'Done');
return Result::failure(message: 'Something went wrong');Executing Tasks Immediately
Run a Single Task
php
use App\Tasks\ResizeImageTask;
$task = new ResizeImageTask();
$result = $task->output();
if ($result->isSuccess()) {
$output = $result->output();
}Set Timeout
Task has a default timeout of 60 seconds.
php
$task = new ResizeImageTask();
$task->setTimeout(10);
$result = $task->output();Run Multiple Tasks in Parallel
Use Phenix\Tasks\WorkerPool::awaitAll():
php
use App\Tasks\ResizeImageTask;
use Phenix\Tasks\WorkerPool;
$results = WorkerPool::awaitAll([
new ResizeImageTask(),
new ResizeImageTask(),
]);
foreach ($results as $result) {
if ($result->isFailure()) {
// Handle failure
}
}Using QueuableTask
For dispatching and processing queuable tasks, see the Queues guide.
Queuable tasks are processed asynchronously by queue workers and do not return immediate output at dispatch time.
Error and Cancellation Behavior
- If cancellation happens (for example timeout), task execution returns
Result::failure(...). - If any exception is thrown during task execution, it is reported and converted to
Result::failure(...).
Result API
Phenix\Tasks\Result provides:
output(): mixedmessage(): ?stringisSuccess(): boolisFailure(): boolResult::success(mixed $output = null, ?string $message = null)Result::failure(mixed $output = null, ?string $message = null)
CLI Reference
sh
php phenix make:task ResizeImageTask
php phenix make:task ResizeImageTask --queue
php phenix make:task ResizeImageTask --force
php phenix make:task ResizeImageTask -f
php phenix make:task Media/ResizeImageTaskTask API Reference
Task
output(): ResultsetTimeout(int $timeout): voidgetTimeout(): intTask::setBootingSettings(): void
QueuableTask
Queue metadata:
setConnectionName(string $connectionName): voidgetConnectionName(): ?stringsetQueueName(string $queueName): voidgetQueueName(): ?stringgetTaskId(): ?stringsetTaskId(string $taskId): voidgetAttempts(): intsetAttempts(int $attempts): voidgetMaxTries(): ?intgetPayload(): string
Dispatch helpers:
enqueue(mixed ...$args): PendingTaskenqueueIf(Closure|bool $condition, mixed ...$args): ?PendingTaskdispatch(mixed ...$args): voiddispatchIf(Closure|bool $condition, mixed ...$args): void