本文整理汇总了Python中daemon.DaemonContext类的典型用法代码示例。如果您正苦于以下问题:Python DaemonContext类的具体用法?Python DaemonContext怎么用?Python DaemonContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DaemonContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: open
def open(self):
# Call super
DaemonContext.open(self)
# Needfuls.doit()
self.logger.info('Initializing...')
# RPC connection
self.connection = Connection(self.config['rpchost'])
self.logger.info('Loading plugins...')
# Import and create plugin objects
self.plugins = [
plugin(self.connection, self.config, self.logger.handler)
for plugin in [
getattr(
__import__(
'rpcdaemon.plugins.' + module.lower(),
fromlist=[module]
),
module)
for module in self.config['plugins'].split()
]
]
# Setup worker with plugins and crank it up
self.logger.info('Starting worker...')
self.worker = Worker(self.connection, self.plugins,
handler=self.logger.handler)
self.worker.daemon = True # Daemon thread
self.worker.start()
self.logger.info('Started.')
示例2: daemonise
def daemonise():
from daemon import DaemonContext
from pidfile import PidFile
daemon = DaemonContext(pidfile=PidFile("/var/run/doorbot.pid"))
daemon.open()
logging.info('Daemonised doorbot')
示例3: transfer
def transfer(app, transfer_job_id):
transfer_job = app.get_transfer_job(transfer_job_id)
if transfer_job is None:
log.error('Invalid transfer job ID: %s' % transfer_job_id)
return False
port_range = app.config.get('app:main', 'transfer_worker_port_range')
try:
port_range = [int(p) for p in port_range.split('-')]
except Exception as e:
log.error('Invalid port range set in transfer_worker_port_range: %s: %s' % (port_range, str(e)))
return False
protocol = transfer_job.params['protocol']
if protocol not in ('http', 'https', 'scp'):
log.error('Unsupported protocol: %s' % protocol)
return False
state_result = StateResult(result=dict(state=transfer_job.states.RUNNING, info='Transfer process starting up.'))
listener_server = ListenerServer(range(port_range[0], port_range[1] + 1), ListenerRequestHandler, app, transfer_job, state_result)
# daemonize here (if desired)
if not debug:
daemon_context = DaemonContext(files_preserve=[listener_server.fileno()], working_directory=os.getcwd())
daemon_context.open()
# If this fails, it'll never be detected. Hopefully it won't fail since it succeeded once.
app.connect_database() # daemon closed the database fd
transfer_job = app.get_transfer_job(transfer_job_id)
listener_thread = threading.Thread(target=listener_server.serve_forever)
listener_thread.setDaemon(True)
listener_thread.start()
# Store this process' pid so unhandled deaths can be handled by the restarter
transfer_job.pid = os.getpid()
app.sa_session.add(transfer_job)
app.sa_session.flush()
terminal_state = None
if protocol in ['http', 'https']:
for transfer_result_dict in http_transfer(transfer_job):
state_result.result = transfer_result_dict
if transfer_result_dict['state'] in transfer_job.terminal_states:
terminal_state = transfer_result_dict
elif protocol in ['scp']:
# Transfer the file using scp
transfer_result_dict = scp_transfer(transfer_job)
# Handle the state of the transfer
state = transfer_result_dict['state']
state_result.result = transfer_result_dict
if state in transfer_job.terminal_states:
terminal_state = transfer_result_dict
if terminal_state is not None:
transfer_job.state = terminal_state['state']
for name in ['info', 'path']:
if name in terminal_state:
transfer_job.__setattr__(name, terminal_state[name])
else:
transfer_job.state = transfer_job.states.ERROR
transfer_job.info = 'Unknown error encountered by transfer worker.'
app.sa_session.add(transfer_job)
app.sa_session.flush()
return True
示例4: actionStartFCGI
def actionStartFCGI(self):
from flup.server.fcgi import WSGIServer
from daemon import DaemonContext
from lockfile import FileLock
from os import getpid
ctx = DaemonContext(working_directory=".", pidfile=FileLock("/tmp/wl-fcgi"))
ctx.stderr = open("error.log", "w+")
with ctx:
open("wl-fcgi.pid", "w").write(str(getpid()))
WSGIServer(app, bindAddress="fcgi.sock", umask=0000).run()
示例5: close
def close(self):
# We might get called more than once, or before worker exists
if self.is_open and self.worker and self.worker.is_alive():
self.logger.info('Stopping worker...')
self.worker.should_stop = True
self.worker.join(5) # Wait up to 5 seconds
if self.worker.is_alive():
self.logger.warn(
'Error stopping worker. Shutting down uncleanly.'
)
self.logger.info('Stopped.')
DaemonContext.close(self)
示例6: open
def open( self ):
self.files_preserve =\
list( tuple(self.files_preserve) + tuple( logger.handler.stream for logger in self.loggers ) )
_DaemonContext.open( self )
gevent.reinit()
## not reliable w/ gevent, unfortunateley .. use stderr redirect instead
#log = logging.getLogger('UNHANDLED')
#sys.excepthook = lambda tp, value, tb:\
# log.error( ''.join( traceback.format_exception( tp, value, tb ) ) )
gevent.signal(signal.SIGTERM, self.run_exit_hooks, signal.SIGTERM, None )
示例7: _daemonize
def _daemonize(self):
from daemon import DaemonContext
try:
from daemon.pidfile import TimeoutPIDLockFile as PIDLockFile
except:
from daemon.pidlockfile import PIDLockFile
pidlock = PIDLockFile('/var/run/fedmsg/%s.pid' % self.name)
output = file('/var/log/fedmsg/%s.log' % self.name, 'a')
daemon = DaemonContext(pidfile=pidlock, stdout=output, stderr=output)
daemon.terminate = self._handle_signal
with daemon:
return self.run()
示例8: _daemonize
def _daemonize(self, func, config):
from daemon import DaemonContext
try:
from daemon.pidfile import TimeoutPIDLockFile as PIDLockFile
except:
from daemon.pidlockfile import PIDLockFile
pidlock = PIDLockFile("/var/run/fedmsg/%s.pid" % self.name)
output = file("/var/log/fedmsg/%s.log" % self.name, "a")
daemon = DaemonContext(pidfile=pidlock, stdout=output, stderr=output)
daemon.terminate = self._handle_signal
with daemon:
return func(**config)
示例9: __init__
def __init__(self, app):
""" Set up the parameters of a new runner.
The `app` argument must have the following attributes:
* `stdin_path`, `stdout_path`, `stderr_path`: Filesystem
paths to open and replace the existing `sys.stdin`,
`sys.stdout`, `sys.stderr`.
* `pidfile_path`: Absolute filesystem path to a file that
will be used as the PID file for the daemon. If
``None``, no PID file will be used.
* `pidfile_timeout`: Used as the default acquisition
timeout value supplied to the runner's PID lock file.
* `run`: Callable that will be invoked when the daemon is
started.
"""
self.parse_args()
self.app = app
self.daemon_context = DaemonContext()
self.daemon_context.stdin = open(app.stdin_path, 'r')
self.daemon_context.stdout = open(app.stdout_path, 'w+')
self.daemon_context.stderr = open(
app.stderr_path, 'w+', buffering=0)
self.pidfile = None
if app.pidfile_path is not None:
self.pidfile = make_pidlockfile(
app.pidfile_path, app.pidfile_timeout)
self.daemon_context.pidfile = self.pidfile
示例10: __init__
def __init__(self, app):
""" Set up the parameters of a new runner.
The `app` argument must have the following attributes:
* `stdin_path`, `stdout_path`, `stderr_path`: Filesystem
paths to open and replace the existing `sys.stdin`,
`sys.stdout`, `sys.stderr`.
* `pidfile_path`: Filesystem path to a file that will be
used as the PID file for the daemon.
* `run`: Callable that will be invoked when the daemon is
started.
"""
self.parse_args()
self.app = app
self.daemon_context = DaemonContext()
self.daemon_context.stdin = open(app.stdin_path, 'r')
self.daemon_context.stdout = open(app.stdout_path, 'w+')
self.daemon_context.stderr = open(
app.stderr_path, 'w+', buffering=0)
self.pidfile = make_pidlockfile(app.pidfile_path)
self.daemon_context.pidfile = self.pidfile
示例11: start_server
def start_server(config):
weblogger = logging.getLogger('plight_httpd')
weblogger.setLevel(config['web_log_level'])
if weblogger.handlers == []:
weblogging_handler = RotatingFileHandler(config['web_log_file'],
mode='a',
maxBytes=config['web_log_filesize'],
backupCount=config['web_log_rotation_count'])
weblogger.addHandler(weblogging_handler)
applogger = logging.getLogger('plight')
applogger.setLevel(config['log_level'])
if applogger.handlers == []:
applogging_handler = RotatingFileHandler(config['log_file'],
mode='a',
maxBytes=config['log_filesize'],
backupCount=config['log_rotation_count'])
applogger.addHandler(applogging_handler)
pidfile = PIDLockFile(PID_FILE)
context = DaemonContext(pidfile=pidfile,
uid=pwd.getpwnam(config['user']).pw_uid,
gid=grp.getgrnam(config['group']).gr_gid,
files_preserve = [
weblogging_handler.stream,
applogging_handler.stream,
],)
context.stdout = applogging_handler.stream
context.stderr = applogging_handler.stream
context.open()
os.umask(0022)
try:
try:
log_message('Plight is starting...')
node_status = plight.NodeStatus(config['state_file'])
server_class = BaseHTTPServer.HTTPServer
http = server_class((config['host'],
config['port']),
plight.StatusHTTPRequestHandler)
http.serve_forever()
except SystemExit, sysexit:
log_message("Stopping... " + str(sysexit))
except Exception, ex:
log_message("ERROR: " + str(ex))
示例12: run_as_daemon
def run_as_daemon(ssm_inst):
"""
Given an SSM object, start it as a daemon process.
"""
log.info("The SSM will run as a daemon.")
# We need to preserve the file descriptor for any log files.
log_files = [x.stream for x in log.handlers]
dc = DaemonContext(files_preserve=log_files)
try:
# Note: because we need to be compatible with python 2.4, we can't use
# with dc:
# here - we need to call the open() and close() methods
# manually.
dc.open()
try:
ssm_inst.startup()
except Exception, err:
print err
print type(err)
print dir(err)
log.info("SSM failed to start: " + str(err))
raise
# Only an exception will break this loop.
# A SystemExit exception will be raised if the process is killed.
while True:
if ssm_inst.is_dead():
raise ssm_inst.get_death_exception()
# Process all the messages one at a time before continuing
try:
while ssm_inst.process_outgoing():
pass
except ssm.SsmException, err:
# SsmException if the message is rejected by the consumer.
# We can wait and try again.
log.error('Error in message processing: '+str(err))
except EncryptException, err:
# EncryptException if something went wrong trying to encrypt
# or sign. Give up.
log.error("Failed to encrypt or sign:" + str(err))
raise
示例13: __init__
def __init__(self, redirect_output = '/tmp/ocsp_responder.log',
chroot = None,
detach = True,
pidfile = '/tmp/ocsp_responder.pid'):
self.redirect_output = file(redirect_output, 'a')
self.chroot = chroot
self.detach = detach
self.pidfile = pidfile
if self.pidfile:
self.pidfile = LockFile(pidfile)
if not self.detach:
self.redirect_output = sys.stdout
DaemonContext.__init__(self,
stdout = self.redirect_output,
stderr = self.redirect_output,
detach_process = self.detach,
pidfile = self.pidfile)
示例14: _daemonize
def _daemonize(self):
import psutil
from daemon import DaemonContext
try:
from daemon.pidfile import TimeoutPIDLockFile as PIDLockFile
except:
from daemon.pidlockfile import PIDLockFile
pidlock = PIDLockFile('/var/run/fedmsg/%s.pid' % self.name)
pid = pidlock.read_pid()
if pid and not psutil.pid_exists(pid):
self.log.warn("PID file exists but with no proc: coup d'etat!")
pidlock.break_lock()
output = file('/var/log/fedmsg/%s.log' % self.name, 'a')
daemon = DaemonContext(pidfile=pidlock, stdout=output, stderr=output)
daemon.terminate = self._handle_signal
with daemon:
return self.run()
示例15: __init__
def __init__(self, args=None):
# Parse args
if args is None:
args = {}
options, _ = getopt.getopt(sys.argv[1:], 'c:d')
options = dict(options)
config_file = options.get('-c', '/usr/local/etc/rpcdaemon.conf')
daemonize = '-d' not in options
# Parse config
self.config = Config(config_file, 'Daemon')
# Initialize logger
self.logger = Logger(
name='rpcdaemon',
level = self.config['loglevel'],
path = self.config['logfile'] if daemonize else None,
handler = None if daemonize else logging.StreamHandler()
)
self.pidfile = PIDFile(self.config['pidfile']) if daemonize else None;
# TOOD: plugin.check thread pool?
self.timeout = int(self.config.get('check_interval', 1))
# Clamp in case we exit before worker exists
self.worker = None
# Initialize daemon
DaemonContext.__init__(
self,
detach_process=daemonize,
files_preserve=[self.logger.handler.stream.fileno()],
pidfile=self.pidfile,
stdout=self.logger.handler.stream,
stderr=self.logger.handler.stream
)