Import ECS deployed relay source and historical patches (2026-10-27)
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
"""Optional official SDK event source, private platform domain explicitly configured."""
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
|
||||
import httpx
|
||||
import redis
|
||||
from .config import Settings
|
||||
|
||||
|
||||
def main():
|
||||
import lark_oapi as lark
|
||||
s = Settings(role="longconn")
|
||||
if not s.feishu_enabled:
|
||||
raise RuntimeError("platform is disabled")
|
||||
# The SDK may log websocket URLs, tokens or event bodies. Disable its logging entirely.
|
||||
logging.disable(logging.CRITICAL)
|
||||
r = redis.Redis.from_url(s.redis_url)
|
||||
lease = r.lock("hn:longconn:leader", timeout=30, thread_local=False)
|
||||
if not lease.acquire(blocking=False):
|
||||
raise RuntimeError("another long-connection consumer owns the lease")
|
||||
url = os.environ.get("HN_CONNECTOR_INTERNAL_URL", "http://connector:8000")
|
||||
client = httpx.Client(base_url=url, timeout=10, follow_redirects=False)
|
||||
done = threading.Event()
|
||||
|
||||
def maintain_lease():
|
||||
while not done.wait(8):
|
||||
try:
|
||||
lease.extend(30, replace_ttl=True)
|
||||
except Exception:
|
||||
# Fail closed. Continuing after lease loss could create two consumers.
|
||||
os._exit(2)
|
||||
|
||||
threading.Thread(target=maintain_lease, daemon=True).start()
|
||||
|
||||
def received(event):
|
||||
data = json.loads(lark.JSON.marshal(event))
|
||||
response = client.post("/internal/events", headers={"X-Internal-Secret": s.internal_secret}, json=data)
|
||||
if response.status_code != 200:
|
||||
raise RuntimeError("connector did not accept event")
|
||||
|
||||
handler = lark.EventDispatcherHandler.builder("", "").register_p2_im_message_receive_v1(received).build()
|
||||
try:
|
||||
# domain is an actual supported SDK argument; no public-cloud-domain monkey patch.
|
||||
lark.ws.Client(s.feishu_app_id, s.feishu_app_secret, event_handler=handler,
|
||||
domain=s.feishu_api_base.rstrip("/"), log_level=lark.LogLevel.ERROR).start()
|
||||
finally:
|
||||
done.set()
|
||||
client.close()
|
||||
try:
|
||||
lease.release()
|
||||
except redis.exceptions.LockError:
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user