Update timing condition

This commit is contained in:
Adrià Casajús 2022-06-30 17:25:15 +02:00
parent b6ebeaac69
commit 03f2ae2bc4
No known key found for this signature in database
GPG key ID: F0033226A5AFC9B9
2 changed files with 7 additions and 26 deletions

View file

@ -223,25 +223,22 @@ SimpleLogin team.
def get_jobs_to_run() -> List[Job]:
# run a job 1h earlier or later is not a big deal ...
min_dt = arrow.now().shift(hours=-1)
max_dt = arrow.now().shift(hours=1)
# Get jobs that match all conditions:
# - Job.state == ready OR (Job.state == taken AND Job.taken_at < now - 30 mins AND Job.attempts < 5)
# - Job.run_at is Null OR (Job.run_at > min_dt AND Job.run_at < max_dt)
# - Job.run_at is Null OR Job.run_at < now + 10 mins
taken_at_earliest = arrow.now().shift(minutes=-config.JOB_TAKEN_RETRY_WAIT_MINS)
run_at_earliest = arrow.now().shift(minutes=+10)
query = Job.filter(
and_(
or_(
Job.state == JobState.ready.value,
and_(
Job.state == JobState.taken.value,
Job.taken_at
< arrow.now().shift(minutes=-config.JOB_TAKEN_RETRY_WAIT_MINS),
Job.taken_at < taken_at_earliest,
Job.attempts < config.JOB_MAX_ATTEMPTS,
),
),
or_(Job.run_at.is_(None), and_(Job.run_at > min_dt, Job.run_at <= max_dt)),
or_(Job.run_at.is_(None), and_(Job.run_at <= run_at_earliest)),
)
)
return query.all()

View file

@ -12,8 +12,7 @@ def test_get_jobs_to_run(flask_client):
expected_jobs_to_run = [
# Jobs in ready state
Job.create(name="", payload=""),
Job.create(name="", payload="", run_at=now.shift(minutes=40)),
Job.create(name="", payload="", run_at=now.shift(minutes=-40)),
Job.create(name="", payload="", run_at=now),
# Jobs in taken state
Job.create(
name="",
@ -33,14 +32,7 @@ def test_get_jobs_to_run(flask_client):
payload="",
state=JobState.taken.value,
taken_at=now.shift(minutes=-(config.JOB_TAKEN_RETRY_WAIT_MINS + 10)),
run_at=now.shift(minutes=30),
),
Job.create(
name="",
payload="",
state=JobState.taken.value,
taken_at=now.shift(minutes=-(config.JOB_TAKEN_RETRY_WAIT_MINS + 10)),
run_at=now.shift(minutes=-30),
run_at=now,
),
]
# Jobs not to run
@ -63,14 +55,6 @@ def test_get_jobs_to_run(flask_client):
taken_at=now.shift(minutes=-(config.JOB_TAKEN_RETRY_WAIT_MINS + 10)),
run_at=now.shift(hours=3),
)
# Job taken with enough time but out of run_at zone
Job.create(
name="",
payload="",
state=JobState.taken.value,
taken_at=now.shift(minutes=-(config.JOB_TAKEN_RETRY_WAIT_MINS + 10)),
run_at=now.shift(hours=-3),
)
# Job out of attempts
Job.create(
name="",