Merge pull request #2743 from coreos-inc/joseph.schorr/QUAY-663/gcworker-interface
Change GC worker to use new data interface style
This commit is contained in:
commit
a96555511b
7 changed files with 68 additions and 31 deletions
|
@ -3,6 +3,6 @@
|
||||||
echo 'Starting GC worker'
|
echo 'Starting GC worker'
|
||||||
|
|
||||||
cd /
|
cd /
|
||||||
venv/bin/python -m workers.gcworker 2>&1
|
venv/bin/python -m workers.gc.gcworker 2>&1
|
||||||
|
|
||||||
echo 'Repository GC exited'
|
echo 'Repository GC exited'
|
0
workers/gc/__init__.py
Normal file
0
workers/gc/__init__.py
Normal file
21
workers/gc/gcworker.py
Normal file
21
workers/gc/gcworker.py
Normal file
|
@ -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()
|
15
workers/gc/models_interface.py
Normal file
15
workers/gc/models_interface.py
Normal file
|
@ -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
|
23
workers/gc/models_pre_oci.py
Normal file
23
workers/gc/models_pre_oci.py
Normal file
|
@ -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()
|
8
workers/gc/test/test_gcworker.py
Normal file
8
workers/gc/test/test_gcworker.py
Normal file
|
@ -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()
|
|
@ -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()
|
|
Reference in a new issue