File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44import gevent
55import argparse
66import random
7+ import shlex
78import traceback
89from collections import defaultdict
910from bson import ObjectId
1011from redis .lock import LuaLock
1112from .processes import Process , ProcessPool
12- from .utils import MovingETA
13+ from .utils import MovingETA , normalize_command
1314from .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
Original file line number Diff line number Diff line change 88import shlex
99import argparse
1010from ..config import add_parser_args
11+ from ..utils import normalize_command
1112import traceback
1213import datetime
1314import 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
Original file line number Diff line number Diff line change 1010from collections import deque
1111from bson import ObjectId
1212import 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+
1943def 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 """
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments