diff --git a/IPython/kernel/clusterdir.py b/IPython/kernel/clusterdir.py index 9c54141..d5b0a11 100644 --- a/IPython/kernel/clusterdir.py +++ b/IPython/kernel/clusterdir.py @@ -15,6 +15,8 @@ The IPython cluster directory # Imports #----------------------------------------------------------------------------- +from __future__ import with_statement + import os import shutil import sys @@ -37,6 +39,10 @@ class ClusterDirError(Exception): pass +class PIDFileError(Exception): + pass + + class ClusterDir(Component): """An object to manage the cluster directory and its resources. @@ -50,9 +56,11 @@ class ClusterDir(Component): security_dir_name = Unicode('security') log_dir_name = Unicode('log') - security_dir = Unicode() - log_dir = Unicode('') - location = Unicode('') + pid_dir_name = Unicode('pid') + security_dir = Unicode(u'') + log_dir = Unicode(u'') + pid_dir = Unicode(u'') + location = Unicode(u'') def __init__(self, location): super(ClusterDir, self).__init__(None) @@ -65,6 +73,7 @@ class ClusterDir(Component): os.chmod(new, 0777) self.security_dir = os.path.join(new, self.security_dir_name) self.log_dir = os.path.join(new, self.log_dir_name) + self.pid_dir = os.path.join(new, self.pid_dir_name) self.check_dirs() def _log_dir_changed(self, name, old, new): @@ -85,9 +94,19 @@ class ClusterDir(Component): else: os.chmod(self.security_dir, 0700) + def _pid_dir_changed(self, name, old, new): + self.check_pid_dir() + + def check_pid_dir(self): + if not os.path.isdir(self.pid_dir): + os.mkdir(self.pid_dir, 0700) + else: + os.chmod(self.pid_dir, 0700) + def check_dirs(self): self.check_security_dir() self.check_log_dir() + self.check_pid_dir() def load_config_file(self, filename): """Load a config file from the top level of the cluster dir. @@ -375,6 +394,8 @@ class ApplicationWithClusterDir(Application): self.security_dir = config.Global.security_dir = sdir ldir = self.cluster_dir_obj.log_dir self.log_dir = config.Global.log_dir = ldir + pdir = self.cluster_dir_obj.pid_dir + self.pid_dir = config.Global.pid_dir = pdir self.log.info("Cluster directory set to: %s" % self.cluster_dir) def start_logging(self): @@ -392,3 +413,46 @@ class ApplicationWithClusterDir(Application): else: open_log_file = sys.stdout log.startLogging(open_log_file) + + def write_pid_file(self): + """Create a .pid file in the pid_dir with my pid. + + This must be called after pre_construct, which sets `self.pid_dir`. + This raises :exc:`PIDFileError` if the pid file exists already. + """ + pid_file = os.path.join(self.pid_dir, self.name + '.pid') + if os.path.isfile(pid_file): + pid = self.get_pid_from_file() + raise PIDFileError( + 'The pid file [%s] already exists. \nThis could mean that this ' + 'server is already running with [pid=%s].' % (pid_file, pid)) + with open(pid_file, 'w') as f: + self.log.info("Creating pid file: %s" % pid_file) + f.write(repr(os.getpid())+'\n') + + def remove_pid_file(self): + """Remove the pid file. + + This should be called at shutdown by registering a callback with + :func:`reactor.addSystemEventTrigger`. + """ + pid_file = os.path.join(self.pid_dir, self.name + '.pid') + if os.path.isfile(pid_file): + try: + self.log.info("Removing pid file: %s" % pid_file) + os.remove(pid_file) + except: + pass + + def get_pid_from_file(self): + """Get the pid from the pid file. + + If the pid file doesn't exist a :exc:`PIDFileError` is raised. + """ + pid_file = os.path.join(self.pid_dir, self.name + '.pid') + if os.path.isfile(pid_file): + with open(pid_file, 'r') as f: + pid = int(f.read().strip()) + return pid + else: + raise PIDFileError('pid file not found: %s' % pid_file) \ No newline at end of file diff --git a/IPython/kernel/ipclusterapp.py b/IPython/kernel/ipclusterapp.py index 6c6360e..3cde768 100644 --- a/IPython/kernel/ipclusterapp.py +++ b/IPython/kernel/ipclusterapp.py @@ -20,13 +20,15 @@ import os import signal import sys +from twisted.scripts._twistd_unix import daemonize + from IPython.core import release from IPython.external import argparse from IPython.config.loader import ArgParseConfigLoader, NoConfigDefault from IPython.utils.importstring import import_item from IPython.kernel.clusterdir import ( - ApplicationWithClusterDir, ClusterDirError + ApplicationWithClusterDir, ClusterDirError, PIDFileError ) from twisted.internet import reactor, defer @@ -132,6 +134,27 @@ class IPClusterCLLoader(ArgParseConfigLoader): help="Don't delete old log flies before starting.", default=NoConfigDefault ) + parser_start.add_argument('--daemon', '-daemon', + dest='Global.daemonize', action='store_true', + help='Daemonize the ipcluster program. This implies --log-to-file', + default=NoConfigDefault + ) + parser_start.add_argument('--nodaemon', '-nodaemon', + dest='Global.daemonize', action='store_false', + help="Dont't daemonize the ipcluster program.", + default=NoConfigDefault + ) + + parser_start = subparsers.add_parser( + 'stop', + help='Stop a cluster.', + parents=[parent_parser1, parent_parser2] + ) + parser_start.add_argument('-sig', '--sig', + dest='Global.stop_signal', type=int, + help="The signal number to use in stopping the cluster (default=2).", + default=NoConfigDefault + ) default_config_file_name = 'ipcluster_config.py' @@ -153,6 +176,8 @@ class IPClusterApp(ApplicationWithClusterDir): self.default_config.Global.n = 2 self.default_config.Global.reset_config = False self.default_config.Global.clean_logs = True + self.default_config.Global.stop_signal = 2 + self.default_config.Global.daemonize = False def create_command_line_config(self): """Create and return a command line config loader.""" @@ -170,7 +195,7 @@ class IPClusterApp(ApplicationWithClusterDir): elif subcommand=='create': self.auto_create_cluster_dir = True super(IPClusterApp, self).find_resources() - elif subcommand=='start': + elif subcommand=='start' or subcommand=='stop': self.auto_create_cluster_dir = False try: super(IPClusterApp, self).find_resources() @@ -182,6 +207,16 @@ class IPClusterApp(ApplicationWithClusterDir): "information about creating and listing cluster dirs." ) + def pre_construct(self): + super(IPClusterApp, self).pre_construct() + config = self.master_config + try: + daemon = config.Global.daemonize + if daemon: + config.Global.log_to_file = True + except AttributeError: + pass + def construct(self): config = self.master_config if config.Global.subcommand=='list': @@ -288,11 +323,48 @@ class IPClusterApp(ApplicationWithClusterDir): super(IPClusterApp, self).start_logging() def start_app(self): + """Start the application, depending on what subcommand is used.""" config = self.master_config - if config.Global.subcommand=='create' or config.Global.subcommand=='list': + subcmd = config.Global.subcommand + if subcmd=='create' or subcmd=='list': return - elif config.Global.subcommand=='start': + elif subcmd=='start': + # First see if the cluster is already running + try: + pid = self.get_pid_from_file() + except: + pass + else: + self.log.critical( + 'Cluster is already running with [pid=%s]. ' + 'use "ipcluster stop" to stop the cluster.' % pid + ) + sys.exit(9) + # Now log and daemonize + self.log.info('Starting ipcluster with [daemon=%r]' % config.Global.daemonize) + if config.Global.daemonize: + if os.name=='posix': + os.chdir(config.Global.cluster_dir) + self.log_level = 40 + daemonize() + + # Now write the new pid file after our new forked pid is active. + self.write_pid_file() + reactor.addSystemEventTrigger('during','shutdown', self.remove_pid_file) reactor.run() + elif subcmd=='stop': + try: + pid = self.get_pid_from_file() + except PIDFileError: + self.log.critical( + 'Problem reading pid file, cluster is probably not running.' + ) + sys.exit(9) + sig = config.Global.stop_signal + self.log.info( + "Stopping cluster [pid=%r] with [signal=%r]" % (pid, sig) + ) + os.kill(pid, sig) def launch_new_instance(): diff --git a/IPython/kernel/ipcontrollerapp.py b/IPython/kernel/ipcontrollerapp.py index 4ac0954..0a27178 100644 --- a/IPython/kernel/ipcontrollerapp.py +++ b/IPython/kernel/ipcontrollerapp.py @@ -15,6 +15,8 @@ The IPython controller application. # Imports #----------------------------------------------------------------------------- +from __future__ import with_statement + import copy import os import sys @@ -213,7 +215,7 @@ class IPControllerApp(ApplicationWithClusterDir): self.start_logging() self.import_statements() - + # Create the service hierarchy self.main_service = service.MultiService() # The controller service @@ -240,6 +242,8 @@ class IPControllerApp(ApplicationWithClusterDir): def start_app(self): # Start the controller service and set things running self.main_service.startService() + self.write_pid_file() + reactor.addSystemEventTrigger('during','shutdown', self.remove_pid_file) reactor.run()