process.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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, why:
  179. code = why[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: %s' % errno.errorcode.get(code, code)
  185. self.record_spawnerr(msg)
  186. self._assertInState(ProcessStates.STARTING)
  187. self.change_state(ProcessStates.BACKOFF)
  188. return
  189. try:
  190. pid = options.fork()
  191. except OSError, why:
  192. code = why[0]
  193. if code == errno.EAGAIN:
  194. # process table full
  195. msg = ('Too many processes in process table to spawn %r' %
  196. self.config.name)
  197. else:
  198. msg = 'unknown error: %s' % errno.errorcode.get(code, code)
  199. self.record_spawnerr(msg)
  200. self._assertInState(ProcessStates.STARTING)
  201. self.change_state(ProcessStates.BACKOFF)
  202. options.close_parent_pipes(self.pipes)
  203. options.close_child_pipes(self.pipes)
  204. return
  205. if pid != 0:
  206. return self._spawn_as_parent(pid)
  207. else:
  208. return self._spawn_as_child(filename, argv)
  209. def _spawn_as_parent(self, pid):
  210. # Parent
  211. self.pid = pid
  212. options = self.config.options
  213. options.close_child_pipes(self.pipes)
  214. options.logger.info('spawned: %r with pid %s' % (self.config.name, pid))
  215. self.spawnerr = None
  216. self.delay = time.time() + self.config.startsecs
  217. options.pidhistory[pid] = self
  218. return pid
  219. def _prepare_child_fds(self):
  220. options = self.config.options
  221. options.dup2(self.pipes['child_stdin'], 0)
  222. options.dup2(self.pipes['child_stdout'], 1)
  223. if self.config.redirect_stderr:
  224. options.dup2(self.pipes['child_stdout'], 2)
  225. else:
  226. options.dup2(self.pipes['child_stderr'], 2)
  227. for i in range(3, options.minfds):
  228. options.close_fd(i)
  229. def _spawn_as_child(self, filename, argv):
  230. options = self.config.options
  231. try:
  232. # prevent child from receiving signals sent to the
  233. # parent by calling os.setpgrp to create a new process
  234. # group for the child; this prevents, for instance,
  235. # the case of child processes being sent a SIGINT when
  236. # running supervisor in foreground mode and Ctrl-C in
  237. # the terminal window running supervisord is pressed.
  238. # Presumably it also prevents HUP, etc received by
  239. # supervisord from being sent to children.
  240. options.setpgrp()
  241. self._prepare_child_fds()
  242. # sending to fd 2 will put this output in the stderr log
  243. msg = self.set_uid()
  244. if msg:
  245. uid = self.config.uid
  246. s = 'supervisor: error trying to setuid to %s ' % uid
  247. options.write(2, s)
  248. options.write(2, "(%s)\n" % msg)
  249. env = os.environ.copy()
  250. env['SUPERVISOR_ENABLED'] = '1'
  251. serverurl = self.config.serverurl
  252. if serverurl is None: # unset
  253. serverurl = self.config.options.serverurl # might still be None
  254. if serverurl:
  255. env['SUPERVISOR_SERVER_URL'] = serverurl
  256. env['SUPERVISOR_PROCESS_NAME'] = self.config.name
  257. if self.group:
  258. env['SUPERVISOR_GROUP_NAME'] = self.group.config.name
  259. if self.config.environment is not None:
  260. env.update(self.config.environment)
  261. try:
  262. cwd = self.config.directory
  263. if cwd is not None:
  264. options.chdir(cwd)
  265. except OSError, why:
  266. code = errno.errorcode.get(why[0], why[0])
  267. msg = "couldn't chdir to %s: %s\n" % (cwd, code)
  268. options.write(2, msg)
  269. else:
  270. try:
  271. if self.config.umask is not None:
  272. options.setumask(self.config.umask)
  273. options.execve(filename, argv, env)
  274. except OSError, why:
  275. code = errno.errorcode.get(why[0], why[0])
  276. msg = "couldn't exec %s: %s\n" % (argv[0], code)
  277. options.write(2, msg)
  278. except:
  279. (file, fun, line), t,v,tbinfo = asyncore.compact_traceback()
  280. error = '%s, %s: file: %s line: %s' % (t, v, file, line)
  281. options.write(2, "couldn't exec %s: %s\n" % (filename,
  282. error))
  283. finally:
  284. options._exit(127)
  285. def stop(self):
  286. """ Administrative stop """
  287. self.administrative_stop = 1
  288. return self.kill(self.config.stopsignal)
  289. def give_up(self):
  290. self.delay = 0
  291. self.backoff = 0
  292. self.system_stop = 1
  293. self._assertInState(ProcessStates.BACKOFF)
  294. self.change_state(ProcessStates.FATAL)
  295. def kill(self, sig):
  296. """Send a signal to the subprocess. This may or may not kill it.
  297. Return None if the signal was sent, or an error message string
  298. if an error occurred or if the subprocess is not running.
  299. """
  300. now = time.time()
  301. options = self.config.options
  302. if not self.pid:
  303. msg = ("attempted to kill %s with sig %s but it wasn't running" %
  304. (self.config.name, signame(sig)))
  305. options.logger.debug(msg)
  306. return msg
  307. #If we're in the stopping state, then we've already sent the stop
  308. #signal and this is the kill signal
  309. if self.state == ProcessStates.STOPPING:
  310. killasgroup = self.config.killasgroup
  311. else:
  312. killasgroup = self.config.stopasgroup
  313. as_group = ""
  314. if killasgroup:
  315. as_group = "process group "
  316. options.logger.debug('killing %s (pid %s) %swith signal %s'
  317. % (self.config.name,
  318. self.pid,
  319. as_group,
  320. signame(sig))
  321. )
  322. # RUNNING/STARTING/STOPPING -> STOPPING
  323. self.killing = 1
  324. self.delay = now + self.config.stopwaitsecs
  325. # we will already be in the STOPPING state if we're doing a
  326. # SIGKILL as a result of overrunning stopwaitsecs
  327. self._assertInState(ProcessStates.RUNNING,ProcessStates.STARTING,
  328. ProcessStates.STOPPING)
  329. self.change_state(ProcessStates.STOPPING)
  330. pid = self.pid
  331. if killasgroup:
  332. # send to the whole process group instead
  333. pid = -self.pid
  334. try:
  335. options.kill(pid, sig)
  336. except:
  337. io = StringIO.StringIO()
  338. traceback.print_exc(file=io)
  339. tb = io.getvalue()
  340. msg = 'unknown problem killing %s (%s):%s' % (self.config.name,
  341. self.pid, tb)
  342. options.logger.critical(msg)
  343. self.change_state(ProcessStates.UNKNOWN)
  344. self.pid = 0
  345. self.killing = 0
  346. self.delay = 0
  347. return msg
  348. return None
  349. def finish(self, pid, sts):
  350. """ The process was reaped and we need to report and manage its state
  351. """
  352. self.drain()
  353. es, msg = decode_wait_status(sts)
  354. now = time.time()
  355. self.laststop = now
  356. processname = self.config.name
  357. tooquickly = now - self.laststart < self.config.startsecs
  358. exit_expected = es in self.config.exitcodes
  359. if self.killing:
  360. # likely the result of a stop request
  361. # implies STOPPING -> STOPPED
  362. self.killing = 0
  363. self.delay = 0
  364. self.exitstatus = es
  365. msg = "stopped: %s (%s)" % (processname, msg)
  366. self._assertInState(ProcessStates.STOPPING)
  367. self.change_state(ProcessStates.STOPPED)
  368. elif tooquickly:
  369. # the program did not stay up long enough to make it to RUNNING
  370. # implies STARTING -> BACKOFF
  371. self.exitstatus = None
  372. self.spawnerr = 'Exited too quickly (process log may have details)'
  373. msg = "exited: %s (%s)" % (processname, msg + "; not expected")
  374. self._assertInState(ProcessStates.STARTING)
  375. self.change_state(ProcessStates.BACKOFF)
  376. else:
  377. # this finish was not the result of a stop request, the
  378. # program was in the RUNNING state but exited implies
  379. # RUNNING -> EXITED
  380. self.delay = 0
  381. self.backoff = 0
  382. self.exitstatus = es
  383. if self.state == ProcessStates.STARTING:
  384. # XXX I dont know under which circumstances this
  385. # happens, but in the wild, there is a transition that
  386. # subverts the RUNNING state (directly from STARTING
  387. # to EXITED), so we perform the correct transition
  388. # here.
  389. self.change_state(ProcessStates.RUNNING)
  390. self._assertInState(ProcessStates.RUNNING)
  391. if exit_expected:
  392. # expected exit code
  393. msg = "exited: %s (%s)" % (processname, msg + "; expected")
  394. self.change_state(ProcessStates.EXITED, expected=True)
  395. else:
  396. # unexpected exit code
  397. self.spawnerr = 'Bad exit code %s' % es
  398. msg = "exited: %s (%s)" % (processname, msg + "; not expected")
  399. self.change_state(ProcessStates.EXITED, expected=False)
  400. self.config.options.logger.info(msg)
  401. self.pid = 0
  402. self.config.options.close_parent_pipes(self.pipes)
  403. self.pipes = {}
  404. self.dispatchers = {}
  405. # if we died before we processed the current event (only happens
  406. # if we're an event listener), notify the event system that this
  407. # event was rejected so it can be processed again.
  408. if self.event is not None:
  409. # Note: this should only be true if we were in the BUSY
  410. # state when finish() was called.
  411. events.notify(events.EventRejectedEvent(self, self.event))
  412. self.event = None
  413. def set_uid(self):
  414. if self.config.uid is None:
  415. return
  416. msg = self.config.options.dropPrivileges(self.config.uid)
  417. return msg
  418. def __cmp__(self, other):
  419. # sort by priority
  420. return cmp(self.config.priority, other.config.priority)
  421. def __repr__(self):
  422. return '<Subprocess at %s with name %s in state %s>' % (
  423. id(self),
  424. self.config.name,
  425. getProcessStateDescription(self.get_state()))
  426. def get_state(self):
  427. return self.state
  428. def transition(self):
  429. now = time.time()
  430. state = self.state
  431. logger = self.config.options.logger
  432. if self.config.options.mood > SupervisorStates.RESTARTING:
  433. # dont start any processes if supervisor is shutting down
  434. if state == ProcessStates.EXITED:
  435. if self.config.autorestart:
  436. if self.config.autorestart is RestartUnconditionally:
  437. # EXITED -> STARTING
  438. self.spawn()
  439. else: # autorestart is RestartWhenExitUnexpected
  440. if self.exitstatus not in self.config.exitcodes:
  441. # EXITED -> STARTING
  442. self.spawn()
  443. elif state == ProcessStates.STOPPED and not self.laststart:
  444. if self.config.autostart:
  445. # STOPPED -> STARTING
  446. self.spawn()
  447. elif state == ProcessStates.BACKOFF:
  448. if self.backoff <= self.config.startretries:
  449. if now > self.delay:
  450. # BACKOFF -> STARTING
  451. self.spawn()
  452. if state == ProcessStates.STARTING:
  453. if now - self.laststart > self.config.startsecs:
  454. # STARTING -> RUNNING if the proc has started
  455. # successfully and it has stayed up for at least
  456. # proc.config.startsecs,
  457. self.delay = 0
  458. self.backoff = 0
  459. self._assertInState(ProcessStates.STARTING)
  460. self.change_state(ProcessStates.RUNNING)
  461. msg = (
  462. 'entered RUNNING state, process has stayed up for '
  463. '> than %s seconds (startsecs)' % self.config.startsecs)
  464. logger.info('success: %s %s' % (self.config.name, msg))
  465. if state == ProcessStates.BACKOFF:
  466. if self.backoff > self.config.startretries:
  467. # BACKOFF -> FATAL if the proc has exceeded its number
  468. # of retries
  469. self.give_up()
  470. msg = ('entered FATAL state, too many start retries too '
  471. 'quickly')
  472. logger.info('gave up: %s %s' % (self.config.name, msg))
  473. elif state == ProcessStates.STOPPING:
  474. time_left = self.delay - now
  475. if time_left <= 0:
  476. # kill processes which are taking too long to stop with a final
  477. # sigkill. if this doesn't kill it, the process will be stuck
  478. # in the STOPPING state forever.
  479. self.config.options.logger.warn(
  480. 'killing %r (%s) with SIGKILL' % (self.config.name,
  481. self.pid))
  482. self.kill(signal.SIGKILL)
  483. class FastCGISubprocess(Subprocess):
  484. """Extends Subprocess class to handle FastCGI subprocesses"""
  485. def __init__(self, config):
  486. Subprocess.__init__(self, config)
  487. self.fcgi_sock = None
  488. def before_spawn(self):
  489. """
  490. The FastCGI socket needs to be created by the parent before we fork
  491. """
  492. if self.group is None:
  493. raise NotImplementedError('No group set for FastCGISubprocess')
  494. if not hasattr(self.group, 'socket_manager'):
  495. raise NotImplementedError('No SocketManager set for '
  496. '%s:%s' % (self.group, dir(self.group)))
  497. self.fcgi_sock = self.group.socket_manager.get_socket()
  498. def spawn(self):
  499. """
  500. Overrides Subprocess.spawn() so we can hook in before it happens
  501. """
  502. self.before_spawn()
  503. pid = Subprocess.spawn(self)
  504. if pid is None:
  505. #Remove object reference to decrement the reference count on error
  506. self.fcgi_sock = None
  507. return pid
  508. def after_finish(self):
  509. """
  510. Releases reference to FastCGI socket when process is reaped
  511. """
  512. #Remove object reference to decrement the reference count
  513. self.fcgi_sock = None
  514. def finish(self, pid, sts):
  515. """
  516. Overrides Subprocess.finish() so we can hook in after it happens
  517. """
  518. retval = Subprocess.finish(self, pid, sts)
  519. self.after_finish()
  520. return retval
  521. def _prepare_child_fds(self):
  522. """
  523. Overrides Subprocess._prepare_child_fds()
  524. The FastCGI socket needs to be set to file descriptor 0 in the child
  525. """
  526. sock_fd = self.fcgi_sock.fileno()
  527. options = self.config.options
  528. options.dup2(sock_fd, 0)
  529. options.dup2(self.pipes['child_stdout'], 1)
  530. if self.config.redirect_stderr:
  531. options.dup2(self.pipes['child_stdout'], 2)
  532. else:
  533. options.dup2(self.pipes['child_stderr'], 2)
  534. for i in range(3, options.minfds):
  535. options.close_fd(i)
  536. class ProcessGroupBase:
  537. def __init__(self, config):
  538. self.config = config
  539. self.processes = {}
  540. for pconfig in self.config.process_configs:
  541. self.processes[pconfig.name] = pconfig.make_process(self)
  542. def __cmp__(self, other):
  543. return cmp(self.config.priority, other.config.priority)
  544. def __repr__(self):
  545. return '<%s instance at %s named %s>' % (self.__class__, id(self),
  546. self.config.name)
  547. def removelogs(self):
  548. for process in self.processes.values():
  549. process.removelogs()
  550. def reopenlogs(self):
  551. for process in self.processes.values():
  552. process.reopenlogs()
  553. def stop_all(self):
  554. processes = self.processes.values()
  555. processes.sort()
  556. processes.reverse() # stop in desc priority order
  557. for proc in processes:
  558. state = proc.get_state()
  559. if state == ProcessStates.RUNNING:
  560. # RUNNING -> STOPPING
  561. proc.stop()
  562. elif state == ProcessStates.STARTING:
  563. # STARTING -> STOPPING
  564. proc.stop()
  565. elif state == ProcessStates.BACKOFF:
  566. # BACKOFF -> FATAL
  567. proc.give_up()
  568. def get_unstopped_processes(self):
  569. """ Processes which aren't in a state that is considered 'stopped' """
  570. return [ x for x in self.processes.values() if x.get_state() not in
  571. STOPPED_STATES ]
  572. def get_dispatchers(self):
  573. dispatchers = {}
  574. for process in self.processes.values():
  575. dispatchers.update(process.dispatchers)
  576. return dispatchers
  577. class ProcessGroup(ProcessGroupBase):
  578. def transition(self):
  579. for proc in self.processes.values():
  580. proc.transition()
  581. class FastCGIProcessGroup(ProcessGroup):
  582. def __init__(self, config, **kwargs):
  583. ProcessGroup.__init__(self, config)
  584. sockManagerKlass = kwargs.get('socketManager', SocketManager)
  585. self.socket_manager = sockManagerKlass(config.socket_config,
  586. logger=config.options.logger)
  587. #It's not required to call get_socket() here but we want
  588. #to fail early during start up if there is a config error
  589. try:
  590. sock = self.socket_manager.get_socket()
  591. except Exception, e:
  592. raise ValueError('Could not create FastCGI socket %s: %s' % (self.socket_manager.config(), e))
  593. class EventListenerPool(ProcessGroupBase):
  594. def __init__(self, config):
  595. ProcessGroupBase.__init__(self, config)
  596. self.event_buffer = []
  597. for event_type in self.config.pool_events:
  598. events.subscribe(event_type, self._acceptEvent)
  599. events.subscribe(events.EventRejectedEvent, self.handle_rejected)
  600. self.serial = -1
  601. self.last_dispatch = 0
  602. self.dispatch_throttle = 0 # in seconds: .00195 is an interesting one
  603. def handle_rejected(self, event):
  604. process = event.process
  605. procs = self.processes.values()
  606. if process in procs: # this is one of our processes
  607. # rebuffer the event
  608. self._acceptEvent(event.event, head=True)
  609. def transition(self):
  610. processes = self.processes.values()
  611. dispatch_capable = False
  612. for process in processes:
  613. process.transition()
  614. # this is redundant, we do it in _dispatchEvent too, but we
  615. # want to reduce function call overhead
  616. if process.state == ProcessStates.RUNNING:
  617. if process.listener_state == EventListenerStates.READY:
  618. dispatch_capable = True
  619. if dispatch_capable:
  620. if self.dispatch_throttle:
  621. now = time.time()
  622. if now - self.last_dispatch < self.dispatch_throttle:
  623. return
  624. self.dispatch()
  625. def dispatch(self):
  626. while self.event_buffer:
  627. # dispatch the oldest event
  628. event = self.event_buffer.pop(0)
  629. ok = self._dispatchEvent(event)
  630. if not ok:
  631. # if we can't dispatch an event, rebuffer it and stop trying
  632. # to process any further events in the buffer
  633. self._acceptEvent(event, head=True)
  634. break
  635. self.last_dispatch = time.time()
  636. def _acceptEvent(self, event, head=False):
  637. # events are required to be instances
  638. # this has a side effect to fail with an attribute error on 'old style' classes
  639. event_type = event.__class__
  640. if not hasattr(event, 'serial'):
  641. event.serial = new_serial(GlobalSerial)
  642. if not hasattr(event, 'pool_serials'):
  643. event.pool_serials = {}
  644. if not event.pool_serials.has_key(self.config.name):
  645. event.pool_serials[self.config.name] = new_serial(self)
  646. else:
  647. self.config.options.logger.debug(
  648. 'rebuffering event %s for pool %s (bufsize %s)' % (
  649. (event.serial, self.config.name, len(self.event_buffer))))
  650. if len(self.event_buffer) >= self.config.buffer_size:
  651. if self.event_buffer:
  652. # discard the oldest event
  653. discarded_event = self.event_buffer.pop(0)
  654. self.config.options.logger.error(
  655. 'pool %s event buffer overflowed, discarding event %s' % (
  656. (self.config.name, discarded_event.serial)))
  657. if head:
  658. self.event_buffer.insert(0, event)
  659. else:
  660. self.event_buffer.append(event)
  661. def _dispatchEvent(self, event):
  662. pool_serial = event.pool_serials[self.config.name]
  663. for process in self.processes.values():
  664. if process.state != ProcessStates.RUNNING:
  665. continue
  666. if process.listener_state == EventListenerStates.READY:
  667. payload = str(event)
  668. try:
  669. event_type = event.__class__
  670. serial = event.serial
  671. envelope = self._eventEnvelope(event_type, serial,
  672. pool_serial, payload)
  673. process.write(envelope)
  674. except OSError, why:
  675. if why[0] != errno.EPIPE:
  676. raise
  677. continue
  678. process.listener_state = EventListenerStates.BUSY
  679. process.event = event
  680. self.config.options.logger.debug(
  681. 'event %s sent to listener %s' % (
  682. event.serial, process.config.name))
  683. return True
  684. return False
  685. def _eventEnvelope(self, event_type, serial, pool_serial, payload):
  686. event_name = events.getEventNameByType(event_type)
  687. payload_len = len(payload)
  688. D = {
  689. 'ver':'3.0',
  690. 'sid':self.config.options.identifier,
  691. 'serial':serial,
  692. 'pool_name':self.config.name,
  693. 'pool_serial':pool_serial,
  694. 'event_name':event_name,
  695. 'len':payload_len,
  696. 'payload':payload,
  697. }
  698. return ('ver:%(ver)s server:%(sid)s serial:%(serial)s '
  699. 'pool:%(pool_name)s poolserial:%(pool_serial)s '
  700. 'eventname:%(event_name)s len:%(len)s\n%(payload)s' % D)
  701. class GlobalSerial:
  702. def __init__(self):
  703. self.serial = -1
  704. GlobalSerial = GlobalSerial() # singleton
  705. def new_serial(inst):
  706. if inst.serial == sys.maxint:
  707. inst.serial = -1
  708. inst.serial += 1
  709. return inst.serial