Example #1
0
 def test_aborted(self, current):
     thread = GThread()
     current.return_value = thread
     event = getattr(thread, thread.ABORT)
     self.assertEqual(GThread.aborted(), event.isSet())
     # abort
     event.set()
     self.assertEqual(GThread.aborted(), event.isSet())
Example #2
0
 def test_aborted(self, current):
     thread = GThread()
     current.return_value = thread
     event = getattr(thread, thread.ABORT)
     self.assertEqual(GThread.aborted(), event.isSet())
     # abort
     event.set()
     self.assertEqual(GThread.aborted(), event.isSet())
Example #3
0
 def __init__(self, worker_id, queue):
     """
     :param worker_id: The worker id in the pool.
     :type worker_id: int
     """
     Thread.__init__(self, name='worker-%d' % worker_id)
     self.queue = queue
     self.setDaemon(True)
Example #4
0
 def __init__(self, worker_id, queue):
     """
     :param worker_id: The worker id in the pool.
     :type worker_id: int
     """
     Thread.__init__(self, name='worker-%d' % worker_id)
     self.queue = queue
     self.setDaemon(True)
Example #5
0
 def __init__(self, plugin):
     """
     :param plugin: A plugin.
     :type plugin: gofer.agent.plugin.Plugin
     """
     Thread.__init__(self, name='scheduler:%s' % plugin.name)
     self.plugin = plugin
     self.pending = Pending(plugin.name)
     self.setDaemon(True)
Example #6
0
 def __init__(self, plugin):
     """
     :param plugin: A plugin.
     :type plugin: gofer.agent.plugin.Plugin
     """
     Thread.__init__(self, name='scheduler:%s' % plugin.name)
     self.plugin = plugin
     self.pending = Pending(plugin.name)
     self.setDaemon(True)
Example #7
0
 def test_start(self, start, abort, join):
     def _register(handler, *args):
         handler(*args)
     thread = GThread()
     with patch('atexit.register') as register:
         register.side_effect = _register
         thread.start()
     start.assert_called_once_with()
     self.assertTrue(register.called)
     abort.assert_called_once_with()
     join.assert_called_once_with()
Example #8
0
 def __init__(self, worker_id, backlog=100):
     """
     :param worker_id: The worker id in the pool.
     :type worker_id: int
     :param backlog: Limits the number of calls queued.
     :type backlog: int
     """
     name = 'worker-%d' % worker_id
     Thread.__init__(self, name=name)
     self.queue = Queue(backlog)
     self.setDaemon(True)
Example #9
0
 def __init__(self, worker_id, backlog=100):
     """
     :param worker_id: The worker id in the pool.
     :type worker_id: int
     :param backlog: Limits the number of calls queued.
     :type backlog: int
     """
     name = 'worker-%d' % worker_id
     Thread.__init__(self, name=name)
     self.queue = Queue(backlog)
     self.setDaemon(True)
Example #10
0
 def __init__(self, node, url):
     """
     :param node: An AMQP queue.
     :type node: gofer.messaging.adapter.model.Node
     :param url: The broker URL.
     :type url: str
     """
     Thread.__init__(self, name=node.name)
     self.url = url
     self.node = node
     self.authenticator = None
     self._reader = None
     self.setDaemon(True)
Example #11
0
 def __init__(self, node, url):
     """
     :param node: An AMQP queue.
     :type node: gofer.messaging.adapter.model.Node
     :param url: The broker URL.
     :type url: str
     """
     Thread.__init__(self, name=node.name)
     self.url = url
     self.node = node
     self.authenticator = None
     self._reader = None
     self.setDaemon(True)
Example #12
0
 def __init__(self, node, url, wait=3):
     """
     :param node: An AMQP queue.
     :type node: gofer.messaging.adapter.model.Node
     :param url: The broker URL.
     :type url: str
     :param wait: Number of seconds to wait for a message.
     :type wait: int
     """
     Thread.__init__(self, name=node.name)
     self.url = url
     self.node = node
     self.wait = wait
     self.authenticator = None
     self.reader = None
     self.setDaemon(True)
Example #13
0
 def __init__(self, node, url, wait=3):
     """
     :param node: An AMQP queue.
     :type node: gofer.messaging.adapter.model.Node
     :param url: The broker URL.
     :type url: str
     :param wait: Number of seconds to wait for a message.
     :type wait: int
     """
     Thread.__init__(self, name=node.name)
     self.url = url
     self.node = node
     self.wait = wait
     self.authenticator = None
     self.reader = None
     self.setDaemon(True)
Example #14
0
 def run(self):
     """
     Run actions.
     """
     while not Thread.aborted():
         for plugin in Plugin.all():
             for action in plugin.actions:
                 plugin.pool.run(action)
         sleep(10)
Example #15
0
File: main.py Project: jortel/gofer
 def run(self):
     """
     Run actions.
     """
     while not Thread.aborted():
         for plugin in Plugin.all():
             for action in plugin.actions:
                 plugin.pool.run(action)
         sleep(10)
Example #16
0
File: pmon.py Project: jortel/gofer
 def run(self):
     """
     Thread main.
     """
     delay = self._precision
     while not Thread.aborted():
         sleep(delay)
         for tracker in self.paths():
             self._sniff(tracker)
Example #17
0
 def run(self):
     """
     Thread main.
     """
     delay = self._precision
     while not Thread.aborted():
         sleep(delay)
         for tracker in self.paths():
             self._sniff(tracker)
Example #18
0
    def no_route(self):
        """
        The link cannot be established.

        Likely that the queue does not exist.
        Abort and reload the plugin.

        Returns:
            Thread: The thread performing the reload.
        """
        def _reload():
            try:
                self.plugin.reload()
            except Exception:
                log.exception('Reload plugin: %s, failed', self.plugin.name)
        self.abort()
        thread = Thread(target=_reload)
        thread.start()
        return thread
Example #19
0
 def _fn(thing, *args, **kwargs):
     repair = lambda: None
     while not Thread.aborted():
         try:
             repair()
             return fn(thing, *args, **kwargs)
         except _NotFound, e:
             raise NotFound(*e.args)
         except LinkError:
             sleep(DELAY)
             repair = thing.repair
Example #20
0
 def open(self):
     """
     Open the reader.
     """
     while not Thread.aborted():
         try:
             self.reader.open()
             break
         except Exception:
             log.exception(self.getName())
             sleep(30)
Example #21
0
 def open(self):
     """
     Open the reader.
     """
     while not Thread.aborted():
         try:
             self.reader.open()
             break
         except Exception:
             log.exception(self.getName())
             sleep(30)
Example #22
0
 def _fn(thing, *args, **kwargs):
     repair = lambda: None
     while not Thread.aborted():
         try:
             repair()
             return fn(thing, *args, **kwargs)
         except _NotFound, e:
             raise NotFound(*e.args)
         except LinkError:
             sleep(DELAY)
             repair = thing.repair
Example #23
0
 def accept(self, socket):
     """
     Accept requests.
     :param socket: An open socket.
     :type socket: socket.socket
     """
     while not Thread.aborted():
         client, address = socket.accept()
         try:
             self.accepted(client)
         finally:
             client.close()
Example #24
0
 def run(self):
     """
     Main consumer loop.
     """
     self.reader = Reader(self.node, self.url)
     self.reader.authenticator = self.authenticator
     self.open()
     try:
         while not Thread.aborted():
             self.read()
     finally:
         self.close()
Example #25
0
 def run(self):
     """
     Main consumer loop.
     """
     self.reader = Reader(self.node, self.url)
     self.reader.authenticator = self.authenticator
     self.open()
     try:
         while not Thread.aborted():
             self.read()
     finally:
         self.close()
Example #26
0
 def accept(self, socket):
     """
     Accept requests.
     :param socket: An open socket.
     :type socket: socket.socket
     """
     while not Thread.aborted():
         client, address = socket.accept()
         try:
             self.accepted(client)
         finally:
             client.close()
Example #27
0
 def run(self):
     """
     Read the pending queue and dispatch requests
     to the plugin thread pool.
     """
     while not Thread.aborted():
         request = self.pending.get()
         try:
             task = Task(self.plugin, request, self.pending.commit)
             self.plugin.pool.run(task)
         except Exception:
             self.pending.commit(request.sn)
             log.exception(request.sn)
Example #28
0
 def run(self):
     """
     Read the pending queue and dispatch requests
     to the plugin thread pool.
     """
     while not Thread.aborted():
         request = self.pending.get()
         try:
             task = Task(self.plugin, request, self.pending.commit)
             self.plugin.pool.run(task)
         except Exception:
             self.pending.commit(request.sn)
             log.exception(request.sn)
Example #29
0
 def run(self):
     """
     Main run loop; processes input queue.
     """
     while not Thread.aborted():
         message = self.queue.get()
         if message == Worker.HALT:
             self.queue.put(message)
             return
         call = message
         try:
             call()
         except Exception:
             log.exception(str(call))
Example #30
0
 def run(self):
     """
     Main run loop; processes input queue.
     """
     while not Thread.aborted():
         message = self.queue.get()
         if message == Worker.HALT:
             self.queue.put(message)
             return
         call = message
         try:
             call()
         except Exception:
             log.exception(str(call))
Example #31
0
 def purge(self, url=None):
     """
     Purge (drain) all queued messages.
     :param url: The broker URL.
     :type url: str
     """
     url = url or self.url
     with Reader(self, url=url) as reader:
         while not Thread.aborted():
             message = reader.get()
             if message:
                 message.ack()
             else:
                 break
Example #32
0
 def run(self):
     """
     Main run loop; processes input queue.
     """
     while not Thread.aborted():
         call = self.queue.get()
         if call == 1:
             # # busy
             continue
         if not call:
             # termination requested
             return
         try:
             call()
         except Exception:
             log.exception(utf8(call))
Example #33
0
 def run(self):
     """
     Read the pending queue and dispatch requests
     to the plugin thread pool.
     """
     while not Thread.aborted():
         request = self.pending.get()
         try:
             pending = self.pending
             plugin = self.select_plugin(request)
             transaction = Transaction(plugin, pending, request)
             task = Task(transaction)
             plugin.pool.run(task)
         except Exception:
             self.pending.commit(request.sn)
             log.exception(request.sn)
Example #34
0
 def _fn(thing, *args, **kwargs):
     repair = lambda: None
     while not Thread.aborted():
         try:
             repair()
             return fn(thing, *args, **kwargs)
         except _NotFound as e:
             raise NotFound(*e.args)
         except LinkError as le:
             log.warning(str(le))
             repair = thing.repair
             sleep(DELAY)
         except ConnectionError as pe:
             log.warning(str(pe))
             repair = thing.repair
             sleep(DELAY)
Example #35
0
 def run(self):
     """
     Main run loop; processes input queue.
     """
     while not Thread.aborted():
         call = self.queue.get()
         if call == 1:
             # # busy
             continue
         if not call:
             # termination requested
             return
         try:
             call()
         except Exception:
             log.exception(utf8(call))
Example #36
0
 def _fn(thing, *args, **kwargs):
     repair = lambda: None
     while not Thread.aborted():
         try:
             repair()
             return fn(thing, *args, **kwargs)
         except _NotFound as e:
             raise NotFound(*e.args)
         except LinkError as le:
             log.warning(str(le))
             repair = thing.repair
             sleep(DELAY)
         except ConnectionError as pe:
             log.warning(str(pe))
             repair = thing.repair
             sleep(DELAY)
Example #37
0
 def _fn(messenger, *args, **kwargs):
     repair = lambda: None
     while not Thread.aborted():
         try:
             repair()
             return fn(messenger, *args, **kwargs)
         except LinkDetached, le:
             if le.condition != NOT_FOUND:
                 log.error(utf8(le))
                 repair = messenger.repair
                 sleep(DELAY)
             else:
                 raise NotFound(*le.args)
         except ConnectionException, pe:
             log.error(utf8(pe))
             repair = messenger.repair
             sleep(DELAY)
Example #38
0
 def _fn(messenger, *args, **kwargs):
     repair = lambda: None
     while not Thread.aborted():
         try:
             repair()
             return fn(messenger, *args, **kwargs)
         except ChannelError, le:
             if le.code != 404:
                 log.error(utf8(le))
                 repair = messenger.repair
                 sleep(DELAY)
             else:
                 raise NotFound(*le.args)
         except CONNECTION_EXCEPTIONS, pe:
             log.error(utf8(pe))
             repair = messenger.repair
             sleep(DELAY)
Example #39
0
 def _fn(messenger, *args, **kwargs):
     repair = lambda: None
     while not Thread.aborted():
         try:
             repair()
             return fn(messenger, *args, **kwargs)
         except ChannelError as le:
             if le.reply_code not in (NO_ROUTE, NOT_FOUND):
                 log.warning(str(le))
                 repair = messenger.repair
                 sleep(DELAY)
             else:
                 raise NotFound(*le.args)
         except CONNECTION_EXCEPTIONS as pe:
             log.warning(str(pe))
             repair = messenger.repair
             sleep(DELAY)
Example #40
0
 def _fn(messenger, *args, **kwargs):
     repair = lambda: None
     while not Thread.aborted():
         try:
             repair()
             return fn(messenger, *args, **kwargs)
         except LinkDetached as le:
             if le.condition != NOT_FOUND:
                 log.warning(str(le))
                 repair = messenger.repair
                 sleep(DELAY)
             else:
                 raise NotFound(*le.args)
         except ConnectionException as pe:
             log.warning(str(pe))
             repair = messenger.repair
             sleep(DELAY)
Example #41
0
 def _fn(messenger, *args, **kwargs):
     repair = lambda: None
     while not Thread.aborted():
         try:
             repair()
             return fn(messenger, *args, **kwargs)
         except ChannelError, le:
             if le.code != 404:
                 log.error(utf8(le))
                 repair = messenger.repair
                 sleep(DELAY)
             else:
                 raise NotFound(*le.args)
         except CONNECTION_EXCEPTIONS, pe:
             log.error(utf8(pe))
             repair = messenger.repair
             sleep(DELAY)
Example #42
0
    def get_reply(self, sn, reader):
        """
        Get the reply matched by serial number.
        :param sn: The request serial number.
        :type sn: str
        :param reader: A reader.
        :type reader: gofer.messaging.consumer.Reader
        :return: The matched reply document.
        :rtype: Document
        """
        timer = Timer()
        timeout = float(self.wait)

        while not Thread.aborted():
            timer.start()
            document = reader.search(sn, int(timeout))
            timer.stop()
            elapsed = timer.duration()
            if elapsed > timeout:
                raise RequestTimeout(sn, self.wait)
            else:
                timeout -= elapsed

            if not document:
                raise RequestTimeout(sn, self.wait)

            # rejected
            if document.status == 'rejected':
                raise DocumentError(
                    document.code,
                    document.description,
                    document.document,
                    document.details)

            # accepted | started
            if document.status in ('accepted', 'started'):
                continue

            # progress reported
            if document.status == 'progress':
                self.on_progress(document)
                continue

            # reply
            return self.on_reply(document)
Example #43
0
 def purge(self, url=None):
     """
     Purge (drain) all queued messages.
     :param url: The broker URL.
     :type url: str
     """
     url = url or self.url
     reader = Reader(self, url=url)
     reader.open()
     try:
         while not Thread.aborted():
             message = reader.get()
             if message:
                 message.ack()
             else:
                 break
     finally:
         reader.close()
Example #44
0
 def run(self):
     """
     Read the pending queue and dispatch requests
     to the plugin thread pool.
     """
     while not Thread.aborted():
         try:
             request = self.pending.get()
         except Empty:
             # aborted
             break
         try:
             plugin = self.select_plugin(request)
             transaction = Transaction(plugin, self.pending, request)
             task = Task(transaction)
             plugin.pool.run(task)
         except Exception:
             self.pending.commit(request.sn)
             log.exception(request.sn)
Example #45
0
    def get_reply(self, sn, reader):
        """
        Get the reply matched by serial number.
        :param sn: The request serial number.
        :type sn: str
        :param reader: A reader.
        :type reader: gofer.messaging.consumer.Reader
        :return: The matched reply document.
        :rtype: Document
        """
        timer = Timer()
        timeout = float(self.wait)

        while not Thread.aborted():
            timer.start()
            document = reader.search(sn, int(timeout))
            timer.stop()
            elapsed = timer.duration()
            if elapsed > timeout:
                raise RequestTimeout(sn, self.wait)
            else:
                timeout -= elapsed

            if not document:
                raise RequestTimeout(sn, self.wait)

            # rejected
            if document.status == 'rejected':
                raise DocumentError(document.code, document.description,
                                    document.document, document.details)

            # accepted | started
            if document.status in ('accepted', 'started'):
                continue

            # progress reported
            if document.status == 'progress':
                self.on_progress(document)
                continue

            # reply
            return self.on_reply(document)
Example #46
0
 def search(self, sn, timeout=90):
     """
     Search for a document by serial number.
     :param sn: A serial number.
     :type sn: str
     :param timeout: The read timeout.
     :type timeout: int
     :return: The matched document.
     :rtype: Document
     :raise: ModelError
     """
     while not Thread.aborted():
         message, document = self.next(timeout)
         if message:
             message.ack()
         else:
             return
         if sn == document.sn:
             # matched
             return document
Example #47
0
File: rmi.py Project: jortel/gofer
 def run(self):
     """
     Read the pending queue and dispatch requests
     to the plugin thread pool.
     """
     self.builtin.start()
     while not Thread.aborted():
         try:
             request = self.pending.get()
         except Empty:
             # aborted
             break
         try:
             plugin = self.select_plugin(request)
             transaction = Transaction(plugin, self.pending, request)
             task = Task(transaction)
             plugin.pool.run(task)
         except Exception:
             self.pending.commit(request.sn)
             log.exception(request.sn)
Example #48
0
 def shutdown(self, hard=False):
     """
     Shutdown the pool.
     Drain the queue, terminate and join all workers.
     :param hard: Abort threads in the pool.
         When not aborted, work in progress will attempt to
         be completed before shutdown.
     :type hard: bool
     :return: List of orphaned calls.  List of: Call.
     :rtype: list
     """
     drained = self.drain()
     if hard:
         for t in self.threads:
             t.abort()
     self.queue.put(Worker.HALT)
     for t in self.threads:
         if t == Thread.current():
             continue
         t.join()
     return drained
Example #49
0
 def shutdown(self, hard=False):
     """
     Shutdown the pool.
     Drain the queue, terminate and join all workers.
     :param hard: Abort threads in the pool.
         When not aborted, work in progress will attempt to
         be completed before shutdown.
     :type hard: bool
     :return: List of orphaned calls.  List of: Call.
     :rtype: list
     """
     drained = self.drain()
     if hard:
         for t in self.threads:
             t.abort()
     self.queue.put(Worker.HALT)
     for t in self.threads:
         if t == Thread.current():
             continue
         t.join()
     return drained
Example #50
0
File: main.py Project: jortel/gofer
 def __init__(self):
     Thread.__init__(self, name='Actions')
     self.setDaemon(True)
Example #51
0
 def test_abort(self):
     thread = GThread()
     event = getattr(thread, thread.ABORT)
     self.assertFalse(event.isSet())
     thread.abort()
     self.assertTrue(event.isSet())
Example #52
0
 def test_current(self, current):
     self.assertEqual(GThread.current(), current.return_value)
Example #53
0
 def __init__(self):
     Thread.__init__(self, name='Actions')
     self.setDaemon(True)
Example #54
0
 def test_abort(self):
     thread = GThread()
     event = getattr(thread, thread.ABORT)
     self.assertFalse(event.isSet())
     thread.abort()
     self.assertTrue(event.isSet())