fix(dsh): reconcile orphan schedules and preserve audit evidence
This commit is contained in:
1 parent
4aaebcb65a
commit
282252a5fb
16 files changed
+578
-27
No files matched your search
@@ -186,3 +186,98 @@ def test_cancel_is_durable_and_cannot_cancel_an_owned_claim(project_service):
|
||||
schedules.claim(claimed['schedule_id'])
|
||||
with pytest.raises(TaskCenterError):
|
||||
schedules.cancel(claimed['schedule_id'])
|
||||
|
||||
|
||||
@pytest.mark.parametrize('run_state', [None, 'running', 'completed', 'paused', 'failed'])
|
||||
def test_reconcile_orphan_claim_preserves_original_run_and_never_replays(project_service, run_state):
|
||||
from dx_productivity.dsh.run_journal import RunJournal
|
||||
service, _, data, ids, hashes = project_service
|
||||
schedules = ScheduleStore(data)
|
||||
row = schedules.save(spec(ids, auto_start=True), hashes)
|
||||
run_id = 'c' * 32
|
||||
schedules.claim(row['schedule_id'], run_id=run_id, automatic=True)
|
||||
journal = RunJournal(data)
|
||||
if run_state:
|
||||
journal.begin(run_id, ids, hashes, schedule_id=row['schedule_id'])
|
||||
if run_state != 'running':
|
||||
journal.finish(run_id, {'status': run_state, 'tasks': [], 'entries': []})
|
||||
before = journal.get(run_id)
|
||||
reopened = ScheduleStore(data)
|
||||
result = reopened.reconcile_claim(service, row['schedule_id'])
|
||||
expected = run_state if run_state in {'completed', 'failed'} else 'interrupted'
|
||||
assert result['status'] == expected and result['run_id'] == run_id
|
||||
assert journal.get(run_id) == before
|
||||
assert result['result']['reconciled'] is True
|
||||
assert reopened.events()[0]['event'] == 'result'
|
||||
assert reopened.events()[0]['payload']['reconciled'] is True
|
||||
assert not reopened.evaluate(service, now=NOW)[0]['dispatchable']
|
||||
assert not list(data.rglob('command.tsv'))
|
||||
|
||||
|
||||
def test_reconcile_refuses_active_owner_and_pending_resources(project_service, monkeypatch):
|
||||
service, _, data, ids, hashes = project_service
|
||||
schedules = ScheduleStore(data)
|
||||
row = schedules.save(spec(ids), hashes)
|
||||
schedules.claim(row['schedule_id'], run_id='d' * 32)
|
||||
with schedules.execution_lease(row['schedule_id']):
|
||||
with pytest.raises(TaskCenterError, match='schedule_busy'):
|
||||
ScheduleStore(data).reconcile_claim(service, row['schedule_id'])
|
||||
monkeypatch.setattr(service, 'pending_batch_resources', lambda: [object()])
|
||||
with pytest.raises(TaskCenterError, match='schedule_busy'):
|
||||
schedules.reconcile_claim(service, row['schedule_id'])
|
||||
assert schedules.get(row['schedule_id'])['status'] == 'claimed'
|
||||
assert len(schedules.events()) == 1
|
||||
|
||||
|
||||
def test_reconcile_audit_failure_leaves_claim_unchanged(project_service):
|
||||
import sqlite3
|
||||
service, _, data, ids, hashes = project_service
|
||||
schedules = ScheduleStore(data)
|
||||
row = schedules.save(spec(ids), hashes)
|
||||
schedules.claim(row['schedule_id'])
|
||||
with sqlite3.connect(schedules.path) as db:
|
||||
db.execute("CREATE TRIGGER reject_result BEFORE INSERT ON schedule_events BEGIN SELECT RAISE(ABORT, 'controlled fault'); END")
|
||||
with pytest.raises(sqlite3.IntegrityError):
|
||||
schedules.reconcile_claim(service, row['schedule_id'])
|
||||
assert schedules.get(row['schedule_id'])['status'] == 'claimed'
|
||||
|
||||
|
||||
def test_hard_process_exit_releases_schedule_lease_for_native_reconciliation(project_service):
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
service, _, data, ids, hashes = project_service
|
||||
schedules = ScheduleStore(data)
|
||||
row = schedules.save(spec(ids, auto_start=True), hashes)
|
||||
child = subprocess.run([sys.executable, '-c',
|
||||
"import os,sys; sys.path.insert(0, 'src'); from dx_productivity.dsh.scheduling import ScheduleStore; "
|
||||
"s=ScheduleStore(sys.argv[1]); lease=s.execution_lease(sys.argv[2]); lease.__enter__(); "
|
||||
"s.claim(sys.argv[2],run_id='e'*32,automatic=True); os._exit(19)", str(data), row['schedule_id']],
|
||||
cwd=Path(__file__).resolve().parents[2], capture_output=True, timeout=20)
|
||||
assert child.returncode == 19, child.stderr.decode('utf8', 'replace')
|
||||
assert ScheduleStore(data).get(row['schedule_id'])['status'] == 'claimed'
|
||||
result = ScheduleStore(data).reconcile_claim(service, row['schedule_id'])
|
||||
assert result['status'] == 'interrupted' and result['run_id'] == 'e' * 32
|
||||
|
||||
|
||||
def test_active_claim_in_another_process_cannot_be_reconciled(project_service):
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
service, _, data, ids, hashes = project_service
|
||||
schedules = ScheduleStore(data)
|
||||
row = schedules.save(spec(ids, auto_start=True), hashes)
|
||||
child = subprocess.Popen([sys.executable, '-c',
|
||||
"import sys; sys.path.insert(0,'src'); from dx_productivity.dsh.scheduling import ScheduleStore; "
|
||||
"s=ScheduleStore(sys.argv[1]); lease=s.execution_lease(sys.argv[2]); lease.__enter__(); "
|
||||
"s.claim(sys.argv[2],run_id='f'*32,automatic=True); print('ready',flush=True); "
|
||||
"sys.stdin.buffer.read(1); lease.__exit__(None,None,None)", str(data), row['schedule_id']],
|
||||
cwd=Path(__file__).resolve().parents[2], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
try:
|
||||
assert child.stdout.readline().strip() == b'ready'
|
||||
with pytest.raises(TaskCenterError, match='schedule_busy'):
|
||||
schedules.reconcile_claim(service, row['schedule_id'])
|
||||
assert schedules.get(row['schedule_id'])['status'] == 'claimed'
|
||||
finally:
|
||||
child.communicate(input=b'q', timeout=10)
|
||||
assert child.returncode == 0
|
||||
Reference in new issue
Block a user