Skip to content

Commit

Permalink
Add rq.contrib.legacy.cleanup_ghosts() function, to clean up old mess.
Browse files Browse the repository at this point in the history
  • Loading branch information
nvie committed Feb 15, 2013
1 parent f1d3da8 commit 223e09f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
24 changes: 24 additions & 0 deletions rq/contrib/legacy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import logging
from rq import get_current_connection
from rq import Worker


logger = logging.getLogger(__name__)


def cleanup_ghosts():
"""
RQ versions < 0.3.6 suffered from a race condition where workers, when
abruptly terminated, did not have a chance to clean up their worker
registration, leading to reports of ghosted workers in `rqinfo`. Since
0.3.6, new worker registrations automatically expire, and the worker will
make sure to refresh the registrations as long as it's alive.
This function will clean up any of such legacy ghosted workers.
"""
conn = get_current_connection()
for worker in Worker.all():
if conn._ttl(worker.key) == -1:
ttl = worker.default_worker_ttl
conn.expire(worker.key, ttl)
logger.info('Marked ghosted worker {0} to expire in {1} seconds.'.format(worker.name, ttl))
3 changes: 3 additions & 0 deletions rq/scripts/rqworker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from rq import Queue, Worker
from rq.logutils import setup_loghandlers
from redis.exceptions import ConnectionError
from rq.contrib.legacy import cleanup_ghosts
from rq.scripts import add_standard_arguments
from rq.scripts import setup_redis
from rq.scripts import read_config_file
Expand Down Expand Up @@ -48,6 +49,8 @@ def main():
setup_loghandlers(args.verbose)
setup_redis(args)

cleanup_ghosts()

try:
queues = map(Queue, args.queues)
w = Worker(queues, name=args.name)
Expand Down

0 comments on commit 223e09f

Please sign in to comment.