Commit ffd52a09 authored by BAIRE Anthony's avatar BAIRE Anthony
Browse files

db checks on startup

- delete old 'replaced' versions
- push 'committed' versions
parent 2d52ceca
......@@ -1225,7 +1225,6 @@ class PushManager(Manager):
setattr(prev, key, getattr(version, key))
# mark this version as replaced
# TODO: purge this entry after some time
version.state = int(VersionState.replaced)
ses.add(version)
......@@ -1404,11 +1403,19 @@ class DockerController:
self.check_db()
def check_db(self):
log.debug("check_db")
def check_db(self, *, startup=False):
log.debug("check_db(startup=%r)", startup)
try:
ses = self.session
with ses.begin():
if startup:
ses.execute("DELETE FROM webapp_versions WHERE state=%d"
% VersionState.replaced)
for version_id, in ses.execute(
"SELECT id FROM webapp_versions WHERE state=%d"
% VersionState.committed).fetchall():
self.image_manager.push(version_id)
for webapp_id, in ses.execute("""SELECT webapps.id FROM webapps
LEFT JOIN webapp_versions ON webapps.id=webapp_versions.webapp_id
WHERE sandbox_state IN (%d,%d) OR state=%d
......@@ -1447,7 +1454,7 @@ class DockerController:
loop.add_signal_handler(signal.SIGINT, self.shutdown)
loop.add_reader(self.sock, self._sock_callback)
self.check_db()
self.check_db(startup=True)
try:
yield from self._shutdown_requested
finally:
......
......@@ -304,8 +304,8 @@ class ControllerTestCase(unittest.TestCase):
wrapper.__name__ = func.__name__
return wrapper
def notify(self):
self._loop.call_soon_threadsafe(self.ctrl.check_db)
def notify(self, *, startup=False):
self._loop.call_soon_threadsafe(lambda: self.ctrl.check_db(startup=startup))
@contextmanager
......@@ -425,6 +425,8 @@ class ControllerTestCase(unittest.TestCase):
if ver.state == V.ready:
docker_check_error(self.dk.push, image, number)
return ver
def start_sandbox(self, app):
......@@ -1427,6 +1429,34 @@ class ControllerTestCase(unittest.TestCase):
ensure_version_ready()
@with_db
def test_check_db_startup(self, ses, app):
with preamble():
committed = (
self.add_dummy_version(app, "1.0", state=V.committed).id,
self.add_dummy_version(app, "1.1", state=V.committed).id,
)
self.add_dummy_version(app, "2.0", state=V.ready)
self.add_dummy_version(app, "2.1", state=V.ready)
replaced = (
self.add_dummy_version(app, "3.0", state=V.replaced).id,
self.add_dummy_version(app, "3.1", state=V.replaced).id,
)
with mock.patch("controller.ImageManager.push") as m_push:
self.notify(startup=True)
time.sleep(.1)
for version_id in committed:
m_push.assert_any_call(version_id)
self.assertEqual(m_push.call_count, 2)
with ses.begin():
self.assertFalse(
ses.query(WebappVersion.id).filter(WebappVersion.id.in_(replaced)).all())
class ManagerTestCase(unittest.TestCase):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment