diff --git a/girder_worker/plugins/docker/__init__.py b/girder_worker/plugins/docker/__init__.py index bc6f62b8..85ad2a9d 100644 --- a/girder_worker/plugins/docker/__init__.py +++ b/girder_worker/plugins/docker/__init__.py @@ -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 @@ -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() diff --git a/girder_worker/plugins/docker/tests/docker_test.py b/girder_worker/plugins/docker/tests/docker_test.py index 80f26a80..8c44f6e2 100644 --- a/girder_worker/plugins/docker/tests/docker_test.py +++ b/girder_worker/plugins/docker/tests/docker_test.py @@ -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') diff --git a/girder_worker/tasks.py b/girder_worker/tasks.py index 0bd95934..d87439b6 100644 --- a/girder_worker/tasks.py +++ b/girder_worker/tasks.py @@ -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