Skip to content

Commit a4b91e9

Browse files
author
lcalem
committed
Fixed a bug where task hash was different when a task had several parameters
1 parent 80eb6f2 commit a4b91e9

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

mrq/scheduler.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .context import log
22
from .queue import send_task
33
import datetime
4+
import json
45

56

67
class Scheduler(object):
@@ -14,8 +15,14 @@ def refresh(self):
1415
self.all_tasks = list(self.collection.find())
1516

1617
def hash_task(self, task):
17-
return " ".join(
18-
[str(task.get(x)) for x in ["path", "params", "interval", "dailytime", "queue"]])
18+
params = task.get("params")
19+
if params:
20+
params = json.dumps(sorted(task["params"].items(), key=lambda x: x[0]))
21+
22+
full = [str(task.get(x)) for x in ["path", "interval", "dailytime", "queue"]]
23+
24+
print full.extend([str(params)])
25+
return " ".join(full)
1926

2027
def sync_tasks(self, tasks):
2128
""" Performs the first sync of a list of tasks, often defined in the config file. """

tests/fixtures/config-scheduler3.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
},
1010
"dailytime": datetime.datetime.fromtimestamp(float(os.environ.get("MRQ_TEST_SCHEDULER_TIME"))).time()
1111
},
12+
{
13+
"path": "tests.tasks.general.MongoInsert",
14+
"params": {
15+
"a": 1,
16+
"b": "test",
17+
"c": 3.0
18+
},
19+
"dailytime": datetime.datetime.fromtimestamp(float(os.environ.get("MRQ_TEST_SCHEDULER_TIME"))).time()
20+
}
1221
]
1322

1423
SCHEDULER_INTERVAL = 1

tests/test_scheduler.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
from bson import ObjectId
2-
import urllib2
31
import time
42
import pytest
5-
import datetime
63

74

85
# We want to test that launching the scheduler several times queues tasks
@@ -73,13 +70,13 @@ def test_scheduler_dailytime(worker, p_flags):
7370
assert collection.find().count() == 0
7471

7572
# It should be done a first time immediately
76-
time.sleep(2)
77-
assert collection.find().count() == 1
73+
time.sleep(3)
74+
assert collection.find().count() == 2
7875

7976
# Then a second time once the dailytime passes
8077
time.sleep(7)
81-
assert collection.find().count() == 2
78+
assert collection.find().count() == 4
8279

8380
# Nothing more should happen today
8481
time.sleep(4)
85-
assert collection.find().count() == 2
82+
assert collection.find().count() == 4

0 commit comments

Comments
 (0)