Configure Workers
Workers are background processes that run alongside your main application. They are typically used for processing long-running tasks, handling queues, or performing scheduled jobs.
Basic Worker Configuration
You can define workers in your config.cap.yaml under the workers section. Each worker needs a unique name and either a command or subcommand to execute.
Note
Learn more about subcommands in the application types documentation.
workers:
my-queue-processor:
command: "php artisan queue:work --queue=my_queue"
replicas: 1
resources: # Optional resource allocation for this worker
cpu: 250m
memory: 512Mi
| Property | Type | Description | Default |
|---|---|---|---|
replicas |
integer |
The number of instances of this worker to run. | 1 |
command |
string or array |
The full command to execute (e.g., php /app/script.php). See YAML Schema Reference. |
|
subcommand |
string or array |
The command to run using the application's framework binary (e.g., queue:work). See YAML Schema Reference. |
|
resources |
object |
Define CPU and memory resources for this worker. See YAML Schema Reference. | Defaults from application type |
Custom Base Image Required for Subcommands
The subcommand option generates a command with a custom binary that is only available within our base container. Only use this feature if you are using our base container.
RabbitMQ Autoscaling for Workers
CAP optionally supports autoscaling for workers based on RabbitMQ queue length. This allows your workers to scale up automatically when there's a backlog of messages and scale down when the queue is empty, optimizing resource usage.
Simple Autoscaling
A simple configuration is to just set scale: true on the worker object. This will automatically configure scaling based on the worker's name as the queue and a default scaling vector (e.g., scale up when more than 20 messages are in the queue).
workers:
runner:
scale: true
Advanced Autoscaling Configuration
You can customize the scaling behavior by providing more details in the scale block:
workers:
runner:
replicas: 2 # Minimum number of workers
scale:
enable: true
connection: "default" # Name of the RabbitMQ connection to use (defaults to 'default')
queue: "my-custom-queue" # Name of the queue to monitor for scaling (defaults to the worker's name)
vector: 5 # Determines when to scale workers based on queue length (base value 20)
maxReplicas: 10 # Maximum number of replicas for the scaler (defaults to 10)
Properties for scale:
| Property | Type | Description | Default |
|---|---|---|---|
enable |
boolean |
Whether to enable autoscaling for this worker. | false |
connection |
string |
The name of the RabbitMQ connection to use. | 'default' |
queue |
string |
The name of the RabbitMQ queue to monitor for scaling. | Worker's name |
vector |
integer |
A multiplier for the queue length to determine the target number of replicas. | 20 |
maxReplicas |
integer |
The maximum number of worker replicas that can be scaled up to. | 10 |
Note
The replicas property in the main worker configuration still defines the minimum number of workers that will always be running, even when autoscaling is enabled and the queue is empty.