Changeset 488a04c in trunk for src/allmydata/scripts
- Timestamp:
- 2022-09-01T23:42:06Z (3 years ago)
- Branches:
- master
- Children:
- 1e6381c
- Parents:
- 7c25e15
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/allmydata/scripts/tahoe_run.py ¶
r7c25e15 r488a04c 21 21 from twisted.python import usage 22 22 from twisted.python.reflect import namedAny 23 from twisted.internet.defer import maybeDeferred 23 from twisted.internet.defer import maybeDeferred, Deferred 24 from twisted.internet.protocol import Protocol 25 from twisted.internet.stdio import StandardIO 24 26 from twisted.application.service import Service 25 27 … … 148 150 149 151 def startService(self): 152 153 from twisted.internet import reactor 150 154 151 155 def start(): … … 188 192 def created(srv): 189 193 srv.setServiceParent(self.parent) 194 # exiting on stdin-closed facilitates cleanup when run 195 # as a subprocess 196 on_stdin_close(reactor, reactor.stop) 190 197 d.addCallback(created) 191 198 d.addErrback(handle_config_error) … … 193 200 return d 194 201 195 from twisted.internet import reactor196 202 reactor.callWhenRunning(start) 197 203 … … 205 211 def makeService(self, so): 206 212 return DaemonizeTheRealService(self.nodetype, self.basedir, so) 213 214 215 def 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 207 250 208 251
Note: See TracChangeset
for help on using the changeset viewer.