remove OnoffProcess parameter start_wait
authorHelmut Grohne <helmut@subdivi.de>
Sat, 19 Aug 2017 20:36:37 +0000 (22:36 +0200)
committerHelmut Grohne <helmut@subdivi.de>
Sat, 19 Aug 2017 20:36:37 +0000 (22:36 +0200)
It duplicates the ondelay parameter of ThrottledDevice. Transform

    dev = OnoffProces(commandline, delay)

into

    dev = ThrottledDevice(OnoffProces(commandline), delay, 0)

README.md
dbus_service.py
onoff/process.py

index 30d6d33..6708b7f 100644 (file)
--- a/README.md
+++ b/README.md
@@ -60,9 +60,12 @@ There currently are four implementations.
 
  *  An `OnoffProcess` can be used if the device is considered active as long as
     the process runs. Deactivation is achieved by killing the process. For
-    example the [redshift][] can be started in this way.
+    example [redshift][] can be started in this way.
 
-        dev = onoff.processs.OnoffProcess(["redshift"], 3)
+        dev = onoff.processs.OnoffProcess(["redshift"])
+
+    To account for the time it takes the process to start up, it can be wrapped
+    in a `ThrottledDevice` (see below).
 
  *  An `InvertedDevice` can be used to swap the activation states. It can be
     used to activate a device all the time and inhibit activation whenever the
index 94ff9b5..5755cb8 100755 (executable)
@@ -21,7 +21,8 @@ def main():
     logging.getLogger().setLevel(logging.DEBUG)
     DBusGMainLoop(set_as_default=True)
     bus = dbus.SessionBus()
-    dev = onoff.process.OnoffProcess(["redshift"], 3)
+    dev = onoff.process.OnoffProcess(["redshift"])
+    dev = onoff.common.ThrottledDevice(dev, 3, 0)
     dev = onoff.common.InvertedDevice(dev)
     dev = onoff.common.ThrottledDevice(dev, 1, 5)
     onoff.dbusutils.OnoffControl(bus, "redshift", dev)
index 92757e3..572cbd9 100644 (file)
@@ -3,7 +3,7 @@ import os
 import signal
 
 from .common import ST_ACTIVE, ST_TRANSITION, OnoffDevice
-from .gobject import spawn_child, ScheduledFunction
+from .gobject import spawn_child
 
 logger = logging.getLogger("onoff.process")
 
@@ -20,74 +20,47 @@ class OnoffProcess(OnoffDevice):
     @ivar killed: indicates whether the termination signal has been sent
             to the spawned process.
     """
-    def __init__(self, command, start_wait=0, termsig=signal.SIGTERM):
+    def __init__(self, command, termsig=signal.SIGTERM):
         """
         @type command: [str]
         @param command: an argument vector to be executed. The first element
                 is used as executable and looked up in $PATH.
-        @param start_wait: duration of the transition period from inactive to
-                active in seconds.
         @param termsig: termination signal to be sent to the process to
                 deactivate it. The process must exit in response to this
                 signal.
         """
         OnoffDevice.__init__(self)
         self.command = command
-        self.start_wait = start_wait
         self.termsig = termsig
         self.desired_state = 0 # bit mask of just ST_ACTIVE
         self.pid = None
-        self.starting = None # timeout event during start
         self.killed = False
 
     @property
     def state(self):
-        if self.starting or self.killed:
+        if self.killed:
             return self.desired_state | ST_TRANSITION
         return self.desired_state
 
     def start_process(self):
         assert self.pid is None
-        assert self.starting is None
         logger.info("starting command %s", " ".join(self.command))
         self.pid = spawn_child(self.command, self.process_died)
         logger.debug("started as pid %d", self.pid)
-        self.starting = ScheduledFunction(self.start_wait, self.process_started)
+        assert self.desired_state == ST_ACTIVE
+        logger.debug("process started")
         self.changestate(self.state)
 
-    def cancel_start_wait(self):
-        if self.starting is None:
-            return
-        logger.debug("cancelling start notification")
-        self.starting.cancel()
-        self.starting = None
-
     def process_died(self, pid, condition):
         assert self.pid == pid
         self.pid = None
         self.killed = False
         logger.info("process %d died", pid)
-        self.cancel_start_wait()
         if self.desired_state == ST_ACTIVE:
             self.start_process()
         else:
             self.changestate(self.state)
 
-    def process_started(self):
-        assert self.desired_state == ST_ACTIVE
-        assert self.starting is not None
-        self.starting = None
-        logger.debug("process started")
-        self.changestate(self.state)
-
-    def stop_process(self):
-        assert self.pid is not None
-        self.cancel_start_wait()
-        logger.info("killing process %d", self.pid)
-        os.kill(self.pid, self.termsig)
-        self.killed = True
-        self.changestate(self.state)
-
     def activate(self):
         if self.desired_state != ST_ACTIVE:
             self.desired_state = ST_ACTIVE
@@ -100,7 +73,10 @@ class OnoffProcess(OnoffDevice):
         if self.desired_state != 0:
             self.desired_state = 0
             if self.pid is not None and not self.killed:
-                self.stop_process()
+                logger.info("killing process %d", self.pid)
+                os.kill(self.pid, self.termsig)
+                self.killed = True
+                self.changestate(self.state)
             else:
                 logger.debug("already deactivated. nothing to do.")