1- #!/usr/bin/env python
21# Licensed to the Apache Software Foundation (ASF) under one
32# or more contributor license agreements. See the NOTICE file
43# distributed with this work for additional information
1817#
1918# Automatically generated by addcopyright.py at 01/29/2013
2019
21- import sys , os , time , atexit
22- import traceback
20+ #!/usr/bin/env python
21+
22+ import sys , os , time , atexit
23+ import traceback
2324import subprocess
24- from signal import SIGTERM
25+ from signal import SIGTERM , SIGKILL
2526import cherrypy
2627import copy
2728
29+ import weakref
30+ import threading
31+ import functools
32+
33+ _internal_lock = threading .RLock ()
34+ _locks = weakref .WeakValueDictionary ()
35+
36+ def _get_lock (name ):
37+ with _internal_lock :
38+ lock = _locks .get (name , threading .RLock ())
39+ if not name in _locks :
40+ _locks [name ] = lock
41+ return lock
42+
43+ class NamedLock (object ):
44+ def __init__ (self , name ):
45+ self .name = name
46+ self .lock = None
47+
48+ def __enter__ (self ):
49+ self .lock = _get_lock (self .name )
50+ self .lock .acquire ()
51+ #logger.debug('%s got lock %s' % (threading.current_thread().name, self.name))
52+
53+ def __exit__ (self , type , value , traceback ):
54+ self .lock .release ()
55+ #logger.debug('%s released lock %s' % (threading.current_thread().name, self.name))
56+
57+
58+ def lock (name = 'defaultLock' ):
59+ def wrap (f ):
60+ @functools .wraps (f )
61+ def inner (* args , ** kwargs ):
62+ with NamedLock (name ):
63+ retval = f (* args , ** kwargs )
64+ return retval
65+ return inner
66+ return wrap
67+
68+
2869class Request (object ):
2970 def __init__ (self ):
3071 self .headers = None
@@ -80,28 +121,28 @@ class Daemon(object):
80121 A generic daemon class.
81122
82123 Usage: subclass the Daemon class and override the run() method
83- """
84- atexit_hooks = []
124+ """
125+ atexit_hooks = []
85126
86127 def __init__ (self , pidfile , stdin = '/dev/null' , stdout = '/dev/null' , stderr = '/dev/null' ):
87128 self .stdin = stdin
88129 self .stdout = stdout
89130 self .stderr = stderr
90131 self .pidfile = pidfile
91-
92- @staticmethod
93- def register_atexit_hook (hook ):
94- Daemon .atexit_hooks .append (hook )
95-
132+
96133 @staticmethod
97- def _atexit ():
98- for hook in Daemon .atexit_hooks :
99- try :
134+ def register_atexit_hook (hook ):
135+ Daemon .atexit_hooks .append (hook )
136+
137+ @staticmethod
138+ def _atexit ():
139+ for hook in Daemon .atexit_hooks :
140+ try :
100141 hook ()
101- except Exception :
142+ except Exception :
102143 content = traceback .format_exc ()
103- err = 'Exception when calling atexit hook[%s]\n %s' % (hook .__name__ , content )
104- #logger.error(err)
144+ err = 'Exception when calling atexit hook[%s]\n %s' % (hook .__name__ , content )
145+ #logger.error(err)
105146
106147 def daemonize (self ):
107148 """
@@ -145,7 +186,7 @@ def daemonize(self):
145186
146187 # write pidfile
147188 Daemon .register_atexit_hook (self .delpid )
148- atexit .register (Daemon ._atexit )
189+ atexit .register (Daemon ._atexit )
149190 pid = str (os .getpid ())
150191 file (self .pidfile ,'w' ).write ("%s\n " % pid )
151192
@@ -173,12 +214,12 @@ def start(self):
173214 sys .exit (0 )
174215
175216 # Start the daemon
176- self .daemonize ()
217+ self .daemonize ()
177218 try :
178- self .run ()
179- except Exception :
180- content = traceback .format_exc ()
181- #logger.error(content)
219+ self .run ()
220+ except Exception :
221+ content = traceback .format_exc ()
222+ #logger.error(content)
182223 sys .exit (1 )
183224
184225 def stop (self ):
@@ -192,25 +233,29 @@ def stop(self):
192233 pf .close ()
193234 except IOError :
194235 pid = None
195-
236+
196237 if not pid :
197238 message = "pidfile %s does not exist. Daemon not running?\n "
198239 sys .stderr .write (message % self .pidfile )
199240 return # not an error in a restart
200241
201242 # Try killing the daemon process
202- try :
203- while 1 :
204- os .kill (pid , SIGTERM )
205- time .sleep (0.1 )
206- except OSError , err :
207- err = str (err )
208- if err .find ("No such process" ) > 0 :
209- if os .path .exists (self .pidfile ):
210- os .remove (self .pidfile )
243+ wait_stop = 5
244+ start_time = time .time ()
245+ while 1 :
246+ if os .path .exists ('/proc/' + str (pid )):
247+ curr_time = time .time ()
248+ if (curr_time - start_time ) > wait_stop :
249+ os .kill (pid , SIGKILL )
250+ else :
251+ os .kill (pid , SIGTERM )
252+ time .sleep (0.3 )
211253 else :
212- print str (err )
213- sys .exit (1 )
254+ if os .path .exists (self .pidfile ):
255+ self .delpid ()
256+ break
257+
258+ print "Stop Daemon Successfully"
214259
215260 def restart (self ):
216261 """
0 commit comments