PK!l4flask_dramatiq.pyfrom importlib import import_module from threading import local from warnings import warn import click from dramatiq import ( Middleware, actor as register_actor, set_broker, ) from dramatiq.cli import ( CPUS, main as dramatiq_worker, make_argument_parser as dramatiq_argument_parser, ) from flask import current_app from flask.cli import with_appcontext def import_object(path): # Implement setuptools entrypoint-like loading of object. modname, objname = path.split(':') mod = import_module(modname) return getattr(mod, objname) class AppContextMiddleware(Middleware): # Setup Flask app for actor. Borrowed from # https://github.com/Bogdanp/flask_dramatiq_example. state = local() def __init__(self, app): self.app = app def before_process_message(self, broker, message): context = self.app.app_context() context.push() self.state.context = context def after_process_message( self, broker, message, *, result=None, exception=None): try: context = self.state.context context.pop(exception) del self.state.context except AttributeError: pass after_skip_message = after_process_message class Dramatiq: # The Flask extension. # Reuse same defaults as dramatiq. cf. # https://github.com/Bogdanp/dramatiq/blob/master/dramatiq/broker.py#L34-L44 DEFAULT_BROKER = 'dramatiq.brokers.rabbitmq:RabbitmqBroker' def __init__(self, app=None, broker_cls=DEFAULT_BROKER, name='dramatiq', config_prefix=None): self.actors = [] self.app = app self.broker_cls = broker_cls self.config_prefix = config_prefix or name.upper() + '_BROKER' self.name = name if app: self.init_app(app) def __repr__(self): return '<%s %s>' % (self.__class__.__name__, self.name) def init_app(self, app): if self.app is not None: warn( "%s is used by more than one flask application. " "Actor's context may be set incorrectly." % (self,), stacklevel=2, ) self.app = app app.extensions['dramatiq-' + self.name] = self app.config.setdefault(self.config_prefix, self.broker_cls) cls = app.config[self.config_prefix] if isinstance(cls, str): cls = import_object(cls) self.broker = cls(url=app.config.get(self.config_prefix + '_URL')) self.broker.add_middleware(AppContextMiddleware(app)) for actor in self.actors: actor.register(broker=self.broker) def actor(self, fn=None, **kw): # Substitude dramatiq.actor decorator to return a lazy wrapper. This # allow to register actors in extension before the broker is # effectively configured by init_app. def decorator(fn): lazy_actor = LazyActor(self, fn, kw) self.actors.append(lazy_actor) if self.app: lazy_actor.register() return lazy_actor if fn: return decorator(fn) return decorator class LazyActor(object): # Intermediate object that register actor on broker an call. def __init__(self, extension, fn, kw): self.extension = extension self.fn = fn self.kw = kw self.actor = None def __call__(self, *a, **kw): return self.fn(*a, **kw) def __getattr__(self, name): if not self.actor: raise AttributeError(name) return getattr(self.actor, name) def register(self, broker): self.actor = register_actor(broker=broker, **self.kw)(self.fn) # Next is regular actor API. def send(self, *a, **kw): return self.actor.send(*a, **kw) def send_with_options(self, *a, **kw): return self.actor.send_with_options(*a, **kw) @click.command() @click.option('-p', '--processes', default=CPUS, metavar='PROCESSES', show_default=True, help="the number of worker processes to run") @click.option('-t', '--threads', default=8, metavar='THREADS', show_default=True, help="the number of worker treads per processes") @click.option('-Q', '--queues', type=str, default=None, metavar='QUEUES', show_default=True, help="listen to a subset of queues, comma separated") @click.argument('broker_name', default='dramatiq') @with_appcontext def worker(processes, threads, queues, broker_name): """Run dramatiq workers. Setup Dramatiq with broker and task modules from Flask app. \b examples: # Run dramatiq with 1 thread per process. $ flask worker --threads 1 \b # Listen only to the "foo" and "bar" queues. $ flask worker -Q foo,bar \b # Consuming from a specific broker $ flask worker mybroker """ # Plugin for flask.commands entrypoint. # # Wraps dramatiq worker CLI in a Flask command. This is private API of # dramatiq. parser = dramatiq_argument_parser() # Set worker broker globally. needle = 'dramatiq-' + broker_name set_broker(current_app.extensions[needle].broker) command = [ "--processes", str(processes), "--threads", str(threads), # This module does not have broker local. Thus dramatiq fallbacks to # global broker. __name__, ] if queues: command += ["--queues"] + queues.split(",") args = parser.parse_args(command) dramatiq_worker(args) PK!H##H%)//flask_dramatiq-0.3.0.dist-info/entry_points.txtNI,KMK)*/N- ǧ%&dZADPK!M&flask_dramatiq-0.3.0.dist-info/LICENSECopyright 2019 Étienne BERSAC Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.PK!HnHTU$flask_dramatiq-0.3.0.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!Hat%K 'flask_dramatiq-0.3.0.dist-info/METADATAVnF}I L24ޒuq(yEMMr虡e>C? 7YrzeeLlŐ晴WAbd.}p?|*Ϋ?8ca|Z! ~5_FjymU)~ÉGF'S,*WEJgH+l7C~ceݐAѳ𻽗çq66CtpU1:uлJeRЁnL\&gahe[DF.Q:6Qce:`Q²HPE efy f3**tyy#Tz@I5Du|Ա֞LZ.B!<(7,]k!p*œ5F-wm[#؞٤,0=*2vhKmLQ)8:;mmy:v y]hlN'| qJfQ |wy9Ubf4bib=߻4/ Tq_R|% U&;<`MVkir8xYOCC+FiH!3S.ah/sl8mdL.gSOA+`:L6KUW^(A/T5kW]Ȣ粭iͷcҵXo0n*Er _A1ʱ=k 8"Pre@d! [rez`-EWnq4ձBHsm*. nު&$/z(I DskC 7l 4YہԞ^]Z ev:bNa 6/X?|?Xdʲƽֆ0c |ISUɦy5h< ,ΖֻMk<f\'UY+aN~cG^Fګ &G.ʼn mBlȂ /'|[Ӄ߆ H;fUmlK̏7E0C&(M{轨_OJPK!HG9A.%flask_dramatiq-0.3.0.dist-info/RECORDлv0ϒPA:D B8\$`C'^{:4I6kʑ T䯣|&?V" RJ YD*1Y*MH(IDzaݕٔƁ_I r@^Yjnޯ(mmEqk7uS=zTt;'