Changeset 488a04c in trunk for src/allmydata/scripts


Ignore:
Timestamp:
2022-09-01T23:42:06Z (3 years ago)
Author:
meejah <meejah@…>
Branches:
master
Children:
1e6381c
Parents:
7c25e15
Message:

exit when stdin closes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/allmydata/scripts/tahoe_run.py

    r7c25e15 r488a04c  
    2121from twisted.python import usage
    2222from twisted.python.reflect import namedAny
    23 from twisted.internet.defer import maybeDeferred
     23from twisted.internet.defer import maybeDeferred, Deferred
     24from twisted.internet.protocol import Protocol
     25from twisted.internet.stdio import StandardIO
    2426from twisted.application.service import Service
    2527
     
    148150
    149151    def startService(self):
     152
     153        from twisted.internet import reactor
    150154
    151155        def start():
     
    188192            def created(srv):
    189193                srv.setServiceParent(self.parent)
     194                # exiting on stdin-closed facilitates cleanup when run
     195                # as a subprocess
     196                on_stdin_close(reactor, reactor.stop)
    190197            d.addCallback(created)
    191198            d.addErrback(handle_config_error)
     
    193200            return d
    194201
    195         from twisted.internet import reactor
    196202        reactor.callWhenRunning(start)
    197203
     
    205211    def makeService(self, so):
    206212        return DaemonizeTheRealService(self.nodetype, self.basedir, so)
     213
     214
     215def on_stdin_close(reactor, fn):
     216    """
     217    Arrange for the function `fn` to run when our stdin closes
     218    """
     219    when_closed_d = Deferred()
     220
     221    class WhenClosed(Protocol):
     222        """
     223        Notify a Deferred when our connection is lost .. as this is passed
     224        to twisted's StandardIO class, it is used to detect our parent
     225        going away.
     226        """
     227
     228        def connectionLost(self, reason):
     229            when_closed_d.callback(None)
     230
     231    def on_close(arg):
     232        try:
     233            fn()
     234        except Exception:
     235            # for our "exit" use-case, this will _mostly_ just be
     236            # ReactorNotRunning (because we're already shutting down
     237            # when our stdin closes) but no matter what "bad thing"
     238            # happens we just want to ignore it.
     239            pass
     240        return arg
     241
     242    when_closed_d.addBoth(on_close)
     243    # we don't need to do anything with this instance because it gets
     244    # hooked into the reactor and thus remembered
     245    StandardIO(
     246        proto=WhenClosed(),
     247        reactor=reactor,
     248    )
     249    return None
    207250
    208251
Note: See TracChangeset for help on using the changeset viewer.