Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clean stale RQ jobs

Before:

redis 127.0.0.1:6379> DBSIZE
(integer) 1282306

After:

redis 127.0.0.1:6379> DBSIZE
(integer) 237694
import datetime
import redis
import pytz
from dateutil import parser
from itertools import product
r = redis.Redis()
last_month = (
datetime.datetime.utcnow().replace(tzinfo=pytz.utc) -
datetime.timedelta(days=30)
)
def clean_redis(prefix='rq:job:'):
keys = (
"".join(chars) for chars in product('0123456789abcdef', repeat=2)
)
for start in keys:
count = 0
prefix_keys = r.keys('{0}{1}*'.format(prefix, start))
for key in prefix_keys:
date = r.hget(key, 'created_at')
if date is None:
print(key)
continue
date = parser.parse(date)
if date < last_month:
r.delete(key)
count += 1
print(start, count)
if __name__ == '__main__':
clean_redis()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.