1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

fix(worker): a database error skips a tick instead of ending the worker

Any error in the manager loop ended the worker at once with os._exit,
without the drain: a PostgreSQL restart or a SQLite write lock held past
its timeout cut every running migration, block copy and backup the
worker drove, and their jobs went to recovery, some ending uncertain. A
database error now skips that tick and drops the broken connection; the
next tick tries again. A lease lost meanwhile still ends the worker when
renew sees it, and other errors still end it as before.
This commit is contained in:
catborise 2026-11-06 09:02:54 +03:00
parent 30f5bf6535
commit b70b1aa877
2 changed files with 32 additions and 4 deletions

View file

@ -256,12 +256,33 @@ class WorkerTestCase(TransactionTestCase):
self.assertIn("no context", words(Operation.objects.get(pk=op.pk).error))
self.assertFalse(Claim.objects.filter(operation=op).exists())
def test_a_database_error_skips_a_tick(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
self.worker = Worker(data=directory.name, tick=0)
errors = [OperationalError("database is locked"), OperationalError("server closed the connection")]
def tick():
if errors:
raise errors.pop()
self.worker.stopping.set()
with (
patch.object(self.worker, "tick_once", side_effect=tick),
patch("operations.worker.signal.signal"),
patch("operations.worker.logging.shutdown"),
patch("operations.worker.os._exit", side_effect=SystemExit) as exit_,
self.assertLogs("operations.worker", "ERROR"),
):
with self.assertRaises(SystemExit):
self.worker.run()
exit_.assert_called_once_with(0)
def test_a_failing_manager_loop_ends_the_process(self):
directory = tempfile.TemporaryDirectory()
self.addCleanup(directory.cleanup)
self.worker = Worker(data=directory.name)
with (
patch.object(self.worker, "tick_once", side_effect=OperationalError("database is down")),
patch.object(self.worker, "tick_once", side_effect=KeyError("a bug")),
patch("operations.worker.signal.signal"),
patch("operations.worker.logging.shutdown"),
patch("operations.worker.os._exit", side_effect=SystemExit) as exit_,