process.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. import os
  2. import sys
  3. import time
  4. import errno
  5. import shlex
  6. import StringIO
  7. import traceback
  8. import signal
  9. from supervisor.medusa import asyncore_25 as asyncore
  10. from supervisor.states import ProcessStates
  11. from supervisor.states import SupervisorStates
  12. from supervisor.states import getProcessStateDescription
  13. from supervisor.states import STOPPED_STATES
  14. from supervisor.options import decode_wait_status
  15. from supervisor.options import signame
  16. from supervisor.options import ProcessException, BadCommand
  17. from supervisor.dispatchers import EventListenerStates
  18. from supervisor import events
  19. from supervisor.datatypes import RestartUnconditionally
  20. from supervisor.socket_manager import SocketManager
  21. class Subprocess:
  22. """A class to manage a subprocess."""
  23. # Initial state; overridden by instance variables
  24. pid = 0 # Subprocess pid; 0 when not running
  25. config = None # ProcessConfig instance
  26. state = None # process state code
  27. listener_state = None # listener state code (if we're an event listener)
  28. event = None # event currently being processed (if we're an event listener)
  29. laststart = 0 # Last time the subprocess was started; 0 if never
  30. laststop = 0 # Last time the subprocess was stopped; 0 if never
  31. delay = 0 # If nonzero, delay starting or killing until this time
  32. administrative_stop = 0 # true if the process has been stopped by an admin
  33. system_stop = 0 # true if the process has been stopped by the system
  34. killing = 0 # flag determining whether we are trying to kill this proc
  35. backoff = 0 # backoff counter (to startretries)
  36. dispatchers = None # asnycore output dispatchers (keyed by fd)
  37. pipes = None # map of channel name to file descriptor #
  38. exitstatus = None # status attached to dead process by finsh()
  39. spawnerr = None # error message attached by spawn() if any
  40. group = None # ProcessGroup instance if process is in the group
  41. def __init__(self, config):
  42. """Constructor.
  43. Argument is a ProcessConfig instance.
  44. """
  45. self.config = config
  46. self.dispatchers = {}
  47. self.pipes = {}
  48. self.state = ProcessStates.STOPPED
  49. def removelogs(self):
  50. for dispatcher in self.dispatchers.values():
  51. if hasattr(dispatcher, 'removelogs'):
  52. dispatcher.removelogs()
  53. def reopenlogs(self):
  54. for dispatcher in self.dispatchers.values():
  55. if hasattr(dispatcher, 'reopenlogs'):
  56. dispatcher.reopenlogs()
  57. def drain(self):
  58. for dispatcher in self.dispatchers.values():
  59. # note that we *must* call readable() for every
  60. # dispatcher, as it may have side effects for a given
  61. # dispatcher (eg. call handle_listener_state_change for
  62. # event listener processes)
  63. if dispatcher.readable():
  64. dispatcher.handle_read_event()
  65. if dispatcher.writable():
  66. dispatcher.handle_write_event()
  67. def write(self, chars):
  68. if not self.pid or self.killing:
  69. raise OSError(errno.EPIPE, "Process already closed")
  70. stdin_fd = self.pipes['stdin']
  71. if stdin_fd is None:
  72. raise OSError(errno.EPIPE, "Process has no stdin channel")
  73. dispatcher = self.dispatchers[stdin_fd]
  74. if dispatcher.closed:
  75. raise OSError(errno.EPIPE, "Process' stdin channel is closed")
  76. dispatcher.input_buffer += chars
  77. dispatcher.flush() # this must raise EPIPE if the pipe is closed
  78. def get_execv_args(self):
  79. """Internal: turn a program name into a file name, using $PATH,
  80. make sure it exists / is executable, raising a ProcessException
  81. if not """
  82. try:
  83. commandargs = shlex.split(self.config.command)
  84. except ValueError, e:
  85. raise BadCommand("can't parse command %r: %s" % \
  86. (self.config.command, str(e)))
  87. if commandargs:
  88. program = commandargs[0]
  89. else:
  90. raise BadCommand("command is empty")
  91. if "/" in program:
  92. filename = program
  93. try:
  94. st = self.config.options.stat(filename)
  95. except OSError:
  96. st = None
  97. else:
  98. path = self.config.options.get_path()
  99. found = None
  100. st = None
  101. for dir in path:
  102. found = os.path.join(dir, program)
  103. try:
  104. st = self.config.options.stat(found)
  105. except OSError:
  106. pass
  107. else:
  108. break
  109. if st is None:
  110. filename = program
  111. else:
  112. filename = found
  113. # check_execv_args will raise a ProcessException if the execv
  114. # args are bogus, we break it out into a separate options
  115. # method call here only to service unit tests
  116. self.config.options.check_execv_args(filename, commandargs, st)
  117. return filename, commandargs
  118. event_map = {
  119. ProcessStates.BACKOFF: events.ProcessStateBackoffEvent,
  120. ProcessStates.FATAL: events.ProcessStateFatalEvent,
  121. ProcessStates.UNKNOWN: events.ProcessStateUnknownEvent,
  122. ProcessStates.STOPPED: events.ProcessStateStoppedEvent,
  123. ProcessStates.EXITED: events.ProcessStateExitedEvent,
  124. ProcessStates.RUNNING: events.ProcessStateRunningEvent,
  125. ProcessStates.STARTING: events.ProcessStateStartingEvent,
  126. ProcessStates.STOPPING: events.ProcessStateStoppingEvent,
  127. }
  128. def change_state(self, new_state, expected=True):
  129. old_state = self.state
  130. if new_state is old_state:
  131. # exists for unit tests
  132. return False
  133. event_class = self.event_map.get(new_state)
  134. if event_class is not None:
  135. event = event_class(self, old_state, expected)
  136. events.notify(event)
  137. if new_state == ProcessStates.BACKOFF:
  138. now = time.time()
  139. self.backoff = self.backoff + 1
  140. self.delay = now + self.backoff
  141. self.state = new_state
  142. def _assertInState(self, *states):
  143. if self.state not in states:
  144. current_state = getProcessStateDescription(self.state)
  145. allowable_states = ' '.join(map(getProcessStateDescription, states))
  146. raise AssertionError('Assertion failed for %s: %s not in %s' % (
  147. self.config.name, current_state, allowable_states))
  148. def record_spawnerr(self, msg):
  149. self.spawnerr = msg
  150. self.config.options.logger.info("spawnerr: %s" % msg)
  151. def spawn(self):
  152. """Start the subprocess. It must not be running already.
  153. Return the process id. If the fork() call fails, return None.
  154. """
  155. options = self.config.options
  156. if self.pid:
  157. msg = 'process %r already running' % self.config.name
  158. options.logger.warn(msg)
  159. return
  160. self.killing = 0
  161. self.spawnerr = None
  162. self.exitstatus = None
  163. self.system_stop = 0
  164. self.administrative_stop = 0
  165. self.laststart = time.time()
  166. self._assertInState(ProcessStates.EXITED, ProcessStates.FATAL,
  167. ProcessStates.BACKOFF, ProcessStates.STOPPED)
  168. self.change_state(ProcessStates.STARTING)
  169. try:
  170. filename, argv = self.get_execv_args()
  171. except ProcessException, what:
  172. self.record_spawnerr(what.args[0])
  173. self._assertInState(ProcessStates.STARTING)
  174. self.change_state(ProcessStates.BACKOFF)
  175. return
  176. try:
  177. self.dispatchers, self.pipes = self.config.make_dispatchers(self)
  178. except (OSError, IOError), why:
  179. code = why.args[0]
  180. if code == errno.EMFILE:
  181. # too many file descriptors open
  182. msg = 'too many open files to spawn %r' % self.config.name
  183. else:
  184. msg = ('unknown error making dispatchers: %s' %
  185. errno.errorcode.get(code, code))
  186. self.record_spawnerr(msg)
  187. self._assertInState(ProcessStates.STARTING)
  188. self.change_state(ProcessStates.BACKOFF)
  189. return
  190. try:
  191. pid = options.fork()
  192. except OSError, why:
  193. code = why.args[0]
  194. if code == errno.EAGAIN:
  195. # process table full
  196. msg = ('Too many processes in process table to spawn %r' %
  197. self.config.name)
  198. else:
  199. msg = ('unknown error during fork: %s' %
  200. errno.errorcode.get(code, code))
  201. self.record_spawnerr(msg)
  202. self._assertInState(ProcessStates.STARTING)
  203. self.change_state(ProcessStates.BACKOFF)
  204. options.close_parent_pipes(self.pipes)
  205. options.close_child_pipes(self.pipes)
  206. return
  207. if pid != 0:
  208. return self._spawn_as_parent(pid)
  209. else:
  210. return self._spawn_as_child(filename, argv)
  211. def _spawn_as_parent(self, pid):
  212. # Parent
  213. self.pid = pid
  214. options = self.config.options
  215. options.close_child_pipes(self.pipes)
  216. options.logger.info('spawned: %r with pid %s' % (self.config.name, pid))
  217. self.spawnerr = None
  218. self.delay = time.time() + self.config.startsecs
  219. options.pidhistory[pid] = self
  220. return pid
  221. def _prepare_child_fds(self):
  222. options = self.config.options
  223. options.dup2(self.pipes['child_stdin'], 0)
  224. options.dup2(self.pipes['child_stdout'], 1)
  225. if self.config.redirect_stderr:
  226. options.dup2(self.pipes['child_stdout'], 2)
  227. else:
  228. options.dup2(self.pipes['child_stderr'], 2)
  229. for i in range(3, options.minfds):
  230. options.close_fd(i)
  231. def _spawn_as_child(self, filename, argv):
  232. options = self.config.options
  233. try:
  234. # prevent child from receiving signals sent to the
  235. # parent by calling os.setpgrp to create a new process
  236. # group for the child; this prevents, for instance,
  237. # the case of child processes being sent a SIGINT when
  238. # running supervisor in foreground mode and Ctrl-C in
  239. # the terminal window running supervisord is pressed.
  240. # Presumably it also prevents HUP, etc received by
  241. # supervisord from being sent to children.
  242. options.setpgrp()
  243. self._prepare_child_fds()
  244. # sending to fd 2 will put this output in the stderr log
  245. # set user
  246. setuid_msg = self.set_uid()
  247. if setuid_msg:
  248. uid = self.config.uid
  249. msg = "couldn't setuid to %s: %s\n" % (uid, setuid_msg)
  250. options.write(2, "supervisor: " + msg)
  251. return # finally clause will exit the child process
  252. # set environment
  253. env = os.environ.copy()
  254. env['SUPERVISOR_ENABLED'] = '1'
  255. serverurl = self.config.serverurl
  256. if serverurl is None: # unset
  257. serverurl = self.config.options.serverurl # might still be None
  258. if serverurl:
  259. env['SUPERVISOR_SERVER_URL'] = serverurl
  260. env['SUPERVISOR_PROCESS_NAME'] = self.config.name
  261. if self.group:
  262. env['SUPERVISOR_GROUP_NAME'] = self.group.config.name
  263. if self.config.environment is not None:
  264. env.update(self.config.environment)
  265. # change directory
  266. try:
  267. cwd = self.config.directory
  268. if cwd is not None:
  269. options.chdir(cwd)
  270. except OSError, why:
  271. code = errno.errorcode.get(why.args[0], why.args[0])
  272. msg = "couldn't chdir to %s: %s\n" % (cwd, code)
  273. options.write(2, "supervisor: " + msg)
  274. return # finally clause will exit the child process
  275. # set umask, then execve
  276. try:
  277. if self.config.umask is not None:
  278. options.setumask(self.config.umask)
  279. options.execve(filename, argv, env)
  280. except OSError, why:
  281. code = errno.errorcode.get(why.args[0], why.args[0])
  282. msg = "couldn't exec %s: %s\n" % (argv[0], code)
  283. options.write(2, "supervisor: " + msg)
  284. except:
  285. (file, fun, line), t,v,tbinfo = asyncore.compact_traceback()
  286. error = '%s, %s: file: %s line: %s' % (t, v, file, line)
  287. msg = "couldn't exec %s: %s\n" % (filename, error)
  288. options.write(2, "supervisor: " + msg)
  289. # this point should only be reached if execve failed.
  290. # the finally clause will exit the child process.
  291. finally:
  292. options.write(2, "supervisor: child process was not spawned\n")
  293. options._exit(127) # exit process with code for spawn failure
  294. def stop(self):
  295. """ Administrative stop """
  296. self.administrative_stop = 1
  297. return self.kill(self.config.stopsignal)
  298. def give_up(self):
  299. self.delay = 0
  300. self.backoff = 0
  301. self.system_stop = 1
  302. self._assertInState(ProcessStates.BACKOFF)
  303. self.change_state(ProcessStates.FATAL)
  304. def kill(self, sig):
  305. """Send a signal to the subprocess. This may or may not kill it.
  306. Return None if the signal was sent, or an error message string
  307. if an error occurred or if the subprocess is not running.
  308. """
  309. now = time.time()
  310. options = self.config.options
  311. # Properly stop processes in BACKOFF state.
  312. if self.state == ProcessStates.BACKOFF:
  313. msg = ("Attempted to kill %s, which is in BACKOFF state." %
  314. (self.config.name))
  315. options.logger.debug(msg)
  316. self.change_state(ProcessStates.STOPPED)
  317. return None
  318. if not self.pid:
  319. msg = ("attempted to kill %s with sig %s but it wasn't running" %
  320. (self.config.name, signame(sig)))
  321. options.logger.debug(msg)
  322. return msg
  323. #If we're in the stopping state, then we've already sent the stop
  324. #signal and this is the kill signal
  325. if self.state == ProcessStates.STOPPING:
  326. killasgroup = self.config.killasgroup
  327. else:
  328. killasgroup = self.config.stopasgroup
  329. as_group = ""
  330. if killasgroup:
  331. as_group = "process group "
  332. options.logger.debug('killing %s (pid %s) %swith signal %s'
  333. % (self.config.name,
  334. self.pid,
  335. as_group,
  336. signame(sig))
  337. )
  338. # RUNNING/STARTING/STOPPING -> STOPPING
  339. self.killing = 1
  340. self.delay = now + self.config.stopwaitsecs
  341. # we will already be in the STOPPING state if we're doing a
  342. # SIGKILL as a result of overrunning stopwaitsecs
  343. self._assertInState(ProcessStates.RUNNING,ProcessStates.STARTING,
  344. ProcessStates.STOPPING)
  345. self.change_state(ProcessStates.STOPPING)
  346. pid = self.pid
  347. if killasgroup:
  348. # send to the whole process group instead
  349. pid = -self.pid
  350. try:
  351. options.kill(pid, sig)
  352. except:
  353. io = StringIO.StringIO()
  354. traceback.print_exc(file=io)
  355. tb = io.getvalue()
  356. msg = 'unknown problem killing %s (%s):%s' % (self.config.name,
  357. self.pid, tb)
  358. options.logger.critical(msg)
  359. self.change_state(ProcessStates.UNKNOWN)
  360. self.pid = 0
  361. self.killing = 0
  362. self.delay = 0
  363. return msg
  364. return None
  365. def finish(self, pid, sts):
  366. """ The process was reaped and we need to report and manage its state
  367. """
  368. self.drain()
  369. es, msg = decode_wait_status(sts)
  370. now = time.time()
  371. self.laststop = now
  372. processname = self.config.name
  373. tooquickly = now - self.laststart < self.config.startsecs
  374. exit_expected = es in self.config.exitcodes
  375. if self.killing:
  376. # likely the result of a stop request
  377. # implies STOPPING -> STOPPED
  378. self.killing = 0
  379. self.delay = 0
  380. self.exitstatus = es
  381. msg = "stopped: %s (%s)" % (processname, msg)
  382. self._assertInState(ProcessStates.STOPPING)
  383. self.change_state(ProcessStates.STOPPED)
  384. elif tooquickly:
  385. # the program did not stay up long enough to make it to RUNNING
  386. # implies STARTING -> BACKOFF
  387. self.exitstatus = None
  388. self.spawnerr = 'Exited too quickly (process log may have details)'
  389. msg = "exited: %s (%s)" % (processname, msg + "; not expected")
  390. self._assertInState(ProcessStates.STARTING)
  391. self.change_state(ProcessStates.BACKOFF)
  392. else:
  393. # this finish was not the result of a stop request, the
  394. # program was in the RUNNING state but exited implies
  395. # RUNNING -> EXITED
  396. self.delay = 0
  397. self.backoff = 0
  398. self.exitstatus = es
  399. if self.state == ProcessStates.STARTING: # pragma: no cover
  400. # XXX I don't know under which circumstances this
  401. # happens, but in the wild, there is a transition that
  402. # subverts the RUNNING state (directly from STARTING
  403. # to EXITED), so we perform the correct transition
  404. # here.
  405. self.change_state(ProcessStates.RUNNING)
  406. self._assertInState(ProcessStates.RUNNING)
  407. if exit_expected:
  408. # expected exit code
  409. msg = "exited: %s (%s)" % (processname, msg + "; expected")
  410. self.change_state(ProcessStates.EXITED, expected=True)
  411. else:
  412. # unexpected exit code
  413. self.spawnerr = 'Bad exit code %s' % es
  414. msg = "exited: %s (%s)" % (processname, msg + "; not expected")
  415. self.change_state(ProcessStates.EXITED, expected=False)
  416. self.config.options.logger.info(msg)
  417. self.pid = 0
  418. self.config.options.close_parent_pipes(self.pipes)
  419. self.pipes = {}
  420. self.dispatchers = {}
  421. # if we died before we processed the current event (only happens
  422. # if we're an event listener), notify the event system that this
  423. # event was rejected so it can be processed again.
  424. if self.event is not None:
  425. # Note: this should only be true if we were in the BUSY
  426. # state when finish() was called.
  427. events.notify(events.EventRejectedEvent(self, self.event))
  428. self.event = None
  429. def set_uid(self):
  430. if self.config.uid is None:
  431. return
  432. msg = self.config.options.dropPrivileges(self.config.uid)
  433. return msg
  434. def __cmp__(self, other):
  435. # sort by priority
  436. return cmp(self.config.priority, other.config.priority)
  437. def __repr__(self):
  438. return '<Subprocess at %s with name %s in state %s>' % (
  439. id(self),
  440. self.config.name,
  441. getProcessStateDescription(self.get_state()))
  442. def get_state(self):
  443. return self.state
  444. def transition(self):
  445. now = time.time()
  446. state = self.state
  447. logger = self.config.options.logger
  448. if self.config.options.mood > SupervisorStates.RESTARTING:
  449. # dont start any processes if supervisor is shutting down
  450. if state == ProcessStates.EXITED:
  451. if self.config.autorestart:
  452. if self.config.autorestart is RestartUnconditionally:
  453. # EXITED -> STARTING
  454. self.spawn()
  455. else: # autorestart is RestartWhenExitUnexpected
  456. if self.exitstatus not in self.config.exitcodes:
  457. # EXITED -> STARTING
  458. self.spawn()
  459. elif state == ProcessStates.STOPPED and not self.laststart:
  460. if self.config.autostart:
  461. # STOPPED -> STARTING
  462. self.spawn()
  463. elif state == ProcessStates.BACKOFF:
  464. if self.backoff <= self.config.startretries:
  465. if now > self.delay:
  466. # BACKOFF -> STARTING
  467. self.spawn()
  468. if state == ProcessStates.STARTING:
  469. if now - self.laststart > self.config.startsecs:
  470. # STARTING -> RUNNING if the proc has started
  471. # successfully and it has stayed up for at least
  472. # proc.config.startsecs,
  473. self.delay = 0
  474. self.backoff = 0
  475. self._assertInState(ProcessStates.STARTING)
  476. self.change_state(ProcessStates.RUNNING)
  477. msg = (
  478. 'entered RUNNING state, process has stayed up for '
  479. '> than %s seconds (startsecs)' % self.config.startsecs)
  480. logger.info('success: %s %s' % (self.config.name, msg))
  481. if state == ProcessStates.BACKOFF:
  482. if self.backoff > self.config.startretries:
  483. # BACKOFF -> FATAL if the proc has exceeded its number
  484. # of retries
  485. self.give_up()
  486. msg = ('entered FATAL state, too many start retries too '
  487. 'quickly')
  488. logger.info('gave up: %s %s' % (self.config.name, msg))
  489. elif state == ProcessStates.STOPPING:
  490. time_left = self.delay - now
  491. if time_left <= 0:
  492. # kill processes which are taking too long to stop with a final
  493. # sigkill. if this doesn't kill it, the process will be stuck
  494. # in the STOPPING state forever.
  495. self.config.options.logger.warn(
  496. 'killing %r (%s) with SIGKILL' % (self.config.name,
  497. self.pid))
  498. self.kill(signal.SIGKILL)
  499. class FastCGISubprocess(Subprocess):
  500. """Extends Subprocess class to handle FastCGI subprocesses"""
  501. def __init__(self, config):
  502. Subprocess.__init__(self, config)
  503. self.fcgi_sock = None
  504. def before_spawn(self):
  505. """
  506. The FastCGI socket needs to be created by the parent before we fork
  507. """
  508. if self.group is None:
  509. raise NotImplementedError('No group set for FastCGISubprocess')
  510. if not hasattr(self.group, 'socket_manager'):
  511. raise NotImplementedError('No SocketManager set for '
  512. '%s:%s' % (self.group, dir(self.group)))
  513. self.fcgi_sock = self.group.socket_manager.get_socket()
  514. def spawn(self):
  515. """
  516. Overrides Subprocess.spawn() so we can hook in before it happens
  517. """
  518. self.before_spawn()
  519. pid = Subprocess.spawn(self)
  520. if pid is None:
  521. #Remove object reference to decrement the reference count on error
  522. self.fcgi_sock = None
  523. return pid
  524. def after_finish(self):
  525. """
  526. Releases reference to FastCGI socket when process is reaped
  527. """
  528. #Remove object reference to decrement the reference count
  529. self.fcgi_sock = None
  530. def finish(self, pid, sts):
  531. """
  532. Overrides Subprocess.finish() so we can hook in after it happens
  533. """
  534. retval = Subprocess.finish(self, pid, sts)
  535. self.after_finish()
  536. return retval
  537. def _prepare_child_fds(self):
  538. """
  539. Overrides Subprocess._prepare_child_fds()
  540. The FastCGI socket needs to be set to file descriptor 0 in the child
  541. """
  542. sock_fd = self.fcgi_sock.fileno()
  543. options = self.config.options
  544. options.dup2(sock_fd, 0)
  545. options.dup2(self.pipes['child_stdout'], 1)
  546. if self.config.redirect_stderr:
  547. options.dup2(self.pipes['child_stdout'], 2)
  548. else:
  549. options.dup2(self.pipes['child_stderr'], 2)
  550. for i in range(3, options.minfds):
  551. options.close_fd(i)
  552. class ProcessGroupBase:
  553. def __init__(self, config):
  554. self.config = config
  555. self.processes = {}
  556. for pconfig in self.config.process_configs:
  557. self.processes[pconfig.name] = pconfig.make_process(self)
  558. def __cmp__(self, other):
  559. return cmp(self.config.priority, other.config.priority)
  560. def __repr__(self):
  561. return '<%s instance at %s named %s>' % (self.__class__, id(self),
  562. self.config.name)
  563. def removelogs(self):
  564. for process in self.processes.values():
  565. process.removelogs()
  566. def reopenlogs(self):
  567. for process in self.processes.values():
  568. process.reopenlogs()
  569. def stop_all(self):
  570. processes = self.processes.values()
  571. processes.sort()
  572. processes.reverse() # stop in desc priority order
  573. for proc in processes:
  574. state = proc.get_state()
  575. if state == ProcessStates.RUNNING:
  576. # RUNNING -> STOPPING
  577. proc.stop()
  578. elif state == ProcessStates.STARTING:
  579. # STARTING -> STOPPING
  580. proc.stop()
  581. elif state == ProcessStates.BACKOFF:
  582. # BACKOFF -> FATAL
  583. proc.give_up()
  584. def get_unstopped_processes(self):
  585. """ Processes which aren't in a state that is considered 'stopped' """
  586. return [ x for x in self.processes.values() if x.get_state() not in
  587. STOPPED_STATES ]
  588. def get_dispatchers(self):
  589. dispatchers = {}
  590. for process in self.processes.values():
  591. dispatchers.update(process.dispatchers)
  592. return dispatchers
  593. class ProcessGroup(ProcessGroupBase):
  594. def transition(self):
  595. for proc in self.processes.values():
  596. proc.transition()
  597. class FastCGIProcessGroup(ProcessGroup):
  598. def __init__(self, config, **kwargs):
  599. ProcessGroup.__init__(self, config)
  600. sockManagerKlass = kwargs.get('socketManager', SocketManager)
  601. self.socket_manager = sockManagerKlass(config.socket_config,
  602. logger=config.options.logger)
  603. # It's not required to call get_socket() here but we want
  604. # to fail early during start up if there is a config error
  605. try:
  606. self.socket_manager.get_socket()
  607. except Exception, e:
  608. raise ValueError('Could not create FastCGI socket %s: %s' % (self.socket_manager.config(), e))
  609. class EventListenerPool(ProcessGroupBase):
  610. def __init__(self, config):
  611. ProcessGroupBase.__init__(self, config)
  612. self.event_buffer = []
  613. for event_type in self.config.pool_events:
  614. events.subscribe(event_type, self._acceptEvent)
  615. events.subscribe(events.EventRejectedEvent, self.handle_rejected)
  616. self.serial = -1
  617. self.last_dispatch = 0
  618. self.dispatch_throttle = 0 # in seconds: .00195 is an interesting one
  619. def handle_rejected(self, event):
  620. process = event.process
  621. procs = self.processes.values()
  622. if process in procs: # this is one of our processes
  623. # rebuffer the event
  624. self._acceptEvent(event.event, head=True)
  625. def transition(self):
  626. processes = self.processes.values()
  627. dispatch_capable = False
  628. for process in processes:
  629. process.transition()
  630. # this is redundant, we do it in _dispatchEvent too, but we
  631. # want to reduce function call overhead
  632. if process.state == ProcessStates.RUNNING:
  633. if process.listener_state == EventListenerStates.READY:
  634. dispatch_capable = True
  635. if dispatch_capable:
  636. if self.dispatch_throttle:
  637. now = time.time()
  638. if now - self.last_dispatch < self.dispatch_throttle:
  639. return
  640. self.dispatch()
  641. def dispatch(self):
  642. while self.event_buffer:
  643. # dispatch the oldest event
  644. event = self.event_buffer.pop(0)
  645. ok = self._dispatchEvent(event)
  646. if not ok:
  647. # if we can't dispatch an event, rebuffer it and stop trying
  648. # to process any further events in the buffer
  649. self._acceptEvent(event, head=True)
  650. break
  651. self.last_dispatch = time.time()
  652. def _acceptEvent(self, event, head=False):
  653. # events are required to be instances
  654. # this has a side effect to fail with an attribute error on 'old style' classes
  655. if not hasattr(event, 'serial'):
  656. event.serial = new_serial(GlobalSerial)
  657. if not hasattr(event, 'pool_serials'):
  658. event.pool_serials = {}
  659. if not event.pool_serials.has_key(self.config.name):
  660. event.pool_serials[self.config.name] = new_serial(self)
  661. else:
  662. self.config.options.logger.debug(
  663. 'rebuffering event %s for pool %s (bufsize %s)' % (
  664. (event.serial, self.config.name, len(self.event_buffer))))
  665. if len(self.event_buffer) >= self.config.buffer_size:
  666. if self.event_buffer:
  667. # discard the oldest event
  668. discarded_event = self.event_buffer.pop(0)
  669. self.config.options.logger.error(
  670. 'pool %s event buffer overflowed, discarding event %s' % (
  671. (self.config.name, discarded_event.serial)))
  672. if head:
  673. self.event_buffer.insert(0, event)
  674. else:
  675. self.event_buffer.append(event)
  676. def _dispatchEvent(self, event):
  677. pool_serial = event.pool_serials[self.config.name]
  678. for process in self.processes.values():
  679. if process.state != ProcessStates.RUNNING:
  680. continue
  681. if process.listener_state == EventListenerStates.READY:
  682. payload = str(event)
  683. try:
  684. event_type = event.__class__
  685. serial = event.serial
  686. envelope = self._eventEnvelope(event_type, serial,
  687. pool_serial, payload)
  688. process.write(envelope)
  689. except OSError, why:
  690. if why.args[0] != errno.EPIPE:
  691. raise
  692. continue
  693. process.listener_state = EventListenerStates.BUSY
  694. process.event = event
  695. self.config.options.logger.debug(
  696. 'event %s sent to listener %s' % (
  697. event.serial, process.config.name))
  698. return True
  699. return False
  700. def _eventEnvelope(self, event_type, serial, pool_serial, payload):
  701. event_name = events.getEventNameByType(event_type)
  702. payload_len = len(payload)
  703. D = {
  704. 'ver':'3.0',
  705. 'sid':self.config.options.identifier,
  706. 'serial':serial,
  707. 'pool_name':self.config.name,
  708. 'pool_serial':pool_serial,
  709. 'event_name':event_name,
  710. 'len':payload_len,
  711. 'payload':payload,
  712. }
  713. return ('ver:%(ver)s server:%(sid)s serial:%(serial)s '
  714. 'pool:%(pool_name)s poolserial:%(pool_serial)s '
  715. 'eventname:%(event_name)s len:%(len)s\n%(payload)s' % D)
  716. class GlobalSerial:
  717. def __init__(self):
  718. self.serial = -1
  719. GlobalSerial = GlobalSerial() # singleton
  720. def new_serial(inst):
  721. if inst.serial == sys.maxint:
  722. inst.serial = -1
  723. inst.serial += 1
  724. return inst.serial