process.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. import os
  2. import sys
  3. import time
  4. import errno
  5. import shlex
  6. import traceback
  7. import signal
  8. from supervisor.medusa import asyncore_25 as asyncore
  9. from supervisor.states import ProcessStates
  10. from supervisor.states import SupervisorStates
  11. from supervisor.states import getProcessStateDescription
  12. from supervisor.states import STOPPED_STATES
  13. from supervisor.options import decode_wait_status
  14. from supervisor.options import signame
  15. from supervisor.options import ProcessException, BadCommand
  16. from supervisor.dispatchers import EventListenerStates
  17. from supervisor import events
  18. from supervisor.datatypes import RestartUnconditionally
  19. from supervisor.socket_manager import SocketManager
  20. class Subprocess:
  21. """A class to manage a subprocess."""
  22. # Initial state; overridden by instance variables
  23. pid = 0 # Subprocess pid; 0 when not running
  24. config = None # ProcessConfig instance
  25. state = None # process state code
  26. listener_state = None # listener state code (if we're an event listener)
  27. event = None # event currently being processed (if we're an event listener)
  28. laststart = 0 # Last time the subprocess was started; 0 if never
  29. laststop = 0 # Last time the subprocess was stopped; 0 if never
  30. laststopreport = 0 # Last time "waiting for x to stop" logged, to throttle
  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. self.laststopreport = 0
  298. return self.kill(self.config.stopsignal)
  299. def stop_report(self):
  300. """ Log a 'waiting for x to stop' message with throttling. """
  301. if self.state == ProcessStates.STOPPING:
  302. now = time.time()
  303. if now > (self.laststopreport + 2): # every 2 seconds
  304. self.config.options.logger.info(
  305. 'waiting for %s to stop' % self.config.name)
  306. self.laststopreport = now
  307. def give_up(self):
  308. self.delay = 0
  309. self.backoff = 0
  310. self.system_stop = 1
  311. self._assertInState(ProcessStates.BACKOFF)
  312. self.change_state(ProcessStates.FATAL)
  313. def kill(self, sig):
  314. """Send a signal to the subprocess. This may or may not kill it.
  315. Return None if the signal was sent, or an error message string
  316. if an error occurred or if the subprocess is not running.
  317. """
  318. now = time.time()
  319. options = self.config.options
  320. # Properly stop processes in BACKOFF state.
  321. if self.state == ProcessStates.BACKOFF:
  322. msg = ("Attempted to kill %s, which is in BACKOFF state." %
  323. (self.config.name))
  324. options.logger.debug(msg)
  325. self.change_state(ProcessStates.STOPPED)
  326. return None
  327. if not self.pid:
  328. msg = ("attempted to kill %s with sig %s but it wasn't running" %
  329. (self.config.name, signame(sig)))
  330. options.logger.debug(msg)
  331. return msg
  332. #If we're in the stopping state, then we've already sent the stop
  333. #signal and this is the kill signal
  334. if self.state == ProcessStates.STOPPING:
  335. killasgroup = self.config.killasgroup
  336. else:
  337. killasgroup = self.config.stopasgroup
  338. as_group = ""
  339. if killasgroup:
  340. as_group = "process group "
  341. options.logger.debug('killing %s (pid %s) %swith signal %s'
  342. % (self.config.name,
  343. self.pid,
  344. as_group,
  345. signame(sig))
  346. )
  347. # RUNNING/STARTING/STOPPING -> STOPPING
  348. self.killing = 1
  349. self.delay = now + self.config.stopwaitsecs
  350. # we will already be in the STOPPING state if we're doing a
  351. # SIGKILL as a result of overrunning stopwaitsecs
  352. self._assertInState(ProcessStates.RUNNING,ProcessStates.STARTING,
  353. ProcessStates.STOPPING)
  354. self.change_state(ProcessStates.STOPPING)
  355. pid = self.pid
  356. if killasgroup:
  357. # send to the whole process group instead
  358. pid = -self.pid
  359. try:
  360. options.kill(pid, sig)
  361. except:
  362. tb = traceback.format_exc()
  363. msg = 'unknown problem killing %s (%s):%s' % (self.config.name,
  364. self.pid, tb)
  365. options.logger.critical(msg)
  366. self.change_state(ProcessStates.UNKNOWN)
  367. self.pid = 0
  368. self.killing = 0
  369. self.delay = 0
  370. return msg
  371. return None
  372. def signal(self, sig):
  373. """Send a signal to the subprocess, without intending to kill it.
  374. Return None if the signal was sent, or an error message string
  375. if an error occurred or if the subprocess is not running.
  376. """
  377. options = self.config.options
  378. if not self.pid:
  379. msg = ("attempted to send %s sig %s but it wasn't running" %
  380. (self.config.name, signame(sig)))
  381. options.logger.debug(msg)
  382. return msg
  383. options.logger.debug('sending %s (pid %s) sig %s'
  384. % (self.config.name,
  385. self.pid,
  386. signame(sig))
  387. )
  388. self._assertInState(ProcessStates.RUNNING,ProcessStates.STARTING,
  389. ProcessStates.STOPPING)
  390. try:
  391. options.kill(self.pid, sig)
  392. except:
  393. tb = traceback.format_exc()
  394. msg = 'unknown problem sending sig %s (%s):%s' % (
  395. self.config.name, self.pid, tb)
  396. options.logger.critical(msg)
  397. self.change_state(ProcessStates.UNKNOWN)
  398. self.pid = 0
  399. return msg
  400. return None
  401. def finish(self, pid, sts):
  402. """ The process was reaped and we need to report and manage its state
  403. """
  404. self.drain()
  405. es, msg = decode_wait_status(sts)
  406. now = time.time()
  407. self.laststop = now
  408. processname = self.config.name
  409. if now > self.laststart:
  410. too_quickly = now - self.laststart < self.config.startsecs
  411. else:
  412. too_quickly = False
  413. self.config.options.logger.warn(
  414. "process %r (%s) laststart time is in the future, don't "
  415. "know how long process was running so assuming it did "
  416. "not exit too quickly" % (self.config.name, self.pid))
  417. exit_expected = es in self.config.exitcodes
  418. if self.killing:
  419. # likely the result of a stop request
  420. # implies STOPPING -> STOPPED
  421. self.killing = 0
  422. self.delay = 0
  423. self.exitstatus = es
  424. msg = "stopped: %s (%s)" % (processname, msg)
  425. self._assertInState(ProcessStates.STOPPING)
  426. self.change_state(ProcessStates.STOPPED)
  427. elif too_quickly:
  428. # the program did not stay up long enough to make it to RUNNING
  429. # implies STARTING -> BACKOFF
  430. self.exitstatus = None
  431. self.spawnerr = 'Exited too quickly (process log may have details)'
  432. msg = "exited: %s (%s)" % (processname, msg + "; not expected")
  433. self._assertInState(ProcessStates.STARTING)
  434. self.change_state(ProcessStates.BACKOFF)
  435. else:
  436. # this finish was not the result of a stop request, the
  437. # program was in the RUNNING state but exited
  438. # implies RUNNING -> EXITED normally but see next comment
  439. self.delay = 0
  440. self.backoff = 0
  441. self.exitstatus = es
  442. # if the process was STARTING but a system time change causes
  443. # self.laststart to be in the future, the normal STARTING->RUNNING
  444. # transition can be subverted so we perform the transition here.
  445. if self.state == ProcessStates.STARTING:
  446. self.change_state(ProcessStates.RUNNING)
  447. self._assertInState(ProcessStates.RUNNING)
  448. if exit_expected:
  449. # expected exit code
  450. msg = "exited: %s (%s)" % (processname, msg + "; expected")
  451. self.change_state(ProcessStates.EXITED, expected=True)
  452. else:
  453. # unexpected exit code
  454. self.spawnerr = 'Bad exit code %s' % es
  455. msg = "exited: %s (%s)" % (processname, msg + "; not expected")
  456. self.change_state(ProcessStates.EXITED, expected=False)
  457. self.config.options.logger.info(msg)
  458. self.pid = 0
  459. self.config.options.close_parent_pipes(self.pipes)
  460. self.pipes = {}
  461. self.dispatchers = {}
  462. # if we died before we processed the current event (only happens
  463. # if we're an event listener), notify the event system that this
  464. # event was rejected so it can be processed again.
  465. if self.event is not None:
  466. # Note: this should only be true if we were in the BUSY
  467. # state when finish() was called.
  468. events.notify(events.EventRejectedEvent(self, self.event))
  469. self.event = None
  470. def set_uid(self):
  471. if self.config.uid is None:
  472. return
  473. msg = self.config.options.dropPrivileges(self.config.uid)
  474. return msg
  475. def __cmp__(self, other):
  476. # sort by priority
  477. return cmp(self.config.priority, other.config.priority)
  478. def __repr__(self):
  479. return '<Subprocess at %s with name %s in state %s>' % (
  480. id(self),
  481. self.config.name,
  482. getProcessStateDescription(self.get_state()))
  483. def get_state(self):
  484. return self.state
  485. def transition(self):
  486. now = time.time()
  487. state = self.state
  488. logger = self.config.options.logger
  489. if self.config.options.mood > SupervisorStates.RESTARTING:
  490. # dont start any processes if supervisor is shutting down
  491. if state == ProcessStates.EXITED:
  492. if self.config.autorestart:
  493. if self.config.autorestart is RestartUnconditionally:
  494. # EXITED -> STARTING
  495. self.spawn()
  496. else: # autorestart is RestartWhenExitUnexpected
  497. if self.exitstatus not in self.config.exitcodes:
  498. # EXITED -> STARTING
  499. self.spawn()
  500. elif state == ProcessStates.STOPPED and not self.laststart:
  501. if self.config.autostart:
  502. # STOPPED -> STARTING
  503. self.spawn()
  504. elif state == ProcessStates.BACKOFF:
  505. if self.backoff <= self.config.startretries:
  506. if now > self.delay:
  507. # BACKOFF -> STARTING
  508. self.spawn()
  509. if state == ProcessStates.STARTING:
  510. if now - self.laststart > self.config.startsecs:
  511. # STARTING -> RUNNING if the proc has started
  512. # successfully and it has stayed up for at least
  513. # proc.config.startsecs,
  514. self.delay = 0
  515. self.backoff = 0
  516. self._assertInState(ProcessStates.STARTING)
  517. self.change_state(ProcessStates.RUNNING)
  518. msg = (
  519. 'entered RUNNING state, process has stayed up for '
  520. '> than %s seconds (startsecs)' % self.config.startsecs)
  521. logger.info('success: %s %s' % (self.config.name, msg))
  522. if state == ProcessStates.BACKOFF:
  523. if self.backoff > self.config.startretries:
  524. # BACKOFF -> FATAL if the proc has exceeded its number
  525. # of retries
  526. self.give_up()
  527. msg = ('entered FATAL state, too many start retries too '
  528. 'quickly')
  529. logger.info('gave up: %s %s' % (self.config.name, msg))
  530. elif state == ProcessStates.STOPPING:
  531. time_left = self.delay - now
  532. if time_left <= 0:
  533. # kill processes which are taking too long to stop with a final
  534. # sigkill. if this doesn't kill it, the process will be stuck
  535. # in the STOPPING state forever.
  536. self.config.options.logger.warn(
  537. 'killing %r (%s) with SIGKILL' % (self.config.name,
  538. self.pid))
  539. self.kill(signal.SIGKILL)
  540. class FastCGISubprocess(Subprocess):
  541. """Extends Subprocess class to handle FastCGI subprocesses"""
  542. def __init__(self, config):
  543. Subprocess.__init__(self, config)
  544. self.fcgi_sock = None
  545. def before_spawn(self):
  546. """
  547. The FastCGI socket needs to be created by the parent before we fork
  548. """
  549. if self.group is None:
  550. raise NotImplementedError('No group set for FastCGISubprocess')
  551. if not hasattr(self.group, 'socket_manager'):
  552. raise NotImplementedError('No SocketManager set for '
  553. '%s:%s' % (self.group, dir(self.group)))
  554. self.fcgi_sock = self.group.socket_manager.get_socket()
  555. def spawn(self):
  556. """
  557. Overrides Subprocess.spawn() so we can hook in before it happens
  558. """
  559. self.before_spawn()
  560. pid = Subprocess.spawn(self)
  561. if pid is None:
  562. #Remove object reference to decrement the reference count on error
  563. self.fcgi_sock = None
  564. return pid
  565. def after_finish(self):
  566. """
  567. Releases reference to FastCGI socket when process is reaped
  568. """
  569. #Remove object reference to decrement the reference count
  570. self.fcgi_sock = None
  571. def finish(self, pid, sts):
  572. """
  573. Overrides Subprocess.finish() so we can hook in after it happens
  574. """
  575. retval = Subprocess.finish(self, pid, sts)
  576. self.after_finish()
  577. return retval
  578. def _prepare_child_fds(self):
  579. """
  580. Overrides Subprocess._prepare_child_fds()
  581. The FastCGI socket needs to be set to file descriptor 0 in the child
  582. """
  583. sock_fd = self.fcgi_sock.fileno()
  584. options = self.config.options
  585. options.dup2(sock_fd, 0)
  586. options.dup2(self.pipes['child_stdout'], 1)
  587. if self.config.redirect_stderr:
  588. options.dup2(self.pipes['child_stdout'], 2)
  589. else:
  590. options.dup2(self.pipes['child_stderr'], 2)
  591. for i in range(3, options.minfds):
  592. options.close_fd(i)
  593. class ProcessGroupBase:
  594. def __init__(self, config):
  595. self.config = config
  596. self.processes = {}
  597. for pconfig in self.config.process_configs:
  598. self.processes[pconfig.name] = pconfig.make_process(self)
  599. def __cmp__(self, other):
  600. return cmp(self.config.priority, other.config.priority)
  601. def __repr__(self):
  602. return '<%s instance at %s named %s>' % (self.__class__, id(self),
  603. self.config.name)
  604. def removelogs(self):
  605. for process in self.processes.values():
  606. process.removelogs()
  607. def reopenlogs(self):
  608. for process in self.processes.values():
  609. process.reopenlogs()
  610. def stop_all(self):
  611. processes = self.processes.values()
  612. processes.sort()
  613. processes.reverse() # stop in desc priority order
  614. for proc in processes:
  615. state = proc.get_state()
  616. if state == ProcessStates.RUNNING:
  617. # RUNNING -> STOPPING
  618. proc.stop()
  619. elif state == ProcessStates.STARTING:
  620. # STARTING -> STOPPING
  621. proc.stop()
  622. elif state == ProcessStates.BACKOFF:
  623. # BACKOFF -> FATAL
  624. proc.give_up()
  625. def get_unstopped_processes(self):
  626. """ Processes which aren't in a state that is considered 'stopped' """
  627. return [ x for x in self.processes.values() if x.get_state() not in
  628. STOPPED_STATES ]
  629. def get_dispatchers(self):
  630. dispatchers = {}
  631. for process in self.processes.values():
  632. dispatchers.update(process.dispatchers)
  633. return dispatchers
  634. class ProcessGroup(ProcessGroupBase):
  635. def transition(self):
  636. for proc in self.processes.values():
  637. proc.transition()
  638. class FastCGIProcessGroup(ProcessGroup):
  639. def __init__(self, config, **kwargs):
  640. ProcessGroup.__init__(self, config)
  641. sockManagerKlass = kwargs.get('socketManager', SocketManager)
  642. self.socket_manager = sockManagerKlass(config.socket_config,
  643. logger=config.options.logger)
  644. # It's not required to call get_socket() here but we want
  645. # to fail early during start up if there is a config error
  646. try:
  647. self.socket_manager.get_socket()
  648. except Exception, e:
  649. raise ValueError('Could not create FastCGI socket %s: %s' % (self.socket_manager.config(), e))
  650. class EventListenerPool(ProcessGroupBase):
  651. def __init__(self, config):
  652. ProcessGroupBase.__init__(self, config)
  653. self.event_buffer = []
  654. for event_type in self.config.pool_events:
  655. events.subscribe(event_type, self._acceptEvent)
  656. events.subscribe(events.EventRejectedEvent, self.handle_rejected)
  657. self.serial = -1
  658. self.last_dispatch = 0
  659. self.dispatch_throttle = 0 # in seconds: .00195 is an interesting one
  660. def handle_rejected(self, event):
  661. process = event.process
  662. procs = self.processes.values()
  663. if process in procs: # this is one of our processes
  664. # rebuffer the event
  665. self._acceptEvent(event.event, head=True)
  666. def transition(self):
  667. processes = self.processes.values()
  668. dispatch_capable = False
  669. for process in processes:
  670. process.transition()
  671. # this is redundant, we do it in _dispatchEvent too, but we
  672. # want to reduce function call overhead
  673. if process.state == ProcessStates.RUNNING:
  674. if process.listener_state == EventListenerStates.READY:
  675. dispatch_capable = True
  676. if dispatch_capable:
  677. if self.dispatch_throttle:
  678. now = time.time()
  679. if now - self.last_dispatch < self.dispatch_throttle:
  680. return
  681. self.dispatch()
  682. def dispatch(self):
  683. while self.event_buffer:
  684. # dispatch the oldest event
  685. event = self.event_buffer.pop(0)
  686. ok = self._dispatchEvent(event)
  687. if not ok:
  688. # if we can't dispatch an event, rebuffer it and stop trying
  689. # to process any further events in the buffer
  690. self._acceptEvent(event, head=True)
  691. break
  692. self.last_dispatch = time.time()
  693. def _acceptEvent(self, event, head=False):
  694. # events are required to be instances
  695. # this has a side effect to fail with an attribute error on 'old style' classes
  696. if not hasattr(event, 'serial'):
  697. event.serial = new_serial(GlobalSerial)
  698. if not hasattr(event, 'pool_serials'):
  699. event.pool_serials = {}
  700. if not event.pool_serials.has_key(self.config.name):
  701. event.pool_serials[self.config.name] = new_serial(self)
  702. else:
  703. self.config.options.logger.debug(
  704. 'rebuffering event %s for pool %s (bufsize %s)' % (
  705. (event.serial, self.config.name, len(self.event_buffer))))
  706. if len(self.event_buffer) >= self.config.buffer_size:
  707. if self.event_buffer:
  708. # discard the oldest event
  709. discarded_event = self.event_buffer.pop(0)
  710. self.config.options.logger.error(
  711. 'pool %s event buffer overflowed, discarding event %s' % (
  712. (self.config.name, discarded_event.serial)))
  713. if head:
  714. self.event_buffer.insert(0, event)
  715. else:
  716. self.event_buffer.append(event)
  717. def _dispatchEvent(self, event):
  718. pool_serial = event.pool_serials[self.config.name]
  719. for process in self.processes.values():
  720. if process.state != ProcessStates.RUNNING:
  721. continue
  722. if process.listener_state == EventListenerStates.READY:
  723. payload = str(event)
  724. try:
  725. event_type = event.__class__
  726. serial = event.serial
  727. envelope = self._eventEnvelope(event_type, serial,
  728. pool_serial, payload)
  729. process.write(envelope)
  730. except OSError, why:
  731. if why.args[0] != errno.EPIPE:
  732. raise
  733. continue
  734. process.listener_state = EventListenerStates.BUSY
  735. process.event = event
  736. self.config.options.logger.debug(
  737. 'event %s sent to listener %s' % (
  738. event.serial, process.config.name))
  739. return True
  740. return False
  741. def _eventEnvelope(self, event_type, serial, pool_serial, payload):
  742. event_name = events.getEventNameByType(event_type)
  743. payload_len = len(payload)
  744. D = {
  745. 'ver':'3.0',
  746. 'sid':self.config.options.identifier,
  747. 'serial':serial,
  748. 'pool_name':self.config.name,
  749. 'pool_serial':pool_serial,
  750. 'event_name':event_name,
  751. 'len':payload_len,
  752. 'payload':payload,
  753. }
  754. return ('ver:%(ver)s server:%(sid)s serial:%(serial)s '
  755. 'pool:%(pool_name)s poolserial:%(pool_serial)s '
  756. 'eventname:%(event_name)s len:%(len)s\n%(payload)s' % D)
  757. class GlobalSerial:
  758. def __init__(self):
  759. self.serial = -1
  760. GlobalSerial = GlobalSerial() # singleton
  761. def new_serial(inst):
  762. if inst.serial == sys.maxint:
  763. inst.serial = -1
  764. inst.serial += 1
  765. return inst.serial