def startTwisted(application, startDir='./', nodaemon=0, logfile=None, rundir='.', appname='tums', pidfile='/var/run/tums.pid'): """ A freezable twistd bootstrap layer """ from twisted.application import service, internet, strports, app from twisted.python import log, syslog try: from twisted.scripts import _twistd_unix as twistd except: from twisted.scripts import twistd config = { 'profile': None, 'reactor': None, 'encrypted': 0, 'syslog': 0, 'prefix': appname, 'report-profile': None, 'euid': 0, 'file': 'twistd.tap', 'originalname': appname, 'rundir': rundir, 'logfile': logfile, 'nodaemon': nodaemon, 'uid': None, 'xml': None, 'chroot': None, 'no_save': True, 'quiet': 0, 'source': None, 'nothotshot': 0, 'gid': None, 'savestats': 0, 'debug': False, 'pidfile': pidfile } twistd.checkPID(config['pidfile']) #app.installReactor(config['reactor']) config['nodaemon'] = config['nodaemon'] or config['debug'] oldstdout = sys.stdout oldstderr = sys.stderr twistd.startLogging(config['logfile'], config['syslog'], config['prefix'], config['nodaemon']) app.initialLog() twistd.startApplication(config, application) app.runReactorWithLogging(config, oldstdout, oldstderr) twistd.removePID(config['pidfile']) app.reportProfile(config['report-profile'], service.IProcess(application).processName) log.msg("Server Shut Down.")
def postOptions(self): # This does not invoke the super implementation. At the time this # method was implemented, all the super method did was *conditionally* # set self['no_save'] to True and take the abspath of self['pidfile']. # See below for the irrelevance of those operations. self._fixConfig() self._checkPID() S = self.parent.getStore() # make sure we open it here oldstdout = sys.stdout oldstderr = sys.stderr self._startLogging() app.initialLog() self.application = application = self._constructApplication() self._startApplication() app.runReactorWithLogging(self, oldstdout, oldstderr) self._removePID() app.reportProfile(self['report-profile'], service.IProcess(application).processName) log.msg("Server Shut Down.")
def postOptions(self): # This does not invoke the super implementation. At the time this # method was implemented, all the super method did was *conditionally* # set self['no_save'] to True and take the abspath of self['pidfile']. # See below for the irrelevance of those operations. self._fixConfig() self._checkPID() S = self.parent.getStore() # make sure we open it here oldstdout = sys.stdout oldstderr = sys.stderr self._startLogging() app.initialLog() self.application = application = self._constructApplication() self._startApplication() app.runReactorWithLogging(self, oldstdout, oldstderr) self._removePID() app.reportProfile( self['report-profile'], service.IProcess(application).processName) log.msg("Server Shut Down.")
def postApplication(self): """ Start the application and run the reactor. """ service.IService(self.application).privilegedStartService() app.startApplication(self.application, not self.config['no_save']) app.startApplication(internet.TimerService(0.1, lambda:None), 0) app.runReactorWithLogging(self.config, self.oldstdout, self.oldstderr) app.reportProfile(self.config['report-profile'], service.IProcess(self.application).processName) log.msg("Server Shut Down.")
def postApplication(self): """ Start the application and run the reactor. """ service.IService(self.application).privilegedStartService() app.startApplication(self.application, not self.config['no_save']) app.startApplication(internet.TimerService(0.1, lambda: None), 0) app.runReactorWithLogging(self.config, self.oldstdout, self.oldstderr) app.reportProfile(self.config['report-profile'], service.IProcess(self.application).processName) log.msg("Server Shut Down.")
def postApplication(self): """ To be called after the application is created: start the application and run the reactor. After the reactor stops, clean up PID files and such. """ startApplication(self.config, self.application) app.runReactorWithLogging(self.config, self.oldstdout, self.oldstderr) removePID(self.config['pidfile']) app.reportProfile(self.config['report-profile'], service.IProcess(self.application).processName) log.msg("Server Shut Down.")
def runApp(config): checkPID(config["pidfile"]) passphrase = app.getPassphrase(config["encrypted"]) app.installReactor(config["reactor"]) config["nodaemon"] = config["nodaemon"] or config["debug"] oldstdout = sys.stdout oldstderr = sys.stderr startLogging(config["logfile"], config["syslog"], config["prefix"], config["nodaemon"]) app.initialLog() application = app.getApplication(config, passphrase) startApplication(config, application) app.runReactorWithLogging(config, oldstdout, oldstderr) removePID(config["pidfile"]) app.reportProfile(config["report-profile"], service.IProcess(application).processName) log.msg("Server Shut Down.")
def runApp(config): passphrase = app.getPassphrase(config['encrypted']) app.installReactor(config['reactor']) application = app.getApplication(config, passphrase) oldstdout = sys.stdout oldstderr = sys.stderr startLogging(config['logfile']) app.initialLog() os.chdir(config['rundir']) service.IService(application).privilegedStartService() app.startApplication(application, not config['no_save']) app.startApplication(internet.TimerService(0.1, lambda:None), 0) app.runReactorWithLogging(config, oldstdout, oldstderr) app.reportProfile(config['report-profile'], service.IProcess(application).processName) log.msg("Server Shut Down.")
def runApp(config): passphrase = app.getPassphrase(config['encrypted']) app.installReactor(config['reactor']) application = app.getApplication(config, passphrase) oldstdout = sys.stdout oldstderr = sys.stderr startLogging(config['logfile']) app.initialLog() os.chdir(config['rundir']) service.IService(application).privilegedStartService() app.startApplication(application, not config['no_save']) app.startApplication(internet.TimerService(0.1, lambda: None), 0) app.runReactorWithLogging(config, oldstdout, oldstderr) app.reportProfile(config['report-profile'], service.IProcess(application).processName) log.msg("Server Shut Down.")
def runApp(config): checkPID(config['pidfile']) passphrase = app.getPassphrase(config['encrypted']) app.installReactor(config['reactor']) config['nodaemon'] = config['nodaemon'] or config['debug'] oldstdout = sys.stdout oldstderr = sys.stderr startLogging(config['logfile'], config['syslog'], config['prefix'], config['nodaemon']) app.initialLog() application = app.getApplication(config, passphrase) startApplication(config, application) app.runReactorWithLogging(config, oldstdout, oldstderr) removePID(config['pidfile']) app.reportProfile(config['report-profile'], service.IProcess(application).processName) log.msg("Server Shut Down.")
def startTwisted(application, startDir='./', nodaemon=0, logfile=None, rundir='.', appname='enamel', pidfile='/var/run/enamel.pid'): """ A freezable twistd bootstrap layer """ config = { 'profile': None, 'reactor': None, 'encrypted': 0, 'syslog': None, 'prefix': appname, 'report-profile': None, 'euid': 0, 'file': 'twistd.tap', 'originalname': appname, 'rundir': rundir, 'logfile': logfile, 'nodaemon': nodaemon, 'uid': None, 'xml': None, 'chroot': None, 'no_save': True, 'quiet': 0, 'source': None, 'nothotshot': 0, 'gid': None, 'savestats': 0, 'debug': False, 'pidfile': pidfile, 'umask': None, } #application = compat.convert(application) twistd.checkPID(config['pidfile']) #app.installReactor(config['reactor']) config['nodaemon'] = config['nodaemon'] or config['debug'] oldstdout = sys.stdout oldstderr = sys.stderr try: twistd.startLogging(config['logfile'], config['syslog'], config['prefix'], config['nodaemon']) passDeprecate = False except AttributeError: passDeprecate = True if config['nodaemon']: config['logfile'] = "-" #if not passDeprecate: #app.initialLog() try: twistd.startApplication(config, application) print "twisted 2.5" except AttributeError: print "wt?" # Use hotshot in Twisted 8.x config['profiler'] = 'hotshot' AR = twistd.UnixApplicationRunner(config) AR.startApplication(application) AR.logger.start(application) app.runReactorWithLogging(config, oldstdout, oldstderr) try: twistd.removePID(config['pidfile']) except AttributeError: AR.removePID(config['pidfile']) if not passDeprecate: app.reportProfile(config['report-profile'], service.IProcess(application).processName) log.msg("Server Shut Down.")