Skip to content
Snippets Groups Projects
Commit 5c3a2d48 authored by Martin Mareš's avatar Martin Mareš
Browse files

Jobs: TheJob.run umí hotový job spustit znovu

Hodí se na ladění jobů.

Navíc lze TheJob.load() neškodně zavolat vícekrát.
parent 0122d5e6
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !81. Comments created here will be created in the context of that merge request.
......@@ -34,6 +34,7 @@ class TheJob:
self.errors = []
def load(self) -> db.Job:
if getattr(self, 'job', None) is None:
sess = db.get_session()
self.job = sess.query(db.Job).with_for_update().get(self.job_id)
return self.job
......@@ -103,11 +104,29 @@ class TheJob:
logger.info(f'Job: >> {msg}')
self.errors.append(msg)
def run(self):
def _check_runnable(self, retry: bool) -> Optional[str]:
s = self.job.state
if s == db.JobState.ready:
return None
elif s == db.JobState.running:
# Může se stát, že ho mezitím začal vyřizovat jiný proces
return 'právě běží'
elif s in (db.JobState.done, db.JobState.failed):
return None if retry else 'je už hotový'
else:
return 'je v neznámém stavu'
def run(self, retry: bool = False):
sess = db.get_session()
if not self.load() or self.job.state != db.JobState.ready:
# Někdo ho mezitím smazal nebo vyřídil
logger.info(f'Job: Job #{self.job_id} vyřizuje někdo jiný')
if not self.load():
# Někdo ho mezitím smazal
logger.info(f'Job: Job #{self.job_id} neexistuje')
sess.rollback()
return
reject_reason = self._check_runnable(retry)
if reject_reason is not None:
logger.info(f'Job: Job #{self.job_id} {reject_reason}')
sess.rollback()
return
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment