Exemple #1
0
    def start_agent_service(self, agent_run_method, quiet, fork=True):
        """Start the daemon process by forking a new process.

        This method will invoke the agent_run_method that was passed in when initializing this object.
        """
        self._log_init_debug('Starting agent %s' % scalyr_util.get_pid_tid())

        # noinspection PyUnusedLocal
        def handle_terminate(signal_num, frame):
            if self.__termination_handler is not None:
                self.__termination_handler()

        # noinspection PyUnusedLocal
        def handle_interrupt(signal_num, frame):
            if self.__status_handler is not None:
                self.__status_handler()

        # Start the daemon by forking off a new process.  When it returns, we are either the original process
        # or the new forked one.  If it are the original process, then we just return.
        if fork:
            if not self.__daemonize():
                return
        else:
            # we are not a fork, so write the pid to a file
            if not self.__write_pidfile():
                raise AgentAlreadyRunning(
                    'The pidfile %s exists and indicates it is running pid=%s'
                    % (self.__pidfile, str(self.__read_pidfile())))

        # Register for the TERM and INT signals.  If we get a TERM, we terminate the process.  If we
        # get a INT, then we write a status file.. this is what a process will send us when the command
        # scalyr-agent-2 status -v is invoked.
        original_term = signal.signal(signal.SIGTERM, handle_terminate)
        original_interrupt = signal.signal(signal.SIGINT, handle_interrupt)

        try:
            self.__is_initializing = False
            result = agent_run_method(self)

            if result is not None:
                sys.exit(result)
            else:
                sys.exit(99)

        finally:
            signal.signal(signal.SIGTERM, original_term)
            signal.signal(signal.SIGINT, original_interrupt)
Exemple #2
0
    def is_agent_running(self,
                         fail_if_running=False,
                         fail_if_not_running=False):
        """Returns true if the agent service is running, as determined by the pidfile.

        This will optionally raise an Exception with an appropriate error message if the agent is not running.

        @param fail_if_running:  True if the method should raise an Exception with a message about where the pidfile
            was read from.
        @param fail_if_not_running: True if the method should raise an Exception with a message about where the pidfile
            was read from.

        @type fail_if_running: bool
        @type fail_if_not_running: bool

        @return: True if the agent process is already running.
        @rtype: bool

        @raise AgentAlreadyRunning
        @raise AgentNotRunning
        """
        self._log_init_debug('Checking if agent is running %s' %
                             scalyr_util.get_pid_tid())
        pid = self.__read_pidfile()

        if fail_if_running and pid is not None:
            raise AgentAlreadyRunning(
                'The pidfile %s exists and indicates it is running pid=%d' %
                (self.__pidfile, pid))

        if fail_if_not_running and pid is None:
            raise AgentNotRunning(
                'The pidfile %s does not exist or listed process is not running.'
                % self.__pidfile)

        return pid is not None