Phase 24: Shared operation lock
This commit is contained in:
@@ -0,0 +1,35 @@
|
|||||||
|
"""Canonical shared lock and state bindings for the dev.3 composition root."""
|
||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from core.approval_store import ApprovalStore
|
||||||
|
from core.backup_evidence import BackupEvidence
|
||||||
|
from core.locking import DeploymentLock
|
||||||
|
from core.operation_guard import OperationGuard
|
||||||
|
from core.transactions import TransactionJournal
|
||||||
|
|
||||||
|
|
||||||
|
class SiteOperations:
|
||||||
|
def __init__(self, root):
|
||||||
|
root = Path(root)
|
||||||
|
if root.resolve() != root:
|
||||||
|
raise ValueError("Site runtime directory must be canonical")
|
||||||
|
state = root / "state"
|
||||||
|
approvals = root / "approvals"
|
||||||
|
state.mkdir(mode=0o700, exist_ok=True)
|
||||||
|
approvals.mkdir(mode=0o700, exist_ok=True)
|
||||||
|
self.lock_path = state / ".deployment.lock"
|
||||||
|
self.journal = TransactionJournal(state)
|
||||||
|
self.backup_evidence = BackupEvidence(state)
|
||||||
|
self.approvals = ApprovalStore(approvals)
|
||||||
|
self.guard = OperationGuard(self.approvals)
|
||||||
|
|
||||||
|
def common(self):
|
||||||
|
return {
|
||||||
|
"lock_path": self.lock_path,
|
||||||
|
"journal": self.journal,
|
||||||
|
"backup_evidence": self.backup_evidence,
|
||||||
|
}
|
||||||
|
|
||||||
|
def lock(self):
|
||||||
|
return DeploymentLock(self.lock_path)
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from core.dev3.operations import SiteOperations
|
||||||
|
from core.locking import LockError
|
||||||
|
|
||||||
|
|
||||||
|
class SiteOperationsTests(unittest.TestCase):
|
||||||
|
def test_independently_constructed_bindings_contend(self):
|
||||||
|
with tempfile.TemporaryDirectory() as directory:
|
||||||
|
first, second = [SiteOperations(Path(directory).resolve()) for _ in range(2)]
|
||||||
|
self.assertEqual(first.journal.directory, second.journal.directory)
|
||||||
|
with first.lock(), self.assertRaises(LockError):
|
||||||
|
with second.lock():
|
||||||
|
self.fail("Competing operation acquired site lock")
|
||||||
Reference in New Issue
Block a user