MCPcopy Create free account
hub / github.com/Roy3838/Observer / _poll

Function _poll

api/remote.py:445–488  ·  view source on GitHub ↗

Long-poll for messages sent from the paired phones/chats of `sessions` (channel -> code), all in one request. Every call also marks each session as active, which is what lets the webhooks tell "queue it" apart from "no Observer session is open", and feeds the dead-man's switch (see

(sessions: dict[Channel, str], deadman: bool | None = None)

Source from the content-addressed store, hash-verified

443
444
445async def _poll(sessions: dict[Channel, str], deadman: bool | None = None) -> dict:
446 """
447 Long-poll for messages sent from the paired phones/chats of `sessions` (channel ->
448 code), all in one request. Every call also marks each session as active, which is
449 what lets the webhooks tell "queue it" apart from "no Observer session is open",
450 and feeds the dead-man's switch (see deadman_sweeper).
451 """
452 r = await get_redis()
453 for channel, code in sessions.items():
454 await r.setex(_alive_key(code, channel), ALIVE_TTL, "1")
455
456 # A disarming poll answers at once: it may be the tab's last one (remote control
457 # being turned off), and it must not take messages it won't stay to handle.
458 if await _deadman_poll(sessions, deadman):
459 return {"messages": []}
460
461 # RPUSH + BLPOP = FIFO, and each message goes to exactly one polling tab. Each BLPOP
462 # waits on every inbox at once, in short slices rather than one long block: redis-py
463 # (>= 8) reads with a 5s socket timeout by default, and a longer block fails.
464 inboxes = {_inbox_key(code, channel): channel for channel, code in sessions.items()}
465 deadline = time.monotonic() + INBOX_WAIT
466 popped = None
467 while popped is None and time.monotonic() < deadline:
468 await _keep_typing(sessions)
469 popped = await r.blpop(list(inboxes), timeout=BLPOP_SLICE)
470 if not popped:
471 return {"messages": []}
472 raw = [(inboxes[popped[0]], popped[1])]
473 for inbox, channel in inboxes.items():
474 raw += [(channel, entry) for entry in await r.lpop(inbox, 20) or []]
475
476 now = time.time()
477 messages = []
478 for channel, entry in raw:
479 item = json.loads(entry)
480 if now - item["ts"] <= INBOX_MAX_AGE:
481 messages.append({"channel": channel, "text": item["text"]})
482 # The tab is about to run it: typing starts now (and on WhatsApp, it shows as read).
483 code = sessions[channel]
484 await _set_busy(code, channel, True)
485 if item.get("id"):
486 await r.set(_last_msg_key(code, channel), item["id"], ex=WA_WINDOW_TTL)
487 await _keep_typing(sessions)
488 return {"messages": messages}
489
490
491async def _status(code: str, channel: Channel) -> dict:

Callers 3

remote_listenFunction · 0.85
remote_inboxFunction · 0.85
remote_telegram_inboxFunction · 0.85

Calls 7

get_redisFunction · 0.90
_alive_keyFunction · 0.85
_deadman_pollFunction · 0.85
_inbox_keyFunction · 0.85
_keep_typingFunction · 0.85
_set_busyFunction · 0.85
_last_msg_keyFunction · 0.85

Tested by

no test coverage detected