Skip to content

Instantly share code, notes, and snippets.

@bvanvugt
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvanvugt/0b08b1108220c9aab398 to your computer and use it in GitHub Desktop.
Save bvanvugt/0b08b1108220c9aab398 to your computer and use it in GitHub Desktop.
Simple Task Monitoring in Redis
# See http://blog.sendwithus.com/task-monitoring-using-redis/ 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