Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple Task Monitoring in Redis
# See for details and documentation.
def start_task(task_type):
redis.HINCRBY('ACTIVE_TASKS', task_type, 1)
def stop_task(task_type):
with redis.MULTI:
redis.HSET('IDLE_SINCE', task_type, now())
redis.HINCRBY('ACTIVE_TASKS', task_type, -1)
def get_active_tasks(task_type):
return redis.HGET('ACTIVE_TASKS', task_type) or 0
def is_task_idle(task_type, idle_threshold_seconds):
with redis.MULTI:
active_tasks = redis.HGET('ACTIVE_TASKS', task_type) or 0
idle_since = redis.HGET('IDLE_SINCE', task_type) or 0
if active_tasks > 0:
return False
return (now() - idle_since) > idle_threshold_seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment