Change GC worker to use data interface

This commit is contained in:
Joseph Schorr 2017-06-28 15:13:11 +03:00
parent 38f1752a2d
commit 420a5e5a3a
4 changed files with 45 additions and 15 deletions

View file

@ -1,13 +1,8 @@
import logging
from app import app
from data.model.repository import (find_repository_with_garbage, garbage_collect_repo,
get_random_gc_policy)
from data.database import UseThenDisconnect
from workers.gcworker.models_pre_oci import pre_oci_model as model
from workers.worker import Worker
logger = logging.getLogger(__name__)
class GarbageCollectionWorker(Worker):
def __init__(self):
super(GarbageCollectionWorker, self).__init__()
@ -16,14 +11,8 @@ class GarbageCollectionWorker(Worker):
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)
with UseThenDisconnect(app.config):
model.perform_garbage_collection()
if __name__ == "__main__":
worker = GarbageCollectionWorker()

View file

@ -0,0 +1,13 @@
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

View file

@ -0,0 +1,21 @@
import logging
from data.model.repository import (find_repository_with_garbage, garbage_collect_repo,
get_random_gc_policy)
from workers.gcworker.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()

View file

@ -0,0 +1,7 @@
from workers.gcworker.gcworker import GarbageCollectionWorker
from test.fixtures import *
def test_gc(initialized_db):
worker = GarbageCollectionWorker()
worker._garbage_collection_repos()