procmontap.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- test-case-name: twisted.runner.test.test_procmontap -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Support for creating a service which runs a process monitor.
  6. """
  7. from twisted.python import usage
  8. from twisted.runner.procmon import ProcessMonitor
  9. class Options(usage.Options):
  10. """
  11. Define the options accepted by the I{twistd procmon} plugin.
  12. """
  13. synopsis = "[procmon options] commandline"
  14. optParameters = [["threshold", "t", 1, "How long a process has to live "
  15. "before the death is considered instant, in seconds.",
  16. float],
  17. ["killtime", "k", 5, "How long a process being killed "
  18. "has to get its affairs in order before it gets killed "
  19. "with an unmaskable signal.",
  20. float],
  21. ["minrestartdelay", "m", 1, "The minimum time (in "
  22. "seconds) to wait before attempting to restart a "
  23. "process", float],
  24. ["maxrestartdelay", "M", 3600, "The maximum time (in "
  25. "seconds) to wait before attempting to restart a "
  26. "process", float]]
  27. optFlags = []
  28. longdesc = """\
  29. procmon runs processes, monitors their progress, and restarts them when they
  30. die.
  31. procmon will not attempt to restart a process that appears to die instantly;
  32. with each "instant" death (less than 1 second, by default), it will delay
  33. approximately twice as long before restarting it. A successful run will reset
  34. the counter.
  35. Eg twistd procmon sleep 10"""
  36. def parseArgs(self, *args):
  37. """
  38. Grab the command line that is going to be started and monitored
  39. """
  40. self['args'] = args
  41. def postOptions(self):
  42. """
  43. Check for dependencies.
  44. """
  45. if len(self["args"]) < 1:
  46. raise usage.UsageError("Please specify a process commandline")
  47. def makeService(config):
  48. s = ProcessMonitor()
  49. s.threshold = config["threshold"]
  50. s.killTime = config["killtime"]
  51. s.minRestartDelay = config["minrestartdelay"]
  52. s.maxRestartDelay = config["maxrestartdelay"]
  53. s.addProcess(" ".join(config["args"]), config["args"])
  54. return s