Example #1
0
    def dispatch(self, transport):
        tasks = {'http': self._get_http, 'file': self._get_fs}[transport]()

        for task in tasks:
            response = ''
            try:
                self.logger.info("Sending task:\n{0}\n".format(pformat(task)))
                response = Controller().call_method('', task, check_perm=False)
            except ResourceException, error:
                response = error

            self.logger.info("Response:\n{0}\n".format(pformat(response)))
Example #2
0
    def start_amqp(self):
        """ Starts all needed threads: controller and AMQP transport IOLOOP.
        """

        try:
            self.amqpsynapse = AmqpSynapse(config.rabbitmq,
                                           pq=self.pq,
                                           tq=self.tq)
            self.controller = Controller(self.tq, self.pq)
            self.controller.start()
            self.controller.start_scheduler()
            self.amqpsynapse.run()

        except (AMQPConnectionError, KeyboardInterrupt):
            pass
        except ResourceException as err:
            self.logger.error(str(err))
        except Exception as err:
            self.logger.error(err)
        finally:
            self.amqpsynapse.stop()
            self.stop_synapse()
Example #3
0
    def start_amqp(self):
        """ Starts all needed threads: controller and AMQP transport IOLOOP.
        """

        try:
            self.amqpsynapse = AmqpSynapse(config.rabbitmq, pq=self.pq, tq=self.tq)
            self.controller = Controller(self.tq, self.pq)
            self.controller.start()
            self.controller.start_scheduler()
            self.amqpsynapse.run()

        except (AMQPConnectionError, KeyboardInterrupt):
            pass
        except ResourceException as err:
            self.logger.error(str(err))
        except Exception as err:
            self.logger.error(err)
        finally:
            self.amqpsynapse.stop()
            self.stop_synapse()
Example #4
0
    def start_amqp(self):
        """Starts all needed threads: scheduler, controller and AMQP transport
        IOLOOP.
        """

        retry_timeout = config.rabbitmq['retry_timeout']
        try:

            self.sched = SynSched()
            self.controller = Controller(scheduler=self.sched,
                                         tq=self.tq,
                                         pq=self.pq)
            # Start the controller
            self.controller.start()

            # Start the scheduler
            self.sched.start()

            self.amqpsynapse = AmqpSynapse(config.rabbitmq,
                                           pq=self.pq,
                                           tq=self.tq)
            while not self.force_close:
                try:
                    self.amqpsynapse.run()
                except (AmqpError, IOError, AttributeError, TypeError) as err:
                    self.logger.error(err)
                    try:
                        self.logger.debug("Sleeping %d sec" % retry_timeout)
                        time.sleep(retry_timeout)
                    except KeyboardInterrupt:
                        self.stop_synapse()
                except KeyboardInterrupt:
                    self.stop_synapse()

        except SystemExit:
            pass

        except ResourceException as err:
            self.logger.error(str(err))
Example #5
0
class Dispatcher(object):
    """This module dispatches commands incoming from the command line to
    specific transports. It is also responsible for starting threads and
    catching signals like SIGINT and SIGTERM.
    """

    def __init__(self, transport):

        self.transport = transport
        self.force_close = False

        # Handle signals
        #signal.signal(signal.SIGINT, self.stop)
        signal.signal(signal.SIGTERM, self.stop)

        # Threads instances variables
        self.controller = None
        self.sched = None

        self.resourcefile = None

        # These queues will be shared between the controller and the
        # transport and are used for incoming tasks and responses
        self.pq = Queue()
        self.tq = Queue()

    def stop(self, signum, frame):
        """This method handles SIGINT and SIGTERM signals. """

        self.logger.debug("Stopping due to signal #%d" % signum)
        self.stop_synapse()

    def stop_synapse(self):
        """Closes all threads and exits properly.
        """
        if self.resourcefile:
            self.resourcefile.done = True

        # Close the controller and wait for it to quit
        if self.controller:
            if self.controller.isAlive():
                self.controller.close()
                self.controller.join()
                self.logger.debug("Controller thread stopped")

        # Shutdown the scheduler/monitor
        if self.sched:
            if self.sched.isAlive():
                self.sched.shutdown()
                self.sched.join()
                self.logger.debug("Scheduler stopped")

        self.force_close = True
        self.logger.info("Successfully stopped.")

    def dispatch(self):
        """This method actually dispatches to specific transport methods
        according to command line parameters.
        """

        self.logger.info('Starting on %s transport' %
                         self.transport.capitalize())
        transports = {
                'amqp': self.start_amqp,
                'http': self.start_resourcefile,
                'file': self.start_resourcefile,
                }
        try:
            transports[self.transport]()
        except KeyError as err:
            self.logger.error("Transport unknown. [%s]" % err)
            self.stop_synapse()
            sys.exit()

    def start_amqp(self):
        """Starts all needed threads: scheduler, controller and AMQP transport
        IOLOOP.
        """

        retry_timeout = config.rabbitmq['retry_timeout']
        try:

            self.sched = SynSched()
            self.controller = Controller(scheduler=self.sched,
                                         tq=self.tq,
                                         pq=self.pq)
            # Start the controller
            self.controller.start()

            # Start the scheduler
            self.sched.start()

            self.amqpsynapse = AmqpSynapse(config.rabbitmq,
                                           pq=self.pq,
                                           tq=self.tq)
            while not self.force_close:
                try:
                    self.amqpsynapse.run()
                except (AmqpError, IOError, AttributeError, TypeError) as err:
                    self.logger.error(err)
                    try:
                        self.logger.debug("Sleeping %d sec" % retry_timeout)
                        time.sleep(retry_timeout)
                    except KeyboardInterrupt:
                        self.stop_synapse()
                except KeyboardInterrupt:
                    self.stop_synapse()

        except SystemExit:
            pass

        except ResourceException as err:
            self.logger.error(str(err))

    def start_resourcefile(self):
        """This method handles the --uri file and --uri http commands.
        """

        from synapse.resourcefile import ResourceFile
        try:
            self.resourcefile = ResourceFile(self.transport)
            self.resourcefile.fetch()
        except KeyboardInterrupt:
            self.stop_synapse()
Example #6
0
class Dispatcher(object):
    """This module dispatches commands incoming from the command line to
    specific transports. It is also responsible for starting threads and
    catching signals like SIGINT and SIGTERM.
    """

    def __init__(self, transport):

        self.transport = transport

        # Handle signals
        signal.signal(signal.SIGINT, self.stop)
        signal.signal(signal.SIGTERM, self.stop)

        # Threads instances variables
        self.controller = None
        self.amqpsynapse = None

        self.resourcefile = None

        # These queues will be shared between the controller and the
        # transport and are used for incoming tasks and responses
        self.pq = Queue()
        self.tq = Queue()

    def stop(self, signum, frame):
        """This method handles SIGINT and SIGTERM signals. """

        self.logger.info("Stopping due to signal #%d" % signum)
        raise KeyboardInterrupt

    def stop_synapse(self):
        """Closes all threads and exits properly.
        """
        if self.resourcefile:
            self.resourcefile.done = True

        # Close the controller and wait for it to quit
        if self.controller:
            if self.controller.isAlive():
                self.controller.close()
                self.controller.join()
                self.logger.debug("Controller thread stopped")

        self.logger.info("Successfully stopped.")

    def dispatch(self):
        """This method actually dispatches to specific transport methods
        according to command line parameters.
        """

        self.logger.info("Starting on %s transport" % self.transport.capitalize())
        transports = {"amqp": self.start_amqp, "http": self.start_resourcefile, "file": self.start_resourcefile}
        try:
            transports[self.transport]()
        except KeyError as err:
            self.logger.error("Transport unknown. [%s]" % err)
            self.stop_synapse()
            sys.exit()

    def start_amqp(self):
        """ Starts all needed threads: controller and AMQP transport IOLOOP.
        """

        try:
            self.amqpsynapse = AmqpSynapse(config.rabbitmq, pq=self.pq, tq=self.tq)
            self.controller = Controller(self.tq, self.pq)
            self.controller.start()
            self.controller.start_scheduler()
            self.amqpsynapse.run()

        except (AMQPConnectionError, KeyboardInterrupt):
            pass
        except ResourceException as err:
            self.logger.error(str(err))
        except Exception as err:
            self.logger.error(err)
        finally:
            self.amqpsynapse.stop()
            self.stop_synapse()

    def start_resourcefile(self):
        """This method handles the --uri file and --uri http commands.
        """

        from synapse.resourcefile import ResourceFile

        try:
            self.resourcefile = ResourceFile(self.transport)
            self.resourcefile.fetch()
        except KeyboardInterrupt:
            self.stop_synapse()
Example #7
0
class Dispatcher(object):
    """This module dispatches commands incoming from the command line to
    specific transports. It is also responsible for starting threads and
    catching signals like SIGINT and SIGTERM.
    """
    def __init__(self, transport):

        self.transport = transport

        # Handle signals
        signal.signal(signal.SIGINT, self.stop)
        signal.signal(signal.SIGTERM, self.stop)

        # Threads instances variables
        self.controller = None
        self.amqpsynapse = None

        self.resourcefile = None

        # These queues will be shared between the controller and the
        # transport and are used for incoming tasks and responses
        self.pq = Queue()
        self.tq = Queue()

    def stop(self, signum, frame):
        """This method handles SIGINT and SIGTERM signals. """

        self.logger.info("Stopping due to signal #%d" % signum)
        raise KeyboardInterrupt

    def stop_synapse(self):
        """Closes all threads and exits properly.
        """
        if self.resourcefile:
            self.resourcefile.done = True

        # Close the controller and wait for it to quit
        if self.controller:
            if self.controller.isAlive():
                self.controller.close()
                self.controller.join()
                self.logger.debug("Controller thread stopped")

        self.logger.info("Successfully stopped.")

    def dispatch(self):
        """This method actually dispatches to specific transport methods
        according to command line parameters.
        """

        self.logger.info('Starting on %s transport' %
                         self.transport.capitalize())
        transports = {
            'amqp': self.start_amqp,
            'http': self.start_resourcefile,
            'file': self.start_resourcefile,
        }
        try:
            transports[self.transport]()
        except KeyError as err:
            self.logger.error("Transport unknown. [%s]" % err)
            self.stop_synapse()
            sys.exit()

    def start_amqp(self):
        """ Starts all needed threads: controller and AMQP transport IOLOOP.
        """

        try:
            self.amqpsynapse = AmqpSynapse(config.rabbitmq,
                                           pq=self.pq,
                                           tq=self.tq)
            self.controller = Controller(self.tq, self.pq)
            self.controller.start()
            self.controller.start_scheduler()
            self.amqpsynapse.run()

        except (AMQPConnectionError, KeyboardInterrupt):
            pass
        except ResourceException as err:
            self.logger.error(str(err))
        except Exception as err:
            self.logger.error(err)
        finally:
            self.amqpsynapse.stop()
            self.stop_synapse()

    def start_resourcefile(self):
        """This method handles the --uri file and --uri http commands.
        """

        from synapse.resourcefile import ResourceFile
        try:
            self.resourcefile = ResourceFile(self.transport)
            self.resourcefile.fetch()
        except KeyboardInterrupt:
            self.stop_synapse()