fix: preserve safe stop and explicit forced interruption lifecycle
This commit is contained in:
1 parent
b939101565
commit
9c03cd5900
29 files changed
+1829
-86
No files matched your search
@@ -0,0 +1,129 @@
|
||||
"""Request ordinary stop during save on disposable projects; never replay a run."""
|
||||
from dataclasses import asdict
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
from threading import Event, Thread
|
||||
import time
|
||||
from uuid import uuid4
|
||||
|
||||
REPO = Path(__file__).resolve().parents[1]
|
||||
sys.path[:0] = [str(REPO / 'src'), str(REPO / 'tools')]
|
||||
from probe_batch_runtime_live import hashes, compare_saved, save, OLD, NEW, NAMES
|
||||
from dx_productivity.trial_task_center.isolated_cad import IsolatedCadSession
|
||||
from dx_productivity.trial_task_center.logging_setup import configure_logging
|
||||
from dx_productivity.trial_task_center.service import TaskCenterService
|
||||
from dx_productivity.trial_task_center.store import TaskStore
|
||||
from dx_productivity.trial_task_center.bom_plan import write_bom_template
|
||||
from dx_productivity.trial_task_center.stop_control import StopControl
|
||||
from dx_productivity.trial_task_center.task_queue import execute_task_queue
|
||||
|
||||
|
||||
def main():
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--source-root', required=True, type=Path)
|
||||
parser.add_argument('--library', required=True, type=Path)
|
||||
parser.add_argument('--run-native', action='store_true', required=True)
|
||||
args = parser.parse_args()
|
||||
output = REPO / 'build' / ('safe-stop-live-' + uuid4().hex[:10])
|
||||
output.mkdir()
|
||||
source_root, library = args.source_root.resolve(strict=True), args.library.resolve(strict=True)
|
||||
source_hashes = {name: hashes(source_root / name) for name in NAMES}
|
||||
library_hash = hashlib.sha256(library.read_bytes()).hexdigest()
|
||||
projects = []
|
||||
for name in NAMES:
|
||||
folder = output / 'projects' / name
|
||||
shutil.copytree(source_root / name, folder)
|
||||
project = folder / (name + '.prj')
|
||||
text, count = re.subn(r'(?m)^KEY CentralLibrary .*$', lambda _: f'KEY CentralLibrary "{library.as_posix()}"',
|
||||
project.read_text(encoding='utf-8-sig'))
|
||||
assert count == 1
|
||||
project.write_text(text, encoding='utf-8')
|
||||
projects.append(project)
|
||||
report = {'scope': 'source supervised service, production VBS and real CAD; no GUI or frozen EXE',
|
||||
'output': str(output), 'projects': list(map(str, projects))}
|
||||
print(json.dumps(report, ensure_ascii=False), flush=True)
|
||||
store = TaskStore(output / 'tasks.db')
|
||||
service = TaskCenterService(store, configure_logging(output / 'data'), supervised=True)
|
||||
control, finished = StopControl(), Event()
|
||||
watcher = None
|
||||
def inspect(project, sheets, label):
|
||||
evidence = output / (label + '-' + project.stem)
|
||||
evidence.mkdir()
|
||||
spec, result = evidence / 'spec.json', evidence / 'graph.json'
|
||||
save(spec, {'project': str(project), 'sheets': sorted(sheets), 'destination': str(result)})
|
||||
with IsolatedCadSession(evidence) as cad:
|
||||
child = cad.start_worker((sys.executable, str(REPO / 'tools/probe_batch_runtime_live.py'),
|
||||
'--repo', str(REPO), '--inspect-file', str(spec), '--run-native'), stdout=-1, stderr=-1)
|
||||
try:
|
||||
code = child.wait(timeout=180)
|
||||
(evidence / 'worker-output.txt').write_bytes(child.stdout.read() + b'\n' + child.stderr.read())
|
||||
assert code == 0, (evidence / 'worker-output.txt').read_text(errors='replace')
|
||||
finally:
|
||||
child.close()
|
||||
return json.loads(result.read_text(encoding='utf-8'))
|
||||
try:
|
||||
tasks = service.create_project_tasks(output / 'projects', selected_paths=tuple(projects))
|
||||
bom = write_bom_template(output / 'input.xlsx', ({'project_path': str(p), 'old_part_number': OLD,
|
||||
'new_part_number': NEW, 'refdes': 'RV05', 'enabled': True, 'sync_nc': True} for p in projects))
|
||||
imported = service.inspect_bom_batch(tasks[0].task_id, bom)
|
||||
assert imported.passed_rows == 2 and imported.preview is not None
|
||||
service.save_bom_batch(imported.preview, {p.task_id: p.rows for p in imported.preview.projects})
|
||||
before, sheet_sets = {}, {}
|
||||
for project in projects:
|
||||
rows = next(p.rows for p in imported.preview.projects if p.project_path.resolve() == project.resolve())
|
||||
sheet_sets[project.stem] = {(row.target.schematic_name, row.target.sheet_number) for row in rows}
|
||||
before[project.stem] = inspect(project, sheet_sets[project.stem], 'before')
|
||||
def stop_during_save():
|
||||
while not finished.wait(.01):
|
||||
for path in (output / 'data/tasks').glob('*/quick-run/quick-part-replace.log'):
|
||||
try:
|
||||
text = path.read_text(encoding='utf-8-sig')
|
||||
except OSError:
|
||||
continue
|
||||
if 'save_requested job=' in text:
|
||||
report['stop_requested'] = {'time_ns': time.time_ns(), 'log': str(path),
|
||||
'trigger': 'production save_requested log; ordinary stop only'}
|
||||
control.set()
|
||||
return
|
||||
watcher = Thread(target=stop_during_save, daemon=True)
|
||||
watcher.start()
|
||||
result = execute_task_queue(service, tuple(task.task_id for task in tasks), stop_event=control)
|
||||
finished.set()
|
||||
watcher.join(3)
|
||||
report['queue_result'] = asdict(result)
|
||||
assert report.get('stop_requested') and not control.force_event.is_set()
|
||||
assert result.paused and [row.state for row in result.entries] == ['completed', 'pending'], result
|
||||
report['comparisons'] = []
|
||||
for index, project in enumerate(projects):
|
||||
after = inspect(project, sheet_sets[project.stem], 'after')
|
||||
if index == 0:
|
||||
report['comparisons'].append(compare_saved(before[project.stem], after))
|
||||
else:
|
||||
assert after == before[project.stem], 'second project was changed after stop'
|
||||
report['comparisons'].append({'unchanged': True, 'components': len(after)})
|
||||
assert not service.pending_batch_resources()
|
||||
assert not list((output / 'data').rglob('uncertain.flag'))
|
||||
report['verified'] = True
|
||||
except BaseException as error:
|
||||
import traceback
|
||||
report.update(error=repr(error), traceback=traceback.format_exc())
|
||||
raise
|
||||
finally:
|
||||
finished.set()
|
||||
if watcher is not None:
|
||||
watcher.join(3)
|
||||
store.close()
|
||||
report['source_unchanged'] = all(hashes(source_root / name) == source_hashes[name] for name in NAMES)
|
||||
report['library_lmc_unchanged'] = hashlib.sha256(library.read_bytes()).hexdigest() == library_hash
|
||||
save(output / 'report.json', report)
|
||||
print(json.dumps(report, ensure_ascii=False), flush=True)
|
||||
return 0 if report.get('verified') and report['source_unchanged'] and report['library_lmc_unchanged'] else 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user