def daemon_status(self, *args): """ Print the status of the daemon """ if not is_dbenv_loaded(): from aiida.backends.utils import load_dbenv load_dbenv(process='daemon') if args: print >> sys.stderr, ( "No arguments allowed for the '{}' command.".format( self.get_full_command_name())) sys.exit(1) from aiida.utils import timezone from aiida.daemon.timestamps import get_most_recent_daemon_timestamp from aiida.common.utils import str_timedelta from pytz import UTC most_recent_timestamp = get_most_recent_daemon_timestamp() if most_recent_timestamp is not None: timestamp_delta = (timezone.datetime.now(tz=UTC) - most_recent_timestamp) print("# Most recent daemon timestamp:{}".format( str_timedelta(timestamp_delta))) else: print("# Most recent daemon timestamp: [Never]") pid = self.get_daemon_pid() if pid is None: print "Daemon not running (cannot find the PID for it)" return import psutil def create_time(p): return datetime.fromtimestamp(p.create_time()) try: daemon_process = psutil.Process(self.get_daemon_pid()) except psutil.NoSuchProcess: print "Daemon process can not be found" return print "Daemon is running as pid {pid} since {time}, child processes:".format( pid=daemon_process.pid, time=create_time(daemon_process)) workers = daemon_process.children(recursive=True) if workers: for worker in workers: print " * {name}[{pid}] {status:>10}, started at {time:%Y-%m-%d %H:%M:%S}".format( name=worker.name(), pid=worker.pid, status=worker.status(), time=create_time(worker)) else: print "... but it does not have any child processes, which is wrong"
def configure_user(self, *args): """ Configure the user that can run the daemon. """ if not is_dbenv_loaded(): from aiida.backends.utils import load_dbenv load_dbenv(process='daemon') if args: print >> sys.stderr, ( "No arguments allowed for the '{}' command.".format( self.get_full_command_name())) sys.exit(1) from aiida.utils import timezone from aiida.backends.utils import get_daemon_user, set_daemon_user from aiida.common.utils import (get_configured_user_email, query_yes_no, query_string) from aiida.daemon.timestamps import get_most_recent_daemon_timestamp from aiida.common.utils import str_timedelta from aiida.orm.user import User old_daemon_user = get_daemon_user() this_user = get_configured_user_email() print("> Current default user: {}".format(this_user)) print("> Currently configured user who can run the daemon: {}".format( old_daemon_user)) if old_daemon_user == this_user: print( " (therefore, at the moment you are the user who can run " "the daemon)") pid = self.get_daemon_pid() if pid is not None: print("The daemon is running! I will not proceed.") sys.exit(1) else: print(" (therefore, you cannot run the daemon, at the moment)") most_recent_timestamp = get_most_recent_daemon_timestamp() print "*" * 76 print "* {:72s} *".format("WARNING! Change this setting only if you " "are sure of what you are doing.") print "* {:72s} *".format("Moreover, make sure that the " "daemon is stopped.") if most_recent_timestamp is not None: timestamp_delta = timezone.now() - most_recent_timestamp last_check_string = ( "[The most recent timestamp from the daemon was {}]".format( str_timedelta(timestamp_delta))) print "* {:72s} *".format(last_check_string) print "*" * 76 answer = query_yes_no( "Are you really sure that you want to change " "the daemon user?", default="no") if not answer: sys.exit(0) print "" print "Enter below the email of the new user who can run the daemon." new_daemon_user_email = query_string("New daemon user: "******"ERROR! The user you specified ({}) does " "not exist in the database!!".format(new_daemon_user_email)) print("The available users are {}".format( [_.email for _ in User.search_for_users()])) sys.exit(1) set_daemon_user(new_daemon_user_email) print "The new user that can run the daemon is now {} {}.".format( found_users[0].first_name, found_users[0].last_name)
def daemon_status(self, *args): """ Print the status of the daemon """ if not is_dbenv_loaded(): from aiida.backends.utils import load_dbenv load_dbenv(process='daemon') if args: print >> sys.stderr, ( "No arguments allowed for the '{}' command.".format( self.get_full_command_name())) sys.exit(1) import supervisor import supervisor.supervisorctl import xmlrpclib from aiida.utils import timezone from aiida.daemon.timestamps import get_most_recent_daemon_timestamp from aiida.common.utils import str_timedelta from pytz import UTC most_recent_timestamp = get_most_recent_daemon_timestamp() if most_recent_timestamp is not None: timestamp_delta = (timezone.datetime.now(tz=UTC) - most_recent_timestamp) print("# Most recent daemon timestamp:{}".format( str_timedelta(timestamp_delta))) else: print("# Most recent daemon timestamp: [Never]") pid = self.get_daemon_pid() if pid is None: print "Daemon not running (cannot find the PID for it)" return c = supervisor.supervisorctl.ClientOptions() s = c.read_config(self.conffile_full_path) proxy = xmlrpclib.ServerProxy( 'http://127.0.0.1', transport=supervisor.xmlrpc.SupervisorTransport( s.username, s.password, s.serverurl)) try: running_processes = proxy.supervisor.getAllProcessInfo() except xmlrpclib.Fault as e: if e.faultString == "SHUTDOWN_STATE": print "The daemon is shutting down..." return else: raise except Exception as e: import socket if isinstance(e, socket.error): print "Could not reach the daemon, I got a socket.error: " print " -> [Errno {}] {}".format(e.errno, e.strerror) else: print "Could not reach the daemon, I got a {}: {}".format( e.__class__.__name__, e.message) print "You can try to stop the daemon and start it again." return if running_processes: print "## Found {} process{} running:".format( len(running_processes), '' if len(running_processes) == 1 else 'es') for process in running_processes: print " * {:<22} {:<10} {}".format( "{}[{}]".format(process['group'], process['name']), process['statename'], process['description']) else: print "I was able to connect to the daemon, but I did not find any process..."