Skip to content

Commit 086e463

Browse files
author
maelorn
committed
multiply worker group commands by number of processes
1 parent 41e6225 commit 086e463

4 files changed

Lines changed: 47 additions & 5 deletions

File tree

mrq/agent.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import gevent
55
import argparse
66
import random
7+
import shlex
78
import traceback
89
from collections import defaultdict
910
from bson import ObjectId
1011
from redis.lock import LuaLock
1112
from .processes import Process, ProcessPool
12-
from .utils import MovingETA
13+
from .utils import MovingETA, normalize_command
1314
from .queue import Queue
1415

1516

@@ -195,7 +196,8 @@ def fetch_worker_group_definition(self):
195196
# Prepend all commands by their worker profile.
196197
commands = []
197198
for command in definition.get("commands", []):
198-
commands.append("MRQ_WORKER_GROUP=%s %s" % (self.worker_group, command))
199-
definition["commands"] = commands
199+
simplified_command, worker_count = normalize_command(command, self.worker_group)
200+
commands.extend([simplified_command] * worker_count)
200201

202+
definition["commands"] = commands
201203
return definition

mrq/basetasks/orchestrator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import shlex
99
import argparse
1010
from ..config import add_parser_args
11+
from ..utils import normalize_command
1112
import traceback
1213
import datetime
1314
import re
@@ -84,8 +85,8 @@ def fetch_worker_group_definitions(self):
8485
commands = []
8586
# Prepend all commands by their worker group.
8687
for command in definition.get("commands", []):
87-
commands.append("MRQ_WORKER_GROUP=%s %s" % (definition["_id"], command))
88-
88+
simplified_command, worker_count = normalize_command(command, definition["_id"])
89+
commands.extend([simplified_command] * worker_count)
8990
definition["commands"] = commands
9091

9192
return definitions

mrq/utils.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,36 @@
1010
from collections import deque
1111
from bson import ObjectId
1212
import uuid
13+
import shlex
1314

1415
#
1516
# Utils are functions that should be independent from the rest of MRQ's codebase
1617
#
1718

1819

20+
def normalize_command(command, worker_group):
21+
if "--processes" in command:
22+
simplified_command = ""
23+
worker_count = 0
24+
skip_next = False
25+
for part in shlex.split(command):
26+
if skip_next:
27+
worker_count = part
28+
skip_next = False
29+
continue
30+
if part.startswith("--processes="):
31+
worker_count = part.split("=")[1]
32+
continue
33+
if part == "--processes":
34+
skip_next = True
35+
continue
36+
simplified_command += " %s" % part
37+
skip_next = False
38+
simplified_command = "MRQ_WORKER_GROUP=%s %s" % (worker_group, simplified_command)
39+
return simplified_command, int(worker_count)
40+
return "MRQ_WORKER_GROUP=%s %s" % (worker_group, command), 1
41+
42+
1943
def get_local_ip():
2044
""" Returns the local IP. Can be overwritten in the config with --local-ip so don't call
2145
this function directly, instead get the current value from the config """

tests/test_agent.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,3 +292,18 @@ def test_agent_force_terminate(worker):
292292

293293
assert len(pids_after_sigkill) == len(pids_before_sigkill)
294294
assert set(pids_after_sigkill) != set(pids_before_sigkill)
295+
296+
def test_agent_multiple_processes(worker):
297+
worker.start(agent=True, flags="--worker_group xxx --orchestrate_interval=1 --report_interval=1")
298+
pids_before = psutil.pids()g
299+
300+
connections.mongodb_jobs.mrq_workergroups.insert_one({
301+
"_id": "xxx",
302+
"commands": ["mrq-worker --processes 2 a", "mrq-worker --processes=2 b"],
303+
"process_termination_timeout": 1
304+
})
305+
time.sleep(3)
306+
307+
pids_after = psutil.pids()
308+
# make sure there are 4 workers running
309+
assert len(pids_before) + 4 == len(pids_after)

0 commit comments

Comments
 (0)