Skip to content

Commit 48a250d

Browse files
authored
Merge pull request #134 from FlorianPerucki/master
raw subqueues should use their root queue's config
2 parents 60a19f1 + efa050e commit 48a250d

5 files changed

Lines changed: 39 additions & 8 deletions

File tree

mrq/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
import re
55
from .version import VERSION
6-
from .utils import get_local_ip
6+
from .utils import get_local_ip, DelimiterArgParser
77
import atexit
88

99

@@ -344,7 +344,8 @@ def add_parser_args(parser, config_type):
344344
parser.add_argument(
345345
'--subqueues_delimiter',
346346
default='/',
347-
help='Delimiter between main queue and subqueue names')
347+
help='Delimiter between main queue and subqueue names',
348+
action=DelimiterArgParser)
348349

349350
parser.add_argument(
350351
'--admin_port',

mrq/queue.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class Queue(object):
1515
is_set = False
1616
is_reverse = False
1717

18+
# root_id will contain the root queue id without any trailing subqueue delimiter
19+
# e.g. if self.id is "some_queue/" then self.root_id will contain "some_queue"
20+
# and if self.id is "some_queue/some_subqueue" then self.root_id will contain "some_queue"
21+
root_id = None
22+
1823
use_large_ids = False
1924

2025
# This is a mutable type so it is shared by all instances
@@ -47,6 +52,13 @@ def __init__(self, queue_id, add_to_known_queues=False):
4752
if "_sorted" in self.id:
4853
self.is_sorted = True
4954

55+
self.root_id = self.id
56+
57+
delimiter = context.get_current_config().get("subqueues_delimiter")
58+
if delimiter is not None and delimiter in self.id:
59+
# Get the root queue id with no trailing delimiter
60+
self.root_id = self.id.split(delimiter)[0]
61+
5062
self.use_large_ids = context.get_current_config()["use_large_job_ids"]
5163

5264
# If this is the first time this process sees this queue, try to add it
@@ -117,7 +129,7 @@ def redis_known_subqueues(self):
117129
def get_config(self):
118130
""" Returns the specific configuration for this queue """
119131

120-
return context.get_current_config().get("raw_queues", {}).get(self.id) or {}
132+
return context.get_current_config().get("raw_queues", {}).get(self.root_id) or {}
121133

122134
def serialize_job_ids(self, job_ids):
123135
""" Returns job_ids serialized for storage in Redis """

mrq/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import math
55
import json
66
import datetime
7+
import argparse
78
from collections import deque
89
from bson import ObjectId
910

@@ -194,3 +195,11 @@ def next(self, val):
194195
self.__sum += val
195196
self.__q.append(val)
196197
return 1.0 * self.__sum / len(self.__q)
198+
199+
200+
class DelimiterArgParser(argparse.Action):
201+
def __call__(self, parser, namespace, value, option_string):
202+
if value == '_':
203+
parser.error("Cannot use '%s' as a subqueue delimiter" % value)
204+
205+
setattr(namespace, self.dest, value)

tests/test_raw.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,23 @@ def test_raw_sorted(worker, p_queue, p_pushback, p_timed, p_flags):
7979
assert test_collection.count() == 3
8080

8181

82+
@pytest.mark.parametrize("has_subqueue", [False, True])
8283
@pytest.mark.parametrize(["p_queue", "p_set"], [
8384
["test_raw", False],
8485
["test_set", True]
8586
])
86-
def test_raw_set(worker, p_queue, p_set):
87-
88-
worker.start(
89-
flags="--greenlets 10 --config tests/fixtures/config-raw1.py", queues=p_queue)
87+
def test_raw_set(worker, has_subqueue, p_queue, p_set):
88+
flags = "--greenlets 10 --config tests/fixtures/config-raw1.py"
89+
if has_subqueue:
90+
flags = "%s --subqueues_refresh_interval=0.1" % flags
91+
# worker should dequeue all subqueues
92+
p_queue = "%s/" % p_queue
93+
94+
worker.start(flags=flags, queues=p_queue)
95+
96+
if has_subqueue:
97+
# queue tasks in p_queue/subqueue
98+
p_queue = "%ssubqueue" % p_queue
9099

91100
test_collection = worker.mongodb_logs.tests_inserts
92101
jobs_collection = worker.mongodb_jobs.mrq_jobs

tests/test_subqueues.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_unmatchable_subqueues(worker, queue, enqueue_on):
4343
worker.stop()
4444

4545

46-
@pytest.mark.parametrize(["delimiter"], ["/", ".", "_"])
46+
@pytest.mark.parametrize(["delimiter"], ["/", ".", "-"])
4747
def test_custom_delimiters(worker, delimiter):
4848

4949
queue = "main" + delimiter

0 commit comments

Comments
 (0)