Skip to content

Commit

Permalink
fix websocket subscription jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
nlef committed Jan 3, 2025
1 parent d21e53c commit d7aabc2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 26 deletions.
3 changes: 1 addition & 2 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1236,11 +1236,10 @@ def start_bot(bot_token, socks):

ws_helper = WebSocketHelper(configWrap, klippy, notifier, timelapse, a_scheduler, rotating_handler)

a_scheduler.start()

loop = asyncio.get_event_loop()
loop.create_task(ws_helper.run_forever_async())

a_scheduler.start()
a_scheduler.add_job(
greeting_message,
kwargs={"bot": bot_updater.bot},
Expand Down
2 changes: 1 addition & 1 deletion bot/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def _schedule_notification(self, message: str = "", schedule: bool = False, fini
self._sched.add_job(
self._notify,
kwargs={"message": mess, "silent": self._silent_progress, "group_only": self._group_only, "finish": finish},
misfire_grace_time=None,
misfire_grace_time=180,
coalesce=False,
max_instances=6,
replace_existing=False,
Expand Down
26 changes: 4 additions & 22 deletions bot/websocket_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,27 +338,15 @@ async def websocket_to_message(self, ws_message):
self._scheduler.remove_job("ws_reschedule")
elif klippy_state in ["error", "shutdown", "startup"]:
await self._klippy.set_connected(False)
self._scheduler.add_job(
self.reshedule,
"interval",
seconds=2,
id="ws_reschedule",
replace_existing=True,
)
self._scheduler.add_job(self.reshedule, "interval", seconds=2, id="ws_reschedule", replace_existing=True, coalesce=True, misfire_grace_time=10)
state_message = message_result["state_message"]
if self._klippy.state_message != state_message and klippy_state != "startup":
self._klippy.state_message = state_message
self._notifier.send_error(f"Klippy changed state to {self._klippy.state}\n{self._klippy.state_message}", logs_upload=True)
else:
logger.error("UnKnown klippy state: %s", klippy_state)
await self._klippy.set_connected(False)
self._scheduler.add_job(
self.reshedule,
"interval",
seconds=2,
id="ws_reschedule",
replace_existing=True,
)
self._scheduler.add_job(self.reshedule, "interval", seconds=2, id="ws_reschedule", replace_existing=True, coalesce=True, misfire_grace_time=10)
return

if "devices" in message_result:
Expand All @@ -375,13 +363,7 @@ async def websocket_to_message(self, ws_message):
logger.warning("klippy disconnect detected with message: %s", json_message["method"])
await self.stop_all()
await self._klippy.set_connected(False)
self._scheduler.add_job(
self.reshedule,
"interval",
seconds=2,
id="ws_reschedule",
replace_existing=True,
)
self._scheduler.add_job(self.reshedule, "interval", seconds=2, id="ws_reschedule", replace_existing=True, coalesce=True, misfire_grace_time=10)

if "params" not in json_message:
return
Expand Down Expand Up @@ -446,7 +428,7 @@ async def run_forever_async(self):
):
try:
self._ws = websocket
self._scheduler.add_job(self.reshedule, "interval", seconds=2, id="ws_reschedule", replace_existing=True)
self._scheduler.add_job(self.reshedule, "interval", seconds=2, id="ws_reschedule", replace_existing=True, coalesce=True, misfire_grace_time=10)
# async for message in self._ws:
# await self.websocket_to_message(message)

Expand Down
3 changes: 2 additions & 1 deletion scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
aiolimiter==1.2.1
anyio==4.7.0
APScheduler==3.10.4
emoji==2.14.0
ffmpegcv==0.3.15
idna==3.10
orjson==3.10.13
Pillow==11.1.0 ; python_version>='3.10'
Pillow==10.4.0 ; python_version<='3.9'
psutil==6.1.1
python-telegram-bot[socks,http2,rate-limiter,callback-data,job-queue]==21.9
python-telegram-bot[socks,http2,rate-limiter,callback-data,job-queue]==21.10
tzlocal==2.1
uvloop==0.21.0 ; platform_system != "Windows"
websockets==14.1

0 comments on commit d7aabc2

Please sign in to comment.