Make state change logic atomic

This commit is contained in:
mvdbeek
2023-11-13 15:45:27 +01:00
parent ee7160bb97
commit 2df6048550
2 changed files with 23 additions and 5 deletions
+3 -2
View File
@@ -1548,9 +1548,10 @@ class MinimalJobWrapper(HasResourceParameters):
return
if info:
job.info = info
job.set_state(state)
state_changed = job.set_state(state)
self.sa_session.add(job)
job.update_output_states(self.app.application_stack.supports_skip_locked())
if state_changed:
job.update_output_states(self.app.application_stack.supports_skip_locked())
if flush:
with transaction(self.sa_session):
self.sa_session.commit()
+20 -3
View File
@@ -1636,10 +1636,27 @@ class Job(Base, JobLike, UsesCreateAndUpdateTime, Dictifiable, Serializable):
def set_state(self, state):
"""
Save state history
Save state history. Returns True if stat has changed, else False
"""
self.state = state
self.state_history.append(JobStateHistory(self))
if self.state == state:
# Nothing changed, no action needed
return False
session = object_session(self)
if session and self.id and not state in Job.terminal_states:
# generate statement that will not revert DELETING or DELETED back to anything non-terminal
rval = session.execute(update(Job.table).where(
Job.table.c.id == self.id,
~Job.table.c.state.in_((Job.states.DELETING, Job.states.DELETED))
).values(state=state))
if rval.rowcount == 1:
self.state_history.append(JobStateHistory(self))
return True
else:
return False
else:
self.state = state
self.state_history.append(JobStateHistory(self))
return True
def get_param_values(self, app, ignore_errors=False):
"""