Last active
August 29, 2015 14:06
-
-
Save bvanvugt/0b08b1108220c9aab398 to your computer and use it in GitHub Desktop.
Simple Task Monitoring in Redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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