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