Skip to main content

Workers in PHP

The RoadRunner application server will launch multiple Temporal PHP worker processes based on provided .rr.yaml configuration.

Each worker might connect to one or multiple task queues. Worker poll Temporal service for tasks, performs those tasks, and communicates task execution results back to the Temporal service.

Worker code are developed, deployed, and operated by Temporal customers. To create a worker use Temporal\WorkerFactory:

use Temporal\WorkerFactory;
ini_set('display_errors', 'stderr');include "vendor/autoload.php";
// factory initiates and runs task queue specific activity and workflow workers$factory = WorkerFactory::create();
// Worker that listens on a task queue and hosts both workflow and activity implementations.$worker = $factory->newWorker();
// Workflows are stateful. So you need a type to create instances.$worker->registerWorkflowTypes(App\DemoWorkflow::class);
// Activities are stateless and thread safe. So a shared instance is used.$worker->registerActivityImplementations(new App\DemoActivity());// To register multiple Activities with the Worker, each Activity implementation name must be unique.// And you must provide all Activity function names in the registration call like so:// $worker->registerActivityImplementations(new App/ActivityA(), new App/ActivityB(), new App/ActivityC());
// start primary loop$factory->run();

You can configure task queue name using first argument of WorkerFactory->newWorker:

$worker = $factory->newWorker('my-task-queue');

As mentioned above you can create as many task queue connections inside a single worker as you need.

To configure additional worker options use Temporal\Worker\WorkerOptions:

use Temporal\Worker\WorkerOptions;
$worker = $factory->newWorker(    'my-task-queue',    WorkerOptions::new()        ->withMaxConcurrentWorkflowTaskPollers(10));

Make sure to point the worker file in application server configuration:

rpc:  listen: tcp://
server:  command: "php worker.php"
temporal:  address: "temporal:7233"  activities:    num_workers: 10

You can serve HTTP endpoints using the same server setup.

Get notified of updates