1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
from typing import TYPE_CHECKING, Optional
if TYPE_CHECKING:
from redis import Redis
from rq.worker import BaseWorker
WORKERS_SUSPENDED = 'rq:suspended'
def is_suspended(connection: 'Redis', worker: Optional['BaseWorker'] = None):
"""Checks whether a Worker is suspended on a given connection
PS: pipeline returns a list of responses
Ref: https://github.com/andymccurdy/redis-py#pipelines
Args:
connection (Redis): The Redis Connection
worker (Optional[Worker], optional): The Worker. Defaults to None.
"""
with connection.pipeline() as pipeline:
if worker is not None:
worker.heartbeat(pipeline=pipeline)
pipeline.exists(WORKERS_SUSPENDED)
return pipeline.execute()[-1]
def suspend(connection: 'Redis', ttl: Optional[int] = None):
"""
Suspends.
TTL of 0 will invalidate right away.
Args:
connection (Redis): The Redis connection to use..
ttl (Optional[int], optional): time to live in seconds. Defaults to `None`
"""
connection.set(WORKERS_SUSPENDED, 1)
if ttl is not None:
connection.expire(WORKERS_SUSPENDED, ttl)
def resume(connection: 'Redis'):
"""
Resumes.
Args:
connection (Redis): The Redis connection to use..
"""
return connection.delete(WORKERS_SUSPENDED)
|