def on_event(cls, change): doc = change["fullDocument"] channel = doc.get("channel", None) data = json_encode(doc) for waiter, interest in cls.waiters.items(): if channel in interest: waiter.write_message(data)
def on_event(cls, change): doc = change["fullDocument"] channel = doc.get("channel", None) author = doc.get("author", None) data = json_encode(doc) for waiter, interest in cls.waiters.items(): if ((channel == core4.const.EVENT_CHANNEL) or (author != waiter.current_user)): if channel in interest: waiter.write_message(data)
async def sse(self, event, doc): js = json_encode(doc, indent=None, separators=(',', ':')) try: self.write("event: " + event + "\n") self.write("data: " + js + "\n\n") await self.flush() except StreamClosedError: self.logger.info("stream closed") return True except Exception: self.logger.error("stream error", exc_info=True) return True return False
async def on_queue(cls, change): data = { "created": core4.util.node.mongo_now(), "name": "summary", "author": core4.util.node.get_username(), "channel": core4.const.QUEUE_CHANNEL, } for waiter, interest in cls.waiters.items(): if core4.const.QUEUE_CHANNEL in interest: data["data"] = [] for line in change: qn = line["name"] if not await waiter.user.has_job_access(qn): line["name"] = "UnauthorizedJob" data["data"].append(line) if data["data"] != waiter.last: js = json_encode(data) waiter.write_message(js) waiter.last = data["data"]
async def get(self, _id=None): """ Only jobs with execute access permissions granted to the current user can be streamed. Methods: GET /jobs/poll/<_id> - stream job attributes Parameters: _id (str): job _id Returns: JSON stream with job attributes Raises: 401 Bad Request: failed to parse job _id 401 Unauthorized 403 Forbidden 404 cannot instantiate job Examples: >>> from requests import post, get >>> import json >>> rv = post(url + "/enqueue?name=" + name, headers=h, json={"sleep": 20}) >>> _id = rv.json()["data"]["_id"] >>> rv = get(url + "/jobs/poll/" + _id, headers=h, stream=True) >>> for line in rv.iter_lines(): >>> if line: >>> data = json.loads(line.decode("utf-8")) >>> locked = data.get("locked") >>> state = data.get("state") >>> if locked: >>> print(locked["progress_value"], state) 0.00045184999999996477 running 0.2524361 running 0.5028721 running 0.75340995 running """ if _id == "" or _id is None: raise HTTPError(400, "failed to parse job _id: [{}]".format(_id)) self.set_header('content-type', 'text/event-stream') self.set_header('cache-control', 'no-cache') oid = self.parse_id(_id) last = None exit = False while not exit: doc = await self.get_detail(oid) if doc["state"] in STATE_FINAL: exit = True self.finish(doc) elif last is None or doc != last: last = doc js = json_encode(doc, indent=None, separators=(',', ':')) try: self.write(js + "\n\n") self.logger.info("serving [%s] with [%d] byte", self.current_user, len(js)) await self.flush() except StreamClosedError: self.logger.info("stream closed") exit = True except Exception: self.logger.error("stream error", exc_info=True) exit = True await gen.sleep(1.)