@@ -44,6 +44,10 @@ class Worker(object):
4444 """
4545 status = "init"
4646
47+ mongodb_jobs = None
48+ mongodb_logs = None
49+ redis = None
50+
4751 def __init__ (self , config ):
4852
4953 self .config = config
@@ -99,9 +103,12 @@ def connect(self, force=False):
99103 # Accessing connections attributes will automatically connect
100104 self .redis = connections .redis
101105 self .mongodb_jobs = connections .mongodb_jobs
102- self .mongodb_logs = connections .mongodb_logs
103106
104- self .log_handler .set_collection (self .mongodb_logs .mrq_logs )
107+ if self .config ["mongodb_logs" ] == "0" :
108+ self .log_handler .set_collection (False ) # Disable
109+ else :
110+ self .mongodb_logs = connections .mongodb_logs
111+ self .log_handler .set_collection (self .mongodb_logs .mrq_logs )
105112
106113 self .connected = True
107114
@@ -110,18 +117,20 @@ def connect(self, force=False):
110117
111118 def ensure_indexes (self ):
112119
113- self .mongodb_logs .mrq_logs .ensure_index ([("job" , 1 )], background = False )
114- self .mongodb_logs .mrq_logs .ensure_index ([("worker" , 1 )], background = False , sparse = True )
120+ if self .mongodb_logs :
115121
116- if self .config ["mongodb_logs_size" ] > 0 :
122+ self .mongodb_logs .mrq_logs .ensure_index ([("job" , 1 )], background = False )
123+ self .mongodb_logs .mrq_logs .ensure_index ([("worker" , 1 )], background = False , sparse = True )
117124
118- try :
119- self .mongodb_logs .command ("convertToCapped" , "mrq_logs" , size = self .config ["mongodb_logs_size" ])
120- except :
121- pass
125+ if self .config ["mongodb_logs_size" ] > 0 :
126+
127+ try :
128+ self .mongodb_logs .command ("convertToCapped" , "mrq_logs" , size = self .config ["mongodb_logs_size" ])
129+ except :
130+ pass
122131
123- self .mongodb_logs .mrq_workers .ensure_index ([("status" , 1 )], background = False )
124- self .mongodb_logs .mrq_workers .ensure_index ([("datereported" , 1 )], background = False , expireAfterSeconds = 3600 )
132+ self .mongodb_jobs .mrq_workers .ensure_index ([("status" , 1 )], background = False )
133+ self .mongodb_jobs .mrq_workers .ensure_index ([("datereported" , 1 )], background = False , expireAfterSeconds = 3600 )
125134
126135 self .mongodb_jobs .mrq_jobs .ensure_index ([("status" , 1 )], background = False )
127136 self .mongodb_jobs .mrq_jobs .ensure_index ([("path" , 1 ), ("status" , 1 )], background = False )
@@ -166,6 +175,9 @@ def greenlet_monitoring(self):
166175 self .flush_logs (w = 0 )
167176 time .sleep (int (self .config ["report_interval" ]))
168177
178+ def get_memory (self ):
179+ return self .process .get_memory_info ().rss
180+
169181 def get_worker_report (self ):
170182
171183 greenlets = []
@@ -217,7 +229,7 @@ def get_worker_report(self):
217229 "percent" : self .process .get_cpu_percent (0 )
218230 },
219231 "mem" : {
220- "rss" : self .process . get_memory_info (). rss
232+ "rss" : self .get_memory ()
221233 }
222234 # https://code.google.com/p/psutil/wiki/Documentation
223235 # get_open_files
@@ -233,7 +245,7 @@ def get_worker_report(self):
233245 def report_worker (self , w = 0 ):
234246
235247 try :
236- self .mongodb_logs .mrq_workers .update ({
248+ self .mongodb_jobs .mrq_workers .update ({
237249 "_id" : ObjectId (self .id )
238250 }, {"$set" : self .get_worker_report ()}, upsert = True , w = w )
239251 except pymongo .errors .AutoReconnect :
@@ -321,7 +333,14 @@ def work_loop(self):
321333 while True :
322334
323335 while True :
336+
337+ if self .config ["trace_memory" ]:
338+ # When debugging memory, intermediate psutils call like this one are
339+ # needed for some obscure reason. (tested in test_memoryleaks.py)
340+ self .get_memory ()
341+
324342 free_pool_slots = self .gevent_pool .free_count ()
343+
325344 if free_pool_slots > 0 :
326345 self .status = "wait"
327346 break
@@ -387,6 +406,9 @@ def perform_job(self, job):
387406 This is the first call happening inside the greenlet.
388407 """
389408
409+ if self .config ["trace_memory" ]:
410+ job .trace_memory_start ()
411+
390412 set_current_job (job )
391413
392414 gevent_timeout = gevent .Timeout (job .timeout , JobTimeoutException (
@@ -437,6 +459,9 @@ def perform_job(self, job):
437459
438460 self .done_jobs += 1
439461
462+ if self .config ["trace_memory" ]:
463+ job .trace_memory_stop ()
464+
440465 def shutdown_graceful (self ):
441466 """ Graceful shutdown: waits for all the jobs to finish. """
442467
0 commit comments