Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
class Consumer(web.ApiView):
def get(self, request, app, name, queue=None, nowait=False):
return nodes.consuming_from(name)
def put(self, request, app, name, queue, nowait=False):
return self.Created(nodes.add_consumer(name, queue, nowait=nowait))
post = put
def delete(self, request, app, name, queue, nowait=False):
return self.Ok(nodes.cancel_consumer(name, queue, nowait=nowait))
class Queue(web.ApiView):
def get(self, request, app, name=None):
return queues.get(name) if name else queues.all()
def delete(self, request, app, name, nowait=False):
return self.Ok(queues.delete(name))
def put(self, request, app, name, nowait=False):
return self.Created(queues.add(name, nowait=nowait,
**self.params("exchange", "exchange_type",
"routing_key", "options")))
post = put
class apply(web.ApiView):
get_methods = frozenset(["GET", "HEAD"])
class Queue(web.ApiView):
def get(self, request, app, name=None):
return queues.get(name) if name else queues.all()
def delete(self, request, app, name, nowait=False):
return self.Ok(queues.delete(name))
def put(self, request, app, name, nowait=False):
return self.Created(queues.add(name, nowait=nowait,
**self.params("exchange", "exchange_type",
"routing_key", "options")))
post = put
class apply(web.ApiView):
get_methods = frozenset(["GET", "HEAD"])
re_find_queue = re.compile(r'/?(.+?)/?$')
re_url_in_path = re.compile(r'(.+?/)(\w+://)(.+)')
def prepare_path(self, rest):
path, url = self._parse_path_containing_url(rest)
if path:
m = self.re_find_queue.match(path)
if m:
return m.groups()[0], url
return None, url
def dispatch(self, request, app, rest):
gd = lambda m: getattr(request, m)
queue, url = self.prepare_path(rest)
app = apps.get(app)
def dispatch(self, request, *args, **kwargs):
self.nowait = kwargs.get("nowait", False)
if request.method.lower() == "get":
kwargs.pop("nowait", None)
if self.nowait:
return self.NotImplemented("Operation can't be async.")
try:
data = super(ApiView, self).dispatch(request, *args, **kwargs)
except NoRouteError:
return HttpResponseNotFound()
except NoReplyError:
return HttpResponseTimeout()
except Exception, exc:
return Error({"nok": [safe_repr(exc),
"".join(format_exception(*sys.exc_info()))]})
return self.Response(data)
return self.Accepted({"uuid": result.task_id, "url": url,
"queue": queue, "method": method,
"params": params, "data": data,
"broker": producer.connection.as_uri()})
def _parse_path_containing_url(self, rest):
m = self.re_url_in_path.match(rest)
if m:
first, scheme, last = m.groups()
if scheme:
return first, scheme + last
return first, None
return rest, None
class autoscale(web.ApiView):
def get(self, request, app, name):
node = nodes.get(name)
return {"max": node["max_concurrency"], "min": node["min_concurrency"]}
def post(self, request, app, name, nowait=False):
return self.Ok(nodes.autoscale(name, nowait=nowait,
**self.params(("max", int),
("min", int))))
@web.simple_get
def instance_stats(self, request, app, name):
return nodes.stats(name)
def simple_get(fun):
return type(fun.__name__, (ApiView, ), {"get": fun,
"__module__": fun.__module__, "__doc__": fun.__doc__})
class App(web.ApiView):
def get(self, request, app=None):
return apps.get(app).as_dict() if app else apps.all()
def put(self, request, app=None):
return self.Created(apps.add(app or uuid(), **self.params("broker")))
post = put
def delete(self, request, app):
return apps.delete(app)
class Instance(web.ApiView):
def get(self, request, app, name=None, nowait=False):
return nodes.get(name) if name else nodes.all(app=app)
def delete(self, request, app, name, nowait=False):
return self.Ok(nodes.remove(name, nowait=nowait))
def put(self, request, app, name=None, nowait=False):
return self.Created(nodes.add(name=name, app=app,
nowait=nowait,
**self.params("broker", "pool")))
post = put
class Consumer(web.ApiView):
class Instance(web.ApiView):
def get(self, request, app, name=None, nowait=False):
return nodes.get(name) if name else nodes.all(app=app)
def delete(self, request, app, name, nowait=False):
return self.Ok(nodes.remove(name, nowait=nowait))
def put(self, request, app, name=None, nowait=False):
return self.Created(nodes.add(name=name, app=app,
nowait=nowait,
**self.params("broker", "pool")))
post = put
class Consumer(web.ApiView):
def get(self, request, app, name, queue=None, nowait=False):
return nodes.consuming_from(name)
def put(self, request, app, name, queue, nowait=False):
return self.Created(nodes.add_consumer(name, queue, nowait=nowait))
post = put
def delete(self, request, app, name, queue, nowait=False):
return self.Ok(nodes.cancel_consumer(name, queue, nowait=nowait))
class Queue(web.ApiView):
def get(self, request, app, name=None):
return queues.get(name) if name else queues.all()
from __future__ import absolute_import
import re
from celery import current_app as celery
from celery.result import AsyncResult
from cl.pools import producers
from . import web
from .controller import apps, nodes, queues
from .tasks import webhook
from .utils import uuid
class App(web.ApiView):
def get(self, request, app=None):
return apps.get(app).as_dict() if app else apps.all()
def put(self, request, app=None):
return self.Created(apps.add(app or uuid(), **self.params("broker")))
post = put
def delete(self, request, app):
return apps.delete(app)
class Instance(web.ApiView):
def get(self, request, app, name=None, nowait=False):
return nodes.get(name) if name else nodes.all(app=app)