5
5
sys .exit ("multiprocessing was not available" )
6
6
7
7
import time , os , signal
8
+ import datetime
9
+ import logging
10
+
8
11
from pyres .worker import Worker
9
12
from pyres import ResQ
10
13
from pyres .exceptions import NoQueueError
11
14
from pyres .utils import OrderedDict
12
- import datetime
15
+
13
16
14
17
class Minion (multiprocessing .Process , Worker ):
15
18
def __init__ (self , queues , server , password ):
@@ -18,6 +21,7 @@ def __init__(self, queues, server, password):
18
21
self .validate_queues ()
19
22
self ._shutdown = False
20
23
self .child = None
24
+ self .hostname = os .uname ()[1 ]
21
25
if isinstance (server ,basestring ):
22
26
self .resq = ResQ (server = server , password = password )
23
27
elif isinstance (server , ResQ ):
@@ -26,12 +30,14 @@ def __init__(self, queues, server, password):
26
30
raise Exception ("Bad server argument" )
27
31
#Worker.__init__(self, queues=queues, server="localhost:6379", password=None)
28
32
#super(Minion,self).__init__(name='Minion')
33
+ def prune_dead_workers (self ):
34
+ pass
29
35
30
36
def work (self , interval = 5 ):
31
37
self .startup ()
32
38
while True :
33
39
if self ._shutdown :
34
- print 'shutdown scheduled'
40
+ logging . info ( 'shutdown scheduled' )
35
41
break
36
42
job = self .reserve ()
37
43
if job :
@@ -53,7 +59,6 @@ class Khan(object):
53
59
'REMOVE' : '_remove_minion' ,
54
60
'SHUTDOWN' : '_schedule_shutdown'
55
61
}
56
- _workers = OrderedDict ()
57
62
def __init__ (self , pool_size = 5 , queues = [], server = 'localhost:6379' , password = None ):
58
63
#super(Khan,self).__init__(queues=queues,server=server,password=password)
59
64
self ._shutdown = False
@@ -63,6 +68,7 @@ def __init__(self, pool_size=5, queues=[], server='localhost:6379', password=Non
63
68
self .password = password
64
69
self .pid = os .getpid ()
65
70
self .validate_queues ()
71
+ self ._workers = OrderedDict ()
66
72
if isinstance (server ,basestring ):
67
73
self .resq = ResQ (server = server , password = password )
68
74
elif isinstance (server , ResQ ):
@@ -91,7 +97,7 @@ def _schedule_shutdown(self):
91
97
self .schedule_shutdown (None , None )
92
98
93
99
def schedule_shutdown (self , signum , frame ):
94
- print ' Shutdown scheduled'
100
+ logging . info ( 'Khan Shutdown scheduled')
95
101
self ._shutdown = True
96
102
97
103
def kill_child (self , signum , frame ):
@@ -106,10 +112,10 @@ def register_khan(self):
106
112
107
113
def _check_commands (self ):
108
114
if not self ._shutdown :
109
- print 'Checking commands'
115
+ logging . debug ( 'Checking commands' )
110
116
command_key = 'resque:khan:%s' % self
111
- command = self .resq .redis .pop (command_key )
112
- print 'COMMAND' , command
117
+ command = self .resq .redis .lpop (command_key )
118
+ logging . debug ( 'COMMAND FOUND: ' , command )
113
119
if command :
114
120
self .process_command (command )
115
121
self ._check_commands ()
@@ -124,16 +130,16 @@ def process_command(self, command):
124
130
fn ()
125
131
126
132
def add_minion (self ):
127
- print 'Adding minion'
128
133
m = self ._add_minion ()
129
134
m .start ()
130
135
131
136
def _add_minion (self ):
132
- print 'Adding mminion'
137
+ logging . info ( 'Adding minion' )
133
138
#parent_conn, child_conn = multiprocessing.Pipe()
134
139
m = Minion (self .queues , self .server , self .password )
135
140
#m.start()
136
141
self ._workers [m .pid ] = m
142
+ logging .info ('minion added at %s' % m .pid )
137
143
return m
138
144
#self._workers.append(m)
139
145
@@ -153,12 +159,13 @@ def _remove_minion(self, pid=None):
153
159
return m
154
160
155
161
def register_worker (self ):
162
+ logging .debug ('registering khan' )
156
163
self .resq .redis .sadd ('resque:khans' ,str (self ))
157
164
#self.resq._redis.add("worker:#{self}:started", Time.now.to_s)
158
165
self .started = datetime .datetime .now ()
159
166
160
167
def unregister_worker (self ):
161
- print 'Unregistering'
168
+ logging . debug ( 'unregistering khan' )
162
169
self .resq .redis .srem ('resque:khans' ,str (self ))
163
170
self .started = None
164
171
0 commit comments