class HireFireMiddleware(object): """ The Django middleware that is hardwired to the URL paths HireFire requires. Implements the test response and the json response that contains the procs data. """ test_path = re.compile(r'^/hirefire/test/?$') info_path = re.compile(r'^/hirefire/%s/info/?$' % re.escape(TOKEN)) loaded_procs = load_procs(*PROCS) def test(self, request): """ Doesn't do much except telling the HireFire bot it's installed. """ return HttpResponse(HIREFIRE_FOUND) def info(self, request): """ The heart of the app, returning a JSON ecoded list of proc results. """ payload = dump_procs(self.loaded_procs) return HttpResponse(payload, content_type='application/json') def process_request(self, request): path = request.path if self.test_path.match(path): return self.test(request) elif self.info_path.match(path): return self.info(request)
class HireFireMiddleware(MiddlewareMixin): """ The Django middleware that is hardwired to the URL paths HireFire requires. Implements the test response and the json response that contains the procs data. """ test_path = re.compile(r'^/hirefire/test/?$') info_path = re.compile(r'^/hirefire/%s/info/?$' % re.escape(TOKEN)) loaded_procs = load_procs(*PROCS) def test(self, request): """ Doesn't do much except telling the HireFire bot it's installed. """ return HttpResponse(HIREFIRE_FOUND) def info(self, request): """ Return JSON response serializing all proc names and quantities. """ data = serialize_procs( self.loaded_procs, use_concurrency=USE_CONCURRENCY, serializer_class=DjangoProcSerializer, ) return JsonResponse(data=data, safe=False) def process_request(self, request): path = request.path if self.test_path.match(path): return self.test(request) elif self.info_path.match(path): return self.info(request)
def build_hirefire_blueprint(token, procs): """ The Flask middleware provided as a Blueprint exposing the the URL paths HireFire requires. Implements the test response and the json response that contains the procs data. """ if not procs: raise RuntimeError('At least one proc should be passed') loaded_procs = load_procs(*procs) bp = Blueprint(__name__, 'hirefire') @bp.route('/hirefire/test') def test(): """ Doesn't do much except telling the HireFire bot it's installed. """ return HIREFIRE_FOUND @bp.route('/hirefire/<id>/info') def info(id): """ The heart of the app, returning a JSON ecoded list of proc results. """ return Response(dump_procs(loaded_procs), mimetype='application/json') return bp
def test_can_count_queues_properly(self): try: loaded_procs.clear() # Put some jobs on the queue self._add_jobs_to_queue('high', 2) self._add_jobs_to_queue('bottom', 4) # Now fake a job being active for one of them for idx, queue_name in enumerate(['high', 'bottom']): queue = Queue(queue_name, connection=FakeStrictRedis()) registry = StartedJobRegistry(queue_name, queue.connection) # Passing in a negative score is important here, otherwise the job will be recognized as expired registry.connection.zadd(registry.key, -1, 'job_id_{}'.format(idx)) # Load the HF procs procs = load_procs(*( 'tests.contrib.django.testapp.rq_test_procs.WorkerProc', 'tests.contrib.django.testapp.rq_test_procs.AnotherWorkerProc' )) # Total should be all queued + 1 active for each assert sum([proc.quantity() for proc_name, proc in procs.items()]) == 8 finally: loaded_procs.clear()
def hirefire_handlers(token, procs): if not procs: raise Exception('The HireFire Tornado handler ' 'requires at least one proc defined.') test_path = r'^/hirefire/test/?$' info_path = r'^/hirefire/%s/info/?$' % re.escape(token) HireFireInfoHandler.loaded_procs = load_procs(*procs) handlers = [(test_path, HireFireTestHandler), (info_path, HireFireInfoHandler)] return handlers
def hirefire_handlers(token, procs): if not procs: raise Exception('The HireFire Tornado handler ' 'requires at least one proc defined.') test_path = r'^/hirefire/test/?$' info_path = r'^/hirefire/%s/info/?$' % re.escape(token) HireFireInfoHandler.loaded_procs = load_procs(*procs) handlers = [ (test_path, HireFireTestHandler), (info_path, HireFireInfoHandler) ] return handlers
def hirefire_handlers(procs_list): if not procs: raise Exception('The HireFire Tornado handler ' 'requires at least one proc defined.') global PROCS global loaded_procs PROCS = procs_list loaded_procs = procs.load_procs(*PROCS) test_path = r'^/hirefire/test/?$' info_path = r'^/hirefire/%s/info/?$' % re.escape(TOKEN) handlers = [(test_path, HireFireTestHandler), (info_path, HireFireInfoHandler)] return handlers
def hirefire_handlers(procs_list): if not procs: raise Exception('The HireFire Tornado handler ' 'requires at least one proc defined.') global PROCS global loaded_procs PROCS = procs_list loaded_procs = procs.load_procs(*PROCS) test_path = r'^/hirefire/test/?$' info_path = r'^/hirefire/%s/info/?$' % re.escape(TOKEN) handlers = [ (test_path, HireFireTestHandler), (info_path, HireFireInfoHandler) ] return handlers
class HireFireMiddleware(object): """ The Django middleware that is hardwired to the URL paths HireFire requires. Implements the test response and the json response that contains the procs data. """ test_path = re.compile(r'^/hirefire/test/?$') info_path = re.compile(r'^/hirefire/%s/info/?$' % re.escape(TOKEN)) loaded_procs = procs.load_procs(*PROCS) def test(self, request): """ Doesn't do much except telling the HireFire bot it's installed. """ return HttpResponse('HireFire Middleware Found!') def info(self, request): """ The heart of the app, returning a JSON ecoded list of proc results. """ data = [] for name, proc in self.loaded_procs.items(): data.append({ 'name': name, 'quantity': proc.quantity() or 'null', }) payload = json.dumps(data, cls=TimeAwareJSONEncoder, ensure_ascii=False) return HttpResponse(payload, content_type='application/json') def process_request(self, request): path = request.path if self.test_path.match(path): return self.test(request) elif self.info_path.match(path): return self.info(request)