Tell worker(s) to modify the rate limit for a task by type. See Also: :attr:`celery.app.task.Task.rate_limit`. Arguments: task_name (str): Type of task to set rate limit for. rate_limit (int, str): New rate limit.
(state, task_name, rate_limit, **kwargs)
| 257 | signature='<task_name> <rate_limit (e.g., 5/s | 5/m | 5/h)>', |
| 258 | ) |
| 259 | def rate_limit(state, task_name, rate_limit, **kwargs): |
| 260 | """Tell worker(s) to modify the rate limit for a task by type. |
| 261 | |
| 262 | See Also: |
| 263 | :attr:`celery.app.task.Task.rate_limit`. |
| 264 | |
| 265 | Arguments: |
| 266 | task_name (str): Type of task to set rate limit for. |
| 267 | rate_limit (int, str): New rate limit. |
| 268 | """ |
| 269 | # pylint: disable=redefined-outer-name |
| 270 | # XXX Note that this redefines `terminate`: |
| 271 | # Outside of this scope that is a function. |
| 272 | try: |
| 273 | rate(rate_limit) |
| 274 | except ValueError as exc: |
| 275 | return nok(f'Invalid rate limit string: {exc!r}') |
| 276 | |
| 277 | try: |
| 278 | state.app.tasks[task_name].rate_limit = rate_limit |
| 279 | except KeyError: |
| 280 | logger.error('Rate limit attempt for unknown task %s', |
| 281 | task_name, exc_info=True) |
| 282 | return nok('unknown task') |
| 283 | |
| 284 | state.consumer.reset_rate_limits() |
| 285 | |
| 286 | if not rate_limit: |
| 287 | logger.info('Rate limits disabled for tasks of type %s', task_name) |
| 288 | return ok('rate limit disabled successfully') |
| 289 | |
| 290 | logger.info('New rate limit for tasks of type %s: %s.', |
| 291 | task_name, rate_limit) |
| 292 | return ok('new rate limit set successfully') |
| 293 | |
| 294 | |
| 295 | @control_command( |