celery_handler
Provides a concrete implementation of the
MerlinWorkerHandler for Celery.
This module defines the CeleryWorkerHandler class, which is responsible for launching,
stopping, and querying Celery-based worker processes. It supports additional options
such as echoing launch commands, overriding default worker arguments, and disabling logs.
CeleryWorkerHandler
Bases: MerlinWorkerHandler
Worker handler for launching and managing Celery-based Merlin workers.
This class implements the abstract methods defined in
MerlinWorkerHandler to provide
Celery-specific behavior, including launching workers with optional command-line overrides,
stopping workers, and querying their status.
Attributes:
| Name | Type | Description |
|---|---|---|
merlin_db |
MerlinDatabase
|
The database instance used for worker management. |
Methods:
| Name | Description |
|---|---|
start_workers |
Launch or echo Celery workers with optional arguments. |
stop_workers |
Attempt to stop active Celery workers. |
query_workers |
Return a basic summary of Celery worker status. |
Source code in merlin/workers/handlers/celery_handler.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
filter_workers(all_workers, filters)
Filter workers based on regex patterns or specific names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
all_workers
|
List[str]
|
List of all available workers. |
required |
filters
|
List[str]
|
List of regex patterns or specific names to filter workers. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: Filtered list of workers. |
Source code in merlin/workers/handlers/celery_handler.py
get_active_workers()
Retrieve a mapping of active workers to their associated queues for a Celery application.
This method constructs a dictionary where each key is a worker's name and the corresponding value is a list of queues that the worker is connected to. This allows for easy identification of which queues are being handled by each worker.
Returns:
| Type | Description |
|---|---|
Dict[str, List[str]]
|
A dictionary mapping active worker names to lists of queue names they are attached to. If no active workers are found, an empty dictionary is returned. |
Source code in merlin/workers/handlers/celery_handler.py
get_workers_from_app()
Retrieve a list of all workers connected to the Celery application.
This method uses the Celery control interface to inspect the current state of the application and returns a list of workers that are currently connected. If no workers are found, an empty list is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
The Celery application instance. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
A list of worker names that are currently connected to the Celery application. If no workers are connected, an empty list is returned. |
Source code in merlin/workers/handlers/celery_handler.py
get_workers_from_queues(queues)
Given a list of queue names, retrieve the Celery workers associated with those queues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queues
|
List[str]
|
The list of queue names to filter workers by. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: A list of Celery worker names associated with the specified queues. |
Source code in merlin/workers/handlers/celery_handler.py
normalize_queue_names(queues)
Normalize queue names to conform to Celery's naming conventions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queues
|
List[str]
|
List of queue names to normalize. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: Normalized queue names. |
Source code in merlin/workers/handlers/celery_handler.py
query_workers(formatter, queues=None, workers=None, local_db=False)
Query the status of Celery workers and display using the configured formatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formatter
|
str
|
The worker formatter to use (rich or json). |
required |
queues
|
List[str]
|
List of queue names to filter by (optional). |
None
|
workers
|
List[str]
|
List of worker names to filter by (optional). |
None
|
local_db
|
bool
|
Whether to use the local database for querying (optional). |
False
|
Source code in merlin/workers/handlers/celery_handler.py
send_shutdown_signal(workers_to_stop)
Send a shutdown signal to the specified workers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workers_to_stop
|
List[str]
|
List of worker names to send the shutdown signal to. |
required |
Source code in merlin/workers/handlers/celery_handler.py
start_workers(workers, **kwargs)
Launch or echo Celery workers with optional override behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workers
|
List[CeleryWorker]
|
Workers to launch. |
required |
**kwargs
|
|
{}
|
Source code in merlin/workers/handlers/celery_handler.py
stop_workers(queues=None, workers=None, dry_run=False)
Stop worker processes, optionally filtered by queue or worker name.
This method terminates active worker processes based on the provided filters. The behavior varies by implementation:
- If both
queuesandworkersare None, all active workers are stopped. - If
queuesis provided, only workers attached to those queues are stopped. - If
workersis provided, only workers matching those names/patterns are stopped. - If both are provided, workers must match both criteria (intersection).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
queues
|
List[str]
|
Optional list of queue names to filter workers by. |
None
|
workers
|
List[str]
|
Optional list of worker names or patterns to match. For Celery, these can be logical worker names from the spec or regex patterns matching physical worker names (e.g., "celery@worker1.*"). |
None
|
dry_run
|
bool
|
If True, just print out the names of the workers that will be stopped. |
False
|
Example
handler = CeleryWorkerHandler()
# Stop all workers
handler.stop_workers()
# Stop workers on specific queues
handler.stop_workers(queues=['hello_queue', 'world_queue'])
# Stop specific workers by name
handler.stop_workers(workers=['worker1', 'worker2'])
# Stop workers matching both criteria
handler.stop_workers(queues=['hello_queue'], workers=['worker1.*'])