diff --git a/conf/init/service/gcworker/run b/conf/init/service/gcworker/run index 6a843d4b8..d31f814a4 100755 --- a/conf/init/service/gcworker/run +++ b/conf/init/service/gcworker/run @@ -3,6 +3,6 @@ echo 'Starting GC worker' cd / -venv/bin/python -m workers.gcworker 2>&1 +venv/bin/python -m workers.gc.gcworker 2>&1 echo 'Repository GC exited' \ No newline at end of file diff --git a/workers/gc/__init__.py b/workers/gc/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/workers/gc/gcworker.py b/workers/gc/gcworker.py new file mode 100644 index 000000000..0fc769a13 --- /dev/null +++ b/workers/gc/gcworker.py @@ -0,0 +1,21 @@ +from app import app +from data.database import UseThenDisconnect +from workers.gc.models_pre_oci import pre_oci_model as model +from workers.worker import Worker + + +class GarbageCollectionWorker(Worker): + def __init__(self): + super(GarbageCollectionWorker, self).__init__() + self.add_operation(self._garbage_collection_repos, + app.config.get('GARBAGE_COLLECTION_FREQUENCY', 30)) + + def _garbage_collection_repos(self): + """ Performs garbage collection on repositories. """ + with UseThenDisconnect(app.config): + model.perform_garbage_collection() + + +if __name__ == "__main__": + worker = GarbageCollectionWorker() + worker.start() diff --git a/workers/gc/models_interface.py b/workers/gc/models_interface.py new file mode 100644 index 000000000..561d1ae0d --- /dev/null +++ b/workers/gc/models_interface.py @@ -0,0 +1,15 @@ +from abc import ABCMeta, abstractmethod + +from six import add_metaclass + + +@add_metaclass(ABCMeta) +class GCWorkerDataInterface(object): + """ + Interface that represents all data store interactions required by the GC worker. + """ + + @abstractmethod + def perform_garbage_collection(self): + """ Performs garbage collection on a single repository, if any can be found with garbage. """ + pass diff --git a/workers/gc/models_pre_oci.py b/workers/gc/models_pre_oci.py new file mode 100644 index 000000000..e672007e0 --- /dev/null +++ b/workers/gc/models_pre_oci.py @@ -0,0 +1,23 @@ +import logging + +from data.model.repository import ( + find_repository_with_garbage, garbage_collect_repo, get_random_gc_policy) + +from workers.gc.models_interface import GCWorkerDataInterface + +logger = logging.getLogger(__name__) + + +class PreOCIModel(GCWorkerDataInterface): + def perform_garbage_collection(self): + repository = find_repository_with_garbage(get_random_gc_policy()) + if repository is None: + logger.debug('No repository with garbage found') + return + + logger.debug('Starting GC of repository #%s (%s)', repository.id, repository.name) + garbage_collect_repo(repository) + logger.debug('Finished GC of repository #%s (%s)', repository.id, repository.name) + + +pre_oci_model = PreOCIModel() diff --git a/workers/gc/test/test_gcworker.py b/workers/gc/test/test_gcworker.py new file mode 100644 index 000000000..a153cf460 --- /dev/null +++ b/workers/gc/test/test_gcworker.py @@ -0,0 +1,8 @@ +from workers.gc.gcworker import GarbageCollectionWorker + +from test.fixtures import * + + +def test_gc(initialized_db): + worker = GarbageCollectionWorker() + worker._garbage_collection_repos() diff --git a/workers/gcworker.py b/workers/gcworker.py deleted file mode 100644 index e43bd2262..000000000 --- a/workers/gcworker.py +++ /dev/null @@ -1,30 +0,0 @@ -import logging - -from app import app -from data.model.repository import (find_repository_with_garbage, garbage_collect_repo, - get_random_gc_policy) - -from workers.worker import Worker - -logger = logging.getLogger(__name__) - -class GarbageCollectionWorker(Worker): - def __init__(self): - super(GarbageCollectionWorker, self).__init__() - self.add_operation(self._garbage_collection_repos, - app.config.get('GARBAGE_COLLECTION_FREQUENCY', 30)) - - def _garbage_collection_repos(self): - """ Performs garbage collection on repositories. """ - repository = find_repository_with_garbage(get_random_gc_policy()) - if repository is None: - logger.debug('No repository with garbage found') - return - - logger.debug('Starting GC of repository #%s (%s)', repository.id, repository.name) - garbage_collect_repo(repository) - logger.debug('Finished GC of repository #%s (%s)', repository.id, repository.name) - -if __name__ == "__main__": - worker = GarbageCollectionWorker() - worker.start()