11from pyres .exceptions import NoQueueError
22from pyres .job import Job
33from pyres import ResQ , Stat
4+ import logging
45import signal
56import datetime
67import os , sys
@@ -80,7 +81,7 @@ def schedule_shutdown(self, signum, frame):
8081
8182 def kill_child (self , signum , frame ):
8283 if self .child :
83- print "Killing child at %s" % self .child
84+ logging . info ( "Killing child at %s" % self .child )
8485 os .kill (self .child , signal .SIGKILL )
8586
8687 def __str__ (self ):
@@ -105,24 +106,25 @@ def work(self, interval=5):
105106 self .startup ()
106107 while True :
107108 if self ._shutdown :
108- print 'shutdown scheduled'
109+ logging . info ( 'shutdown scheduled' )
109110 break
110111 job = self .reserve ()
111112 if job :
112- print "got: %s" % job
113+ logging .info ('picked up job' )
114+ logging .debug ('job details: %s' % job )
113115 self .child = os .fork ()
114116 if self .child :
115- print 'Forked %s at %s' % (self .child , datetime .datetime .now ())
117+ logging . info ( 'Forked %s at %s' % (self .child , datetime .datetime .now () ))
116118 try :
117119 os .waitpid (self .child , 0 )
118120 except OSError , ose :
119121 import errno
120122 if ose .errno != errno .EINTR :
121123 raise ose
122124 #os.wait()
123- print 'Done waiting'
125+ logging . debug ( 'done waiting')
124126 else :
125- print 'Processing %s since %s' % (job ._queue , datetime .datetime .now ())
127+ logging . info ( 'Processing %s since %s' % (job ._queue , datetime .datetime .now () ))
126128 self .process (job )
127129 os ._exit (0 )
128130 self .child = None
@@ -140,36 +142,37 @@ def process(self, job=None):
140142 job .perform ()
141143 except Exception , e :
142144 exceptionType , exceptionValue , exceptionTraceback = sys .exc_info ()
143- print "%s failed: %s" % (job , e )
145+ logging . error ( "%s failed: %s" % (job , e ) )
144146 job .fail (exceptionTraceback )
145147 self .failed ()
146148 else :
147- print "done: %s" % job
149+ logging .info ('completed job' )
150+ logging .debug ('job details: %s' % job )
148151 finally :
149152 self .done_working ()
150153
151154 def reserve (self ):
152155 for q in self .queues :
153- print "Checking %s" % q
156+ logging . debug ( 'checking queue %s' % q )
154157 job = Job .reserve (q , self .resq , self .__str__ ())
155158 if job :
156- print " Found job on %s" % q
159+ logging . info ( ' Found job on %s' % q )
157160 return job
158161
159162 def working_on (self , job ):
160- print 'marking as working on'
163+ logging . debug ( 'marking as working on' )
161164 data = {
162165 'queue' : job ._queue ,
163166 'run_at' : str (datetime .datetime .now ()),
164167 'payload' : job ._payload
165168 }
166169 data = json .dumps (data )
167170 self .resq .redis ["resque:worker:%s" % str (self )] = data
168- print "worker:%s" % str (self )
169- print self .resq .redis ["resque:worker:%s" % str (self )]
171+ logging . debug ( "worker:%s" % str (self ) )
172+ logging . debug ( self .resq .redis ["resque:worker:%s" % str (self )])
170173
171174 def done_working (self ):
172- print 'done working'
175+ logging . info ( 'done working' )
173176 self .processed ()
174177 self .resq .redis .delete ("resque:worker:%s" % str (self ))
175178
0 commit comments