Skip to content

Commit

Permalink
BUG Fix jug cleanup for redis backend
Browse files Browse the repository at this point in the history
This was a big bug: the cleanup did not preserve the active tasks at all!

closes #86
  • Loading branch information
luispedro committed May 19, 2022
1 parent 87054ca commit e45e57b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Unreleased
* Fix `jug cleanup` for redis backend

Version 2.2.0 Tue 3 May 2022 by luispedro
* Add `jug pack` subcommand
* Remove six dependency
Expand Down
4 changes: 2 additions & 2 deletions jug/backends/dict_store.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#-*- coding: utf-8 -*-
# Copyright (C) 2009-2020, Luis Pedro Coelho <[email protected]>
# Copyright (C) 2009-2022, Luis Pedro Coelho <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -112,7 +112,7 @@ def cleanup(self, active, keeplocks=False):
existing = set(self.store.keys())
for act in active:
try:
existing.remove(_resultname(act))
existing.remove(_resultname(act.hash()))
except KeyError:
pass

Expand Down
20 changes: 7 additions & 13 deletions jug/backends/redis_store.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#-*- coding: utf-8 -*-
# Copyright (C) 2009-2020, Luis Pedro Coelho <[email protected]>
# Copyright (C) 2009-2022, Luis Pedro Coelho <[email protected]>
# vim: set ts=4 sts=4 sw=4 expandtab smartindent:
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -119,24 +119,18 @@ def cleanup(self, active, keeplocks=False):
Implement 'cleanup' command
'''
existing = list(self.list())
for act in active:
try:
existing.remove(_resultname(act.hash()))
except ValueError:
pass

if keeplocks:
for lock in self.listlocks():
try:
existing.remove(_lockname(lock))
except ValueError:
pass

existing = set(self.list())
existing -= set(act.hash() for act in active)

cleaned = len(existing)
for superflous in existing:
self.redis.delete(_resultname(superflous))

if not keeplocks:
cleaned += self.remove_locks()

return cleaned

def remove_locks(self):
Expand Down
28 changes: 28 additions & 0 deletions jug/tests/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,31 @@ def test_numpy_array_no_compress(tmpdir):
assert np.all(arr2 == arr)
store.remove(key)
store.close()

class MockHash:
def __init__(self, h):
self.h = h

def hash(self):
return self.h

def test_cleanup(store):
assert len(list(store.listlocks())) == 0
key1 = b'jugisbestthingever'
key2 = b'jug_key2'
ob1 = [1]
ob2 = [1, 2]
store.dump(ob1, key1)
store.dump(ob2, key2)

assert len(list(store.list())) == 2
assert store.cleanup([MockHash(key1), MockHash(key2)]) == 0
assert store.cleanup([MockHash(key1), MockHash(key2)], keeplocks=False) == 0

assert len(list(store.list())) == 2
assert store.cleanup([MockHash(key1)]) == 1
assert len(list(store.list())) == 1
assert store.cleanup([MockHash(key2)]) == 1
assert len(list(store.list())) == 0


0 comments on commit e45e57b

Please sign in to comment.