def dequeue(self, queued_signal=None): """ Deserialize and execute a signal, either from the queue or as per the contents of the queued_signal kwarg. If queued_signal contains a serialized signal call datastructure,* dequeue() will deserialize and execute that serialized signal without popping the queue. If queued_signal is None, it will call retrieve() to pop the queue for the next signal, which it will execute if one is returned successfully. * See the QueueBase docstring for an example. """ from django.db.models.loading import cache if queued_signal is None: queued_signal = self.retrieve() if queued_signal is not None: logg.debug("Dequeueing signal: %s" % queued_signal) pass else: return (None, None) signal_dict = queued_signal.get('signal') sender_dict = queued_signal.get('sender') regkey, name = signal_dict.items()[0] sender = None # specifying a sender is optional. if sender_dict is not None: try: sender = cache.get_model(str(sender_dict['app_label']), str(sender_dict['modl_name'])) except (KeyError, AttributeError), err: logg.info("*** Error deserializing sender_dict: %s" % err) sender = None
def register(signal, name, regkey=None): if regkey is None: if hasattr(signal, '__module__'): regkey = signal.__module__ else: raise SignalRegistryError("Cannot register signal: register() called without a regkey and signal '%s' has no __module__ attribute." % ( signal,)) from signalqueue.dispatcher import AsyncSignal if not isinstance(signal, AsyncSignal): raise SignalRegistryError("Cannot register signal: '%s' is not an instance of AsyncSignal." % ( signal,)) logg.debug("*** Registering signal '%s' %s to '%s'" % (name, signal, regkey)) autodiscover.lock.acquire() try: if not hasattr(signal, 'name'): signal.name = name if not hasattr(signal, 'regkey'): signal.regkey = regkey SQ_DMV[regkey].add(signal) finally: autodiscover.lock.release()
def send(self, sender, **named): from signalqueue.worker import queues self.runmode = int(named.pop("runmode", queues._runmode)) logg.debug("--- send() called, runmode = %s" % self.runmode) if self.runmode: if self.runmode == runmodes["SQ_ASYNC_REQUEST"]: # it's a web request -- enqueue it return self.enqueue(sender, **named) elif self.runmode == runmodes["SQ_ASYNC_DAEMON"]: # signal sent in daemon mode -- enqueue it return self.enqueue(sender, **named) elif self.runmode == runmodes["SQ_ASYNC_MGMT"]: # signal sent in command mode -- fire away return self.send_now(sender, **named) elif self.runmode == runmodes["SQ_SYNC"]: # fire normally return self.send_now(sender, **named) else: # unknown runmode value -- fire normally logg.info("*** send() called with an unknown runmode: '%s' -- firing sync signal." % self.runmode) return self.send_now(sender, **named) else: # fire normally logg.info("*** send() called and no runmode configured -- firing sync signal.") return self.send_now(sender, **named)
def __init__(self, connections_info, runmode): logg.debug("*** Initializing a ConnectionHandler with %s queues running in mode %s" % ( len(connections_info), runmode)) self.connections_info = connections_info self._connections = {} self._runmode = runmode self._index = None
def dequeue(self, queued_signal=None): """ Deserialize and execute a signal, either from the queue or as per the contents of the queued_signal kwarg. If queued_signal contains a serialized signal call datastructure,* dequeue() will deserialize and execute that serialized signal without popping the queue. If queued_signal is None, it will call retrieve() to pop the queue for the next signal, which it will execute if one is returned successfully. * See the QueueBase docstring for an example. """ from django.db.models.loading import cache if queued_signal is None: queued_signal = self.retrieve() if queued_signal is not None: logg.debug("Dequeueing signal: %s" % queued_signal) pass else: return (None, None) signal_dict = queued_signal.get('signal') sender_dict = queued_signal.get('sender') regkey, name = signal_dict.items()[0] sender = None # specifying a sender is optional. if sender_dict is not None: try: sender = cache.get_model( str(sender_dict['app_label']), str(sender_dict['modl_name'])) except (KeyError, AttributeError), err: logg.info("*** Error deserializing sender_dict: %s" % err) sender = None
def autodiscover(): """ Auto-discover signals.py modules in the apps in INSTALLED_APPS; and fail silently when not present. N.B. this autdiscover() implementation is based on dajaxice_autodiscover in the Dajaxice module: https://github.com/jorgebastida/django-dajaxice/blob/master/dajaxice/core/Dajaxice.py#L155 ... which in turn was inspired/copied from django.contrib.admin.autodiscover(). One key modification is our use of threading.Lock instead of the global state variables used by Dajaxice. """ autodiscover.lock.acquire() try: import imp from django.conf import settings from signalqueue.dispatcher import AsyncSignal from signalqueue.utils import logg # Gather signals that any of the installed apps define in # their respective signals.py files: logg.debug("*** Registering signals in %s installed apps ..." % (len(settings.INSTALLED_APPS), )) from signalqueue.utils import import_module for appstring in settings.INSTALLED_APPS: try: app = import_module(appstring) except AttributeError, ae: logg.debug(str(ae)) continue try: imp.find_module('signals', app.__path__) except ImportError, ie: logg.debug(str(app.__name__) + " " + str(ie)) continue modstring = "%s.signals" % appstring logg.debug("*** Import signals in '%s' ..." % ((modstring, ))) mod = import_module(modstring) logg.debug("*** Searching for signals in '%s' ..." % ((modstring, ))) for name, thing in mod.__dict__.items(): if isinstance(thing, AsyncSignal): logg.debug("*** Registering %s: %s.%s ..." % (thing.__class__.__name__, modstring, name)) register(thing, name, modstring)
mod = import_module(modstring) logg.debug("*** Searching for signals in '%s' ..." % ((modstring, ))) for name, thing in mod.__dict__.items(): if isinstance(thing, AsyncSignal): logg.debug("*** Registering %s: %s.%s ..." % (thing.__class__.__name__, modstring, name)) register(thing, name, modstring) if hasattr(settings, "SQ_ADDITIONAL_SIGNALS"): if isinstance(settings.SQ_ADDITIONAL_SIGNALS, (list, tuple)): logg.debug( "*** Registering signals from %s SQ_ADDITIONAL_SIGNALS modules ..." % (len(settings.SQ_ADDITIONAL_SIGNALS), )) for addendumstring in settings.SQ_ADDITIONAL_SIGNALS: try: addendum = import_module(addendumstring) except AttributeError, err: # TODO: log this in a reliably sane manner logg.warning( "--- SQ_ADDITIONAL_SIGNALS module '%s' import failure: %s" % (addendumstring, err)) continue logg.debug("*** Searching for signals in '%s' ..." % ((addendumstring, )))
def autodiscover(): """ Auto-discover signals.py modules in the apps in INSTALLED_APPS; and fail silently when not present. N.B. this autdiscover() implementation is based on dajaxice_autodiscover in the Dajaxice module: https://github.com/jorgebastida/django-dajaxice/blob/master/dajaxice/core/Dajaxice.py#L155 ... which in turn was inspired/copied from django.contrib.admin.autodiscover(). One key modification is our use of threading.Lock instead of the global state variables used by Dajaxice. """ autodiscover.lock.acquire() try: import imp from django.conf import settings from signalqueue.dispatcher import AsyncSignal # Gather signals that any of the installed apps define in # their respective signals.py files: logg.debug("*** Looking for AsyncSignal instances in %s apps..." % len(settings.INSTALLED_APPS)) for appstring in settings.INSTALLED_APPS: try: app = import_module(appstring) except AttributeError: continue try: imp.find_module('signals', app.__path__) except ImportError: continue modstring = "%s.signals" % appstring mod = import_module(modstring) for name, thing in mod.__dict__.items(): if isinstance(thing, AsyncSignal): logg.debug("*** Registering signal to %s: %s" % (modstring, thing)) thing.name = name thing.regkey = modstring SQ_DMV[modstring].add(thing) if hasattr(settings, "SQ_ADDITIONAL_SIGNALS"): if isinstance(settings.SQ_ADDITIONAL_SIGNALS, (list, tuple)): logg.debug("*** Registering additional signals from module: %s" % str(settings.SQ_ADDITIONAL_SIGNALS)) for addendumstring in settings.SQ_ADDITIONAL_SIGNALS: try: addendum = import_module(addendumstring) except AttributeError, err: # TODO: log this in a reliably sane manner logg.warning("xxx Got AttributeError when loading an additional signal module: %s" % err) for name, thing in addendum.__dict__.items(): if isinstance(thing, AsyncSignal): logg.debug("*** Adding additional signal to %s: %s" % (addendumstring, thing)) thing.name = name thing.regkey = addendumstring SQ_DMV[addendumstring].add(thing) finally: autodiscover.lock.release()
def autodiscover(): """ Auto-discover signals.py modules in the apps in INSTALLED_APPS; and fail silently when not present. N.B. this autdiscover() implementation is based on dajaxice_autodiscover in the Dajaxice module: https://github.com/jorgebastida/django-dajaxice/blob/master/dajaxice/core/Dajaxice.py#L155 ... which in turn was inspired/copied from django.contrib.admin.autodiscover(). One key modification is our use of threading.Lock instead of the global state variables used by Dajaxice. """ autodiscover.lock.acquire() try: import imp from django.conf import settings from signalqueue.dispatcher import AsyncSignal from signalqueue.utils import logg # Gather signals that any of the installed apps define in # their respective signals.py files: logg.debug("*** Registering signals in %s installed apps ..." % ( len(settings.INSTALLED_APPS),)) from signalqueue.utils import import_module for appstring in settings.INSTALLED_APPS: try: app = import_module(appstring) except AttributeError, ae: logg.debug(str(ae)) continue try: imp.find_module('signals', app.__path__) except ImportError, ie: logg.debug(str(app.__name__) +" "+ str(ie)) continue modstring = "%s.signals" % appstring logg.debug("*** Import signals in '%s' ..." % ( (modstring,))) mod = import_module(modstring) logg.debug("*** Searching for signals in '%s' ..." % ( (modstring,))) for name, thing in mod.__dict__.items(): if isinstance(thing, AsyncSignal): logg.debug("*** Registering %s: %s.%s ..." % ( thing.__class__.__name__, modstring, name)) register(thing, name, modstring)
(modstring,))) mod = import_module(modstring) logg.debug("*** Searching for signals in '%s' ..." % ( (modstring,))) for name, thing in mod.__dict__.items(): if isinstance(thing, AsyncSignal): logg.debug("*** Registering %s: %s.%s ..." % ( thing.__class__.__name__, modstring, name)) register(thing, name, modstring) if hasattr(settings, "SQ_ADDITIONAL_SIGNALS"): if isinstance(settings.SQ_ADDITIONAL_SIGNALS, (list, tuple)): logg.debug("*** Registering signals from %s SQ_ADDITIONAL_SIGNALS modules ..." % ( len(settings.SQ_ADDITIONAL_SIGNALS),)) for addendumstring in settings.SQ_ADDITIONAL_SIGNALS: try: addendum = import_module(addendumstring) except AttributeError, err: # TODO: log this in a reliably sane manner logg.warning("--- SQ_ADDITIONAL_SIGNALS module '%s' import failure: %s" % ( addendumstring, err)) continue logg.debug("*** Searching for signals in '%s' ..." % ( (addendumstring,))) for name, thing in addendum.__dict__.items():
def autodiscover(): """ Auto-discover signals.py modules in the apps in INSTALLED_APPS; and fail silently when not present. N.B. this autdiscover() implementation is based on dajaxice_autodiscover in the Dajaxice module: https://github.com/jorgebastida/django-dajaxice/blob/master/dajaxice/core/Dajaxice.py#L155 ... which in turn was inspired/copied from django.contrib.admin.autodiscover(). One key modification is our use of threading.Lock instead of the global state variables used by Dajaxice. """ autodiscover.lock.acquire() try: import imp from django.conf import settings from signalqueue.dispatcher import AsyncSignal from signalqueue.utils import logg # Gather signals that any of the installed apps define in # their respective signals.py files: logg.debug("*** Registering signals in %s installed apps ..." % ( len(settings.INSTALLED_APPS),)) from signalqueue.utils import import_module for appstring in settings.INSTALLED_APPS: try: app = import_module(appstring) except AttributeError: continue try: imp.find_module('signals', app.__path__) except ImportError: continue modstring = "%s.signals" % appstring mod = import_module(modstring) logg.debug("*** Searching for signals in '%s' ..." % ( (modstring,))) for name, thing in mod.__dict__.items(): if isinstance(thing, AsyncSignal): logg.debug("*** Registering %s: %s.%s ..." % ( thing.__class__.__name__, modstring, name)) register(thing, name, modstring) if hasattr(settings, "SQ_ADDITIONAL_SIGNALS"): if isinstance(settings.SQ_ADDITIONAL_SIGNALS, (list, tuple)): logg.debug("*** Registering signals from %s SQ_ADDITIONAL_SIGNALS modules ..." % ( len(settings.SQ_ADDITIONAL_SIGNALS),)) for addendumstring in settings.SQ_ADDITIONAL_SIGNALS: try: addendum = import_module(addendumstring) except AttributeError, err: # TODO: log this in a reliably sane manner logg.warning("--- SQ_ADDITIONAL_SIGNALS module '%s' import failure: %s" % ( addendumstring, err)) continue logg.debug("*** Searching for signals in '%s' ..." % ( (addendumstring,))) for name, thing in addendum.__dict__.items(): if isinstance(thing, AsyncSignal): logg.debug("*** Registering %s: %s.%s ..." % ( thing.__class__.__name__, addendumstring, name)) register(thing, name, addendumstring) finally: autodiscover.lock.release()
def push(self, value): logg.debug("*** push() value: %s" % value) self.get_or_create(queue_name=self.queue_name, value=value, enqueued=True)