Exemple #1
0
    def execute_command(self, options):
        """
        Send execute command to daemon through IPC or perform execution
        on current process.

        Fires events:

        * manager.execute.completed

        :param options: argparse options
        """
        # If a daemon is started, send the execution to the daemon
        ipc_info = self.check_ipc_info()
        if ipc_info:
            try:
                log.info(
                    'There is a daemon running for this config. Sending execution to running daemon.'
                )
                client = IPCClient(ipc_info['port'], ipc_info['password'])
            except ValueError as e:
                log.error(e)
            else:
                client.execute(dict(options, loglevel=self.options.loglevel))
            self.shutdown()
            return
        # Otherwise we run the execution ourselves
        with self.acquire_lock():
            fire_event('manager.execute.started', self)
            self.task_queue.start()
            self.execute(options)
            self.shutdown(finish_queue=True)
            self.task_queue.wait()
            fire_event('manager.execute.completed', self)
Exemple #2
0
    def execute_command(self, options):
        """
        Send execute command to daemon through IPC or perform execution
        on current process.

        Fires events:

        * manager.execute.completed

        :param options: argparse options
        """
        # If a daemon is started, send the execution to the daemon
        ipc_info = self.check_ipc_info()
        if ipc_info:
            client = IPCClient(ipc_info['port'], ipc_info['password'])
            client.execute(dict(options))
            self.shutdown()
            return
        # Otherwise we run the execution ourselves
        with self.acquire_lock():
            fire_event('manager.execute.started', self)
            self.scheduler.start(run_schedules=False)
            self.scheduler.execute(options)
            self.scheduler.shutdown(finish_queue=True)
            try:
                self.scheduler.wait()
            except KeyboardInterrupt:
                log.error('Got ctrl-c exiting after this task completes. Press ctrl-c again to abort this task.')
            else:
                fire_event('manager.execute.completed', self)
            self.shutdown(finish_queue=False)
Exemple #3
0
    def execute_command(self, options):
        """
        Send execute command to daemon through IPC or perform execution
        on current process.

        Fires events:

        * manager.execute.completed

        :param options: argparse options
        """
        # If a daemon is started, send the execution to the daemon
        ipc_info = self.check_ipc_info()
        if ipc_info:
            try:
                log.info('There is a daemon running for this config. Sending execution to running daemon.')
                client = IPCClient(ipc_info['port'], ipc_info['password'])
            except ValueError as e:
                log.error(e)
            else:
                client.execute(dict(options, loglevel=self.options.loglevel))
            self.shutdown()
            return
        # Otherwise we run the execution ourselves
        with self.acquire_lock():
            fire_event('manager.execute.started', self)
            self.task_queue.start()
            self.execute(options)
            self.shutdown(finish_queue=True)
            self.task_queue.wait()
            fire_event('manager.execute.completed', self)
Exemple #4
0
    def start(self):
        """
        Starting point when executing from commandline, dispatch execution to correct destination.

        If there is a FlexGet process with an ipc server already running, the command will be sent there for execution
        and results will be streamed back.
        If not, this will attempt to obtain a lock, initialize the manager, and run the command here.
        """
        # If another process is started, send the execution to the running process
        ipc_info = self.check_ipc_info()
        if ipc_info:
            try:
                log.info('There is a FlexGet process already running for this config, sending execution there.')
                client = IPCClient(ipc_info['port'], ipc_info['password'])
            except ValueError as e:
                log.error(e)
            else:
                try:
                    client.handle_cli(self.args)
                except KeyboardInterrupt:
                    log.error('Disconnecting from daemon due to ctrl-c. Executions will still continue in the '
                              'background.')
                except EOFError:
                    log.error('Connection from daemon was severed.')
            return
        # No running process, we start our own to handle command
        with self.acquire_lock():
            self.initialize()
            self.handle_cli()
            self._shutdown()
Exemple #5
0
    def execute_command(self, options):
        """
        Send execute command to daemon through IPC or perform execution
        on current process.

        Fires events:

        * manager.execute.completed

        :param options: argparse options
        """
        # If a daemon is started, send the execution to the daemon
        ipc_info = self.check_ipc_info()
        if ipc_info:
            client = IPCClient(ipc_info['port'], ipc_info['password'])
            client.execute(dict(options))
            self.shutdown()
            return
        # Otherwise we run the execution ourselves
        with self.acquire_lock():
            fire_event('manager.execute.started', self)
            self.scheduler.start(run_schedules=False)
            self.scheduler.execute(options)
            self.scheduler.shutdown(finish_queue=True)
            try:
                self.scheduler.wait()
            except KeyboardInterrupt:
                log.error(
                    'Got ctrl-c exiting after this task completes. Press ctrl-c again to abort this task.'
                )
            else:
                fire_event('manager.execute.completed', self)
            self.shutdown(finish_queue=False)
Exemple #6
0
    def start(self):
        """
        Starting point when executing from commandline, dispatch execution to correct destination.

        If there is a FlexGet process with an ipc server already running, the command will be sent there for execution
        and results will be streamed back.
        If not, this will attempt to obtain a lock, initialize the manager, and run the command here.
        """
        # If another process is started, send the execution to the running process
        ipc_info = self.check_ipc_info()
        if ipc_info:
            try:
                log.info(
                    'There is a FlexGet process already running for this config, sending execution there.'
                )
                client = IPCClient(ipc_info['port'], ipc_info['password'])
            except ValueError as e:
                log.error(e)
            else:
                try:
                    client.handle_cli(self.args)
                except KeyboardInterrupt:
                    log.error(
                        'Disconnecting from daemon due to ctrl-c. Executions will still continue in the '
                        'background.')
                except EOFError:
                    log.error('Connection from daemon was severed.')
            return
        # No running process, we start our own to handle command
        with self.acquire_lock():
            self.initialize()
            self.handle_cli()
            self._shutdown()
Exemple #7
0
    def start(self):
        """
        Starting point when executing from commandline, dispatch execution to correct destination.

        If there is a FlexGet process with an ipc server already running, the command will be sent there for execution
        and results will be streamed back.
        If not, this will attempt to obtain a lock, initialize the manager, and run the command here.
        """
        if sys.version_info <= (2, 7):
            console('-' * 79)
            console('Python 2.7 will not be maintained past 2020 !')
            console(
                'Consider upgrading to 3.6 or newer at your earliest convenience.'
            )
            console('-' * 79)
        # When we are in test mode, we use a different lock file and db
        if self.options.test:
            self.lockfile = os.path.join(self.config_base,
                                         '.test-%s-lock' % self.config_name)
        # If another process is started, send the execution to the running process
        ipc_info = self.check_ipc_info()
        if ipc_info:
            console(
                'There is a FlexGet process already running for this config, sending execution there.'
            )
            log.debug('Sending command to running FlexGet process: %s' %
                      self.args)
            try:
                client = IPCClient(ipc_info['port'], ipc_info['password'])
            except ValueError as e:
                log.error(e)
            else:
                try:
                    client.handle_cli(self.args)
                except KeyboardInterrupt:
                    log.error(
                        'Disconnecting from daemon due to ctrl-c. Executions will still continue in the '
                        'background.')
                except EOFError:
                    log.error('Connection from daemon was severed.')
            return
        if self.options.test:
            log.info('Test mode, creating a copy from database ...')
            db_test_filename = os.path.join(
                self.config_base, 'test-%s.sqlite' % self.config_name)
            if os.path.exists(self.db_filename):
                shutil.copy(self.db_filename, db_test_filename)
                log.info('Test database created')
            self.db_filename = db_test_filename
        # No running process, we start our own to handle command
        with self.acquire_lock():
            self.initialize()
            self.handle_cli()
            self._shutdown()
Exemple #8
0
    def start(self) -> None:
        """
        Starting point when executing from commandline, dispatch execution to correct destination.

        If there is a FlexGet process with an ipc server already running, the command will be sent there for execution
        and results will be streamed back.
        If not, this will attempt to obtain a lock, initialize the manager, and run the command here.
        """
        # When we are in test mode, we use a different lock file and db
        if self.options.test:
            self.lockfile = os.path.join(self.config_base,
                                         '.test-%s-lock' % self.config_name)
        # If another process is started, send the execution to the running process
        ipc_info = self.check_ipc_info()
        # If we are connecting to a running daemon, we don't want to log to the log file,
        # the daemon is already handling that.
        self._init_logging(to_file=not ipc_info)
        if ipc_info:
            console(
                'There is a FlexGet process already running for this config, sending execution there.'
            )
            logger.debug('Sending command to running FlexGet process: {}',
                         self.args)
            try:
                client = IPCClient(ipc_info['port'], ipc_info['password'])
            except ValueError as e:
                logger.error(e)
            else:
                try:
                    client.handle_cli(self.args)
                except KeyboardInterrupt:
                    logger.error(
                        'Disconnecting from daemon due to ctrl-c. Executions will still continue in the '
                        'background.')
                except EOFError:
                    logger.error('Connection from daemon was severed.')
            return
        if self.options.test:
            logger.info('Test mode, creating a copy from database ...')
            db_test_filename = os.path.join(
                self.config_base, 'test-%s.sqlite' % self.config_name)
            if os.path.exists(self.db_filename):
                shutil.copy(self.db_filename, db_test_filename)
                logger.info('Test database created')
            self.db_filename = db_test_filename
        # No running process, we start our own to handle command
        with self.acquire_lock():
            self.initialize()
            self.handle_cli()
            self._shutdown()
Exemple #9
0
    def start(self):
        """
        Starting point when executing from commandline, dispatch execution to correct destination.

        If there is a FlexGet process with an ipc server already running, the command will be sent there for execution
        and results will be streamed back.
        If not, this will attempt to obtain a lock, initialize the manager, and run the command here.
        """
        if sys.version_info <= (2, 7):
            console('-' * 79)
            console('Python 2.7 will not be maintained past 2020 !')
            console('Consider upgrading to 3.6 or newer at your earliest convenience.')
            console('-' * 79)
        # When we are in test mode, we use a different lock file and db
        if self.options.test:
            self.lockfile = os.path.join(self.config_base, '.test-%s-lock' % self.config_name)
        # If another process is started, send the execution to the running process
        ipc_info = self.check_ipc_info()
        if ipc_info:
            console(
                'There is a FlexGet process already running for this config, sending execution there.'
            )
            log.debug('Sending command to running FlexGet process: %s' % self.args)
            try:
                client = IPCClient(ipc_info['port'], ipc_info['password'])
            except ValueError as e:
                log.error(e)
            else:
                try:
                    client.handle_cli(self.args)
                except KeyboardInterrupt:
                    log.error(
                        'Disconnecting from daemon due to ctrl-c. Executions will still continue in the '
                        'background.'
                    )
                except EOFError:
                    log.error('Connection from daemon was severed.')
            return
        if self.options.test:
            log.info('Test mode, creating a copy from database ...')
            db_test_filename = os.path.join(self.config_base, 'test-%s.sqlite' % self.config_name)
            if os.path.exists(self.db_filename):
                shutil.copy(self.db_filename, db_test_filename)
                log.info('Test database created')
            self.db_filename = db_test_filename
        # No running process, we start our own to handle command
        with self.acquire_lock():
            self.initialize()
            self.handle_cli()
            self._shutdown()
Exemple #10
0
    def daemon_command(self, options):
        """
        Fires events:

        * manager.daemon.started
        * manager.daemon.completed

        :param options: argparse options
        """
        if options.action == 'start':
            if options.daemonize:
                self.daemonize()
            with self.acquire_lock():
                try:
                    signal.signal(signal.SIGTERM, self._handle_sigterm)
                except ValueError as e:
                    # If flexget is being called from another script, e.g. windows service helper, and we are not the
                    # main thread, this error will occur.
                    log.debug('Error registering sigterm handler: %s' % e)
                self.ipc_server.start()
                fire_event('manager.daemon.started', self)
                self.scheduler.start()
                try:
                    self.scheduler.wait()
                except KeyboardInterrupt:
                    log.info('Got ctrl-c, shutting down.')
                    fire_event('manager.daemon.completed', self)
                    self.shutdown(finish_queue=False)
        elif options.action == 'status':
            ipc_info = self.check_ipc_info()
            if ipc_info:
                log.info('Daemon running. (PID: %s)' % ipc_info['pid'])
            else:
                log.info('No daemon appears to be running for this config.')
        elif options.action in ['stop', 'reload']:
            ipc_info = self.check_ipc_info()
            if ipc_info:
                try:
                    client = IPCClient(ipc_info['port'], ipc_info['password'])
                except ValueError as e:
                    log.error(e)
                else:
                    if options.action == 'stop':
                        client.shutdown()
                    elif options.action == 'reload':
                        client.reload()
                self.shutdown()
            else:
                log.error('There does not appear to be a daemon running.')
Exemple #11
0
    def daemon_command(self, options):
        """
        Fires events:

        * manager.daemon.started
        * manager.daemon.completed

        :param options: argparse options
        """
        if options.action == 'start':
            if options.daemonize:
                self.daemonize()
            with self.acquire_lock():
                signal.signal(signal.SIGTERM, self._handle_sigterm)
                self.ipc_server.start()
                fire_event('manager.daemon.started', self)
                self.scheduler.start()
                try:
                    self.scheduler.wait()
                except KeyboardInterrupt:
                    log.info('Got ctrl-c, shutting down.')
                    fire_event('manager.daemon.completed', self)
                    self.shutdown(finish_queue=False)
        elif options.action == 'status':
            ipc_info = self.check_ipc_info()
            if ipc_info:
                log.info('Daemon running. (PID: %s)' % ipc_info['pid'])
            else:
                log.info('No daemon appears to be running for this config.')
        elif options.action in ['stop', 'reload']:
            ipc_info = self.check_ipc_info()
            if ipc_info:
                try:
                    client = IPCClient(ipc_info['port'], ipc_info['password'])
                except ValueError as e:
                    log.error(e)
                else:
                    if options.action == 'stop':
                        client.shutdown()
                    elif options.action == 'reload':
                        client.reload()
                self.shutdown()
            else:
                log.error('There does not appear to be a daemon running.')
Exemple #12
0
    def start(self):
        """
        Starting point when executing from commandline, dispatch execution to correct destination.

        If there is a FlexGet process with an ipc server already running, the command will be sent there for execution
        and results will be streamed back.
        If not, this will attempt to obtain a lock, initialize the manager, and run the command here.
        """
        # When we are in test mode, we use a different lock file and db
        if self.options.test:
            self.lockfile = os.path.join(self.config_base, ".test-%s-lock" % self.config_name)
        # If another process is started, send the execution to the running process
        ipc_info = self.check_ipc_info()
        if ipc_info:
            console("There is a FlexGet process already running for this config, sending execution there.")
            log.debug("Sending command to running FlexGet process: %s" % self.args)
            try:
                client = IPCClient(ipc_info["port"], ipc_info["password"])
            except ValueError as e:
                log.error(e)
            else:
                try:
                    client.handle_cli(self.args)
                except KeyboardInterrupt:
                    log.error(
                        "Disconnecting from daemon due to ctrl-c. Executions will still continue in the " "background."
                    )
                except EOFError:
                    log.error("Connection from daemon was severed.")
            return
        if self.options.test:
            log.info("Test mode, creating a copy from database ...")
            db_test_filename = os.path.join(self.config_base, "test-%s.sqlite" % self.config_name)
            if os.path.exists(self.db_filename):
                shutil.copy(self.db_filename, db_test_filename)
                log.info("Test database created")
            self.db_filename = db_test_filename
        # No running process, we start our own to handle command
        with self.acquire_lock():
            self.initialize()
            self.handle_cli()
            self._shutdown()
Exemple #13
0
    def daemon_command(self, options):
        """
        Fires events:

        * manager.daemon.started
        * manager.daemon.completed

        :param options: argparse options
        """
        if options.action == 'start':
            if options.daemonize:
                self.daemonize()
            with self.acquire_lock():
                signal.signal(signal.SIGTERM, self._handle_sigterm)
                self.ipc_server.start()
                fire_event('manager.daemon.started', self)
                self.scheduler.start()
                try:
                    self.scheduler.wait()
                except KeyboardInterrupt:
                    log.info('Got ctrl-c, shutting down.')
                    fire_event('manager.daemon.completed', self)
                    self.shutdown(finish_queue=False)
        elif options.action == 'stop':
            ipc_info = self.check_ipc_info()
            if ipc_info:
                client = IPCClient(ipc_info['port'], ipc_info['password'])
                client.shutdown()
                self.shutdown()
            else:
                log.error('There does not appear to be a daemon running.')
        elif options.action == 'status':
            ipc_info = self.check_ipc_info()
            if ipc_info:
                log.info('Daemon running. (PID: %s)' % ipc_info['pid'])
            else:
                log.info('No daemon appears to be running for this config.')
Exemple #14
0
    def daemon_command(self, options):
        """
        Fires events:

        * manager.daemon.started
        * manager.daemon.completed

        :param options: argparse options
        """
        if options.action == 'start':
            if options.daemonize:
                self.daemonize()
            with self.acquire_lock():
                try:
                    signal.signal(signal.SIGTERM, self._handle_sigterm)
                except ValueError as e:
                    # If flexget is being called from another script, e.g. windows service helper, and we are not the
                    # main thread, this error will occur.
                    log.debug('Error registering sigterm handler: %s' % e)
                self.ipc_server.start()
                fire_event('manager.daemon.started', self)
                self.scheduler.start()
                try:
                    self.scheduler.wait()
                except KeyboardInterrupt:
                    log.info('Got ctrl-c, shutting down.')
                    fire_event('manager.daemon.completed', self)
                    self.shutdown(finish_queue=False)
        elif options.action == 'status':
            ipc_info = self.check_ipc_info()
            if ipc_info:
                log.info('Daemon running. (PID: %s)' % ipc_info['pid'])
            else:
                log.info('No daemon appears to be running for this config.')
        elif options.action in ['stop', 'reload']:
            ipc_info = self.check_ipc_info()
            if ipc_info:
                try:
                    client = IPCClient(ipc_info['port'], ipc_info['password'])
                except ValueError as e:
                    log.error(e)
                else:
                    if options.action == 'stop':
                        client.shutdown()
                    elif options.action == 'reload':
                        client.reload()
                self.shutdown()
            else:
                log.error('There does not appear to be a daemon running.')