Skip to content

Commit

Permalink
Run cleanup task in celery's after_return handler
Browse files Browse the repository at this point in the history
  • Loading branch information
zachmullen committed Feb 10, 2017
1 parent 13cec40 commit e2adf93
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
11 changes: 11 additions & 0 deletions girder_worker/plugins/docker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
import shutil
import subprocess
import tempfile
import time
from girder_worker import config

# Minimum interval in seconds at which to run the docker-gc script
MIN_GC_INTERVAL = 600


def before_run(e):
import executor
Expand All @@ -28,6 +32,13 @@ def docker_gc(e):
the same directory as this file. After that, deletes all images that are
no longer used by any containers.
"""
stampfile = os.path.join(config.get('girder_worker', 'tmp_root'), '.dockergcstamp')
if os.path.exists(stampfile) and time.time() - os.path.getmtime(stampfile) < MIN_GC_INTERVAL:
return
else: # touch the file
with open(stampfile, 'w') as f:
f.write('')

print('Garbage collecting docker containers and images.')
gc_dir = tempfile.mkdtemp()

Expand Down
1 change: 1 addition & 0 deletions girder_worker/plugins/docker/tests/docker_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def fetchMock(url, request):

@mock.patch('subprocess.Popen')
def testCleanupHook(self, mockPopen):
os.mkdir(_tmp)
mockPopen.return_value = processMock
girder_worker.config.set('docker', 'cache_timeout', '123456')
girder_worker.config.set('docker', 'exclude_images', 'test/test:latest')
Expand Down
6 changes: 5 additions & 1 deletion girder_worker/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
from .app import app


@app.task(name='girder_worker.run')
def _cleanup():
core.events.trigger('cleanup')


@app.task(name='girder_worker.run', after_return=_cleanup)
def run(*pargs, **kwargs):
jobInfo = kwargs.pop('jobInfo', {})
retval = 0
Expand Down

0 comments on commit e2adf93

Please sign in to comment.