Skip to content

Commit 6555dc4

Browse files
Merge pull request #168 from pricingassistant/scheduler-patch
Scheduler patch
2 parents 2e5d48c + 51ed2a3 commit 6555dc4

2 files changed

Lines changed: 36 additions & 12 deletions

File tree

mrq/scheduler.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ def check(self):
9191

9292
interval = datetime.timedelta(seconds=task["interval"])
9393

94-
last_time = now - interval
94+
if task["datelastqueued"] >= now:
95+
continue
9596

9697
if task.get("monthday", current_monthday) != current_monthday:
9798
continue
@@ -103,19 +104,22 @@ def check(self):
103104
if task["datelastqueued"].date() == now.date() or now.time() < task["dailytime"].time():
104105
continue
105106

106-
if task["datelastqueued"] <= last_time:
107+
# if we only have "interval" key
108+
if all(k not in task for k in ["monthday", "weekday", "dailytime"]):
109+
if now - task["datelastqueued"] < interval:
110+
continue
107111

108-
queue_job(
109-
task["path"],
110-
task.get("params") or {},
111-
queue=task.get("queue")
112-
)
112+
queue_job(
113+
task["path"],
114+
task.get("params") or {},
115+
queue=task.get("queue")
116+
)
113117

114-
self.collection.update({"_id": task["_id"]}, {"$set": {
115-
"datelastqueued": now
116-
}})
118+
self.collection.update({"_id": task["_id"]}, {"$set": {
119+
"datelastqueued": now
120+
}})
117121

118-
log.debug("Scheduler: queued %s" % _hash_task(task))
122+
log.debug("Scheduler: queued %s" % _hash_task(task))
119123

120124
# Make sure we never again execute a scheduler with the same exact second.
121125
time.sleep(1)

tests/test_scheduler.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_scheduler_simple(worker, p_flags):
4444

4545
collection.remove({})
4646

47-
scheduled_jobs.update_many({}, {"$set": {"datelastqueued": datetime.datetime.utcnow()}})
47+
scheduled_jobs.update_many({}, {"$set": {"datelastqueued": datetime.datetime.utcnow() + timedelta(seconds=10)}})
4848

4949
# Start with new config
5050
worker.start(
@@ -114,6 +114,26 @@ def _start(deps=True):
114114
assert collection.find({"params.b": "test"}).count() == 1
115115

116116

117+
def test_scheduler_dailytime_with_datelastqueued(worker):
118+
now = time.time()
119+
worker.start(
120+
flags="--scheduler --config tests/fixtures/config-scheduler3.py",
121+
env={
122+
# We need to pass this in the environment so that each worker has the
123+
# exact same hash
124+
"MRQ_TEST_SCHEDULER_TIME": str(now - 100 - 3600 * 24)
125+
})
126+
time.sleep(7)
127+
print(list(worker.mongodb_jobs.tests_inserts.find()))
128+
assert len(list(worker.mongodb_jobs.tests_inserts.find())) == 2
129+
# pretend first run was yesterday at time(now) + 200s
130+
datelastqueued = datetime.datetime.fromtimestamp(now - 3600*24 + 200)
131+
worker.mongodb_jobs.mrq_scheduled_jobs.update({"params.b": "test"}, {"$set": {'datelastqueued': datelastqueued}})
132+
time.sleep(3)
133+
# task is ran
134+
assert len(list(worker.mongodb_jobs.tests_inserts.find())) == 3
135+
136+
117137
def test_scheduler_weekday_dailytime(worker):
118138
# Task is scheduled in 5 seconds
119139
worker.start(

0 commit comments

Comments
 (0)