Pipelines: cancel a run, look up a workload's step, keep cancelled runs cancelled

This commit is contained in:
claude authored and rektdie committed 2026-10-11 22:28:16 +02:00
1 parent 2b80f804af
commit 71b4803120
4 files changed
+179 -7

No files matched your search

+16
View File
@@ -401,6 +401,22 @@ def plan_next(graph, step_key, outcome, visits, fired):
return plan
def followed_edges(graph, hops):
""" Indices of the edges that a run actually went along, so that they can be
shown apart from the ones it did not.
hops : [ ( step that ended, step that it started, how it ended ) ] """
taken = set()
for source, target, outcome in hops:
for index, edge in enumerate(graph.get('edges', [])):
if edge['from'] == source and edge['to'] == target and edge['on'] in (outcome, 'any'):
taken.add(index)
return taken
def classify_outcome(finished, deleted, passed, failed, test_mode, games, spsa=None):
""" One of OUTCOMES for a workload that is over, or None while it is not """
+77 -7
View File
@@ -88,8 +88,8 @@ def _refresh_run(run, halted=False):
# Caller holds the lock on run
status = graphs.run_status(list(run.steps.values_list('status', flat=True)))
if halted and status == 'COMPLETED':
status = 'CANCELLED'
if (halted and status == 'COMPLETED') or run.status == 'CANCELLED':
status = 'CANCELLED' # Cancelled by hand, or by disabling the Pipeline: it stays that way
run.status = status
run.save()
@@ -118,7 +118,7 @@ def start_pipeline(pipeline, starter=None):
_queue(run, graphs.initial_fire(run.graph), None)
run.save()
_log(pipeline.author, 'PIPELINE #%d RUN #%d STARTED' % (pipeline.id, run.id))
_log(run.starter, 'PIPELINE #%d RUN #%d STARTED' % (pipeline.id, run.id)) # By whoever pressed the button
launch_queued(run.id)
return PipelineRun.objects.get(id=run.id), issues
@@ -148,24 +148,37 @@ def launch_step(step_id):
traceback.print_exc(); sys.stdout.flush()
workload, errors = None, ['Unexpected error while creating the workload']
failed = False
with transaction.atomic():
run = PipelineRun.objects.select_for_update().get(id=step.run_id)
run = PipelineRun.objects.select_for_update().get(id=step.run_id)
step = StepRun.objects.select_for_update().get(id=step_id)
if errors:
if step.status != 'LAUNCHING':
# The run was cancelled while this was being created. The step stays as
# cancelling left it, but a workload that did get made is not forgotten
if workload is not None:
step.test_id = workload.id
step.message = (step.message + '. Its workload was already being created')[:512]
step.save()
elif errors:
failed = True
step.status = 'ERROR'
step.message = ' / '.join(errors)[:512]
step.resolved = timezone.now()
step.save()
_add_notes(run, ['Could not create "%s": %s' % (step.step_key, step.message)])
else:
step.status = 'RUNNING'
step.test_id = workload.id
step.save()
step.save()
_refresh_run(run, halted=not pipeline.enabled or pipeline.deleted)
if errors:
if failed:
_log(pipeline.author, 'PIPELINE #%d %s ERROR' % (pipeline.id, step.step_key))
def _create_workload(step, pipeline):
@@ -206,6 +219,63 @@ def _create_workload(step, pipeline):
return workload, None
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# LOOKING UP #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def step_of(test_id):
""" Which step of which run made a workload, for showing on the page of that
workload. None for the great majority of workloads, that no Pipeline made. """
step = StepRun.objects.select_related('run__pipeline', 'parent').filter(test_id=test_id).first()
if step is None:
return None
names = (step.run.graph or {}).get('steps', {})
name = lambda key: (names.get(key) or {}).get('name') or key
return {
'name' : name(step.step_key),
'pipeline' : step.run.pipeline.name,
'run_id' : step.run_id,
'after' : 'After %s %s' % (name(step.parent.step_key), step.via) if step.parent_id else '',
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# CANCELLING #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
def cancel_run(run_id, by):
""" Ends a run for good, so that nothing more starts from it. Whatever has not
started yet is stopped. Workloads that are already running are left alone,
since stopping a test is a decision for the page of that test. Returns
whether there was a running run to cancel. """
with transaction.atomic():
run = PipelineRun.objects.select_for_update().filter(id=run_id).first()
if run is None or run.status != 'RUNNING':
return False
now = timezone.now()
for step in StepRun.objects.select_for_update().filter(run=run, status__in=('QUEUED', 'LAUNCHING')):
step.status = 'STOPPED'
step.message = 'Run cancelled by %s' % (by)
step.resolved = now
step.save()
run.status = 'CANCELLED'
_add_notes(run, ['Cancelled by %s' % (by)])
run.save()
_log(by, 'PIPELINE #%d RUN #%d CANCELLED' % (run.pipeline_id, run.id))
return True
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# ADVANCING #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
+63
View File
@@ -95,10 +95,12 @@ class FakeCreator(object):
def __init__(self, errors=None, explode=False, mode='SPRT'):
self.calls, self.errors, self.explode, self.mode = [], errors, explode, mode
self.during = None # Called in the middle of creating, like a person clicking Cancel then
def __call__(self, request):
self.calls.append(request)
if self.during: self.during()
if self.explode: raise RuntimeError('Github is down')
if self.errors : return None, list(self.errors)
@@ -428,3 +430,64 @@ class AuditTests(PipelineTestCase):
summaries = list(LogEvent.objects.values_list('summary', flat=True))
self.assertTrue(any('STARTED' in s for s in summaries))
self.assertTrue(any(s.endswith('stc PASSED') for s in summaries))
class CancelTests(PipelineTestCase):
def test_cancelling_stops_whatever_has_not_started_and_lets_the_rest_run(self):
both = graph(); both['edges'].append({ 'from' : 'stc', 'on' : 'passed', 'to' : 'retry' })
run = self.start(graph=both)
stc = self.steps(run)['stc']
queued = StepRun.objects.create(run=run, step_key='ltc', kind='TEST', payload=payload()) # Waiting
self.assertTrue(pipelines.cancel_run(run.id, 'alice'))
self.assertEqual(self.status(run), 'CANCELLED')
self.assertEqual(StepRun.objects.get(id=queued.id).status, 'STOPPED')
self.assertEqual(StepRun.objects.get(id=stc.id).status, 'RUNNING') # Its test keeps going
self.assertIn('alice', ' '.join(PipelineRun.objects.get(id=run.id).notes))
def test_a_cancelled_run_starts_nothing_and_stays_cancelled(self):
run = self.start()
pipelines.cancel_run(run.id, 'alice')
self.finish(self.steps(run)['stc'], passed=True) # Would normally start the LTC
self.assertEqual(self.keys(run), ['stc'])
self.assertEqual(self.steps(run)['stc'].status, 'PASSED') # Still told how it ended
self.assertEqual(self.status(run), 'CANCELLED')
def test_only_a_running_run_can_be_cancelled(self):
run = self.start()
self.finish(self.steps(run)['stc'])
self.assertEqual(self.status(run), 'COMPLETED')
self.assertFalse(pipelines.cancel_run(run.id, 'alice'))
self.assertEqual(self.status(run), 'COMPLETED')
self.assertFalse(pipelines.cancel_run(999999, 'alice'))
def test_cancelling_twice_is_harmless(self):
run = self.start()
self.assertTrue(pipelines.cancel_run(run.id, 'alice'))
self.assertFalse(pipelines.cancel_run(run.id, 'alice'))
self.assertEqual(len(PipelineRun.objects.get(id=run.id).notes), 1)
def test_cancelling_while_a_workload_is_being_created_does_not_resurrect_the_step(self):
run = self.pipeline()
started = {}
def click_cancel():
started['run'] = PipelineRun.objects.get(pipeline=run)
self.assertTrue(pipelines.cancel_run(started['run'].id, 'alice'))
self.creator.during = click_cancel
made, _ = pipelines.start_pipeline(run)
stc = StepRun.objects.get(run=started['run'])
self.assertEqual(stc.status, 'STOPPED') # Not flipped back to RUNNING
self.assertNotEqual(stc.test_id, 0) # But the workload that got made is recorded
self.assertEqual(self.status(started['run']), 'CANCELLED')
self.assertIn('cancelled', stc.message)
+23
View File
@@ -312,6 +312,29 @@ class PlanTests(unittest.TestCase):
plan.fires[0].payload['priority'] = '99'
self.assertEqual(graph['steps']['ltc']['payload']['priority'], '0')
class FollowedEdgesTests(unittest.TestCase):
def test_only_the_edges_that_were_gone_along(self):
graph = stc_then_ltc()
self.assertEqual(followed_edges(graph, [('stc', 'ltc', 'passed')]), { 0 })
self.assertEqual(followed_edges(graph, [('stc', 'retry', 'failed')]), { 1 })
def test_nothing_followed_yet(self):
self.assertEqual(followed_edges(stc_then_ltc(), []), set())
def test_the_outcome_has_to_match_the_edge(self):
# stc -> ltc is only for a pass, so a fail that reached ltc some other way is not it
self.assertEqual(followed_edges(stc_then_ltc(), [('stc', 'ltc', 'failed')]), set())
def test_an_any_edge_matches_every_outcome(self):
graph = stc_then_ltc(); graph['edges'] = [{ 'from' : 'stc', 'on' : 'any', 'to' : 'ltc' }]
for outcome in OUTCOMES:
self.assertEqual(followed_edges(graph, [('stc', 'ltc', outcome)]), { 0 })
def test_a_loop_is_one_edge_however_often_it_is_gone_along(self):
graph = stc_then_ltc(); graph['edges'][1]['to'] = 'stc'
self.assertEqual(followed_edges(graph, [('stc', 'stc', 'failed')] * 3), { 1 })
class RunStatusTests(unittest.TestCase):
def test_status(self):