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())
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)
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)
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()
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)
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)
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)
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)
def run(self): """ Thread main. """ delay = self._precision while not Thread.aborted(): sleep(delay) for tracker in self.paths(): self._sniff(tracker)
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
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
def open(self): """ Open the reader. """ while not Thread.aborted(): try: self.reader.open() break except Exception: log.exception(self.getName()) sleep(30)
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()
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()
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)
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))
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
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))
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)
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)
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)
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)
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)
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)
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)
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()
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)
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)
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
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)
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
def __init__(self): Thread.__init__(self, name='Actions') self.setDaemon(True)
def test_abort(self): thread = GThread() event = getattr(thread, thread.ABORT) self.assertFalse(event.isSet()) thread.abort() self.assertTrue(event.isSet())
def test_current(self, current): self.assertEqual(GThread.current(), current.return_value)