Adapt mod_jsonrpc to the new asyncio approach.

This commit is contained in:
adator
2025-11-20 14:04:19 +01:00
parent 51f709e4a1
commit 5b7c2e83d1
3 changed files with 143 additions and 126 deletions

View File

@@ -4,16 +4,23 @@ from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mods.jsonrpc.mod_jsonrpc import Jsonrpc
def thread_subscribe(uplink: 'Jsonrpc') -> None:
async def thread_subscribe(uplink: 'Jsonrpc') -> None:
snickname = uplink.ctx.Config.SERVICE_NICKNAME
schannel = uplink.ctx.Config.SERVICE_CHANLOG
if uplink.is_streaming:
await uplink.ctx.Irc.Protocol.send_priv_msg(nick_from=snickname,
msg=f"[{uplink.ctx.Config.COLORS.green}JSONRPC INFO{uplink.ctx.Config.COLORS.nogc}] IRCd Json-rpc already connected!",
channel=schannel
)
return None
snickname = uplink.Config.SERVICE_NICKNAME
schannel = uplink.Config.SERVICE_CHANLOG
uplink.is_streaming = True
response = asyncio.run(uplink.LiveRpc.subscribe(["all"]))
response = await uplink.LiveRpc.subscribe(["all"])
if response.error.code != 0:
uplink.Protocol.send_priv_msg(nick_from=snickname,
msg=f"[{uplink.Config.COLORS.red}JSONRPC ERROR{uplink.Config.COLORS.nogc}] {response.error.message}",
await uplink.ctx.Irc.Protocol.send_priv_msg(nick_from=snickname,
msg=f"[{uplink.ctx.Config.COLORS.red}JSONRPC ERROR{uplink.ctx.Config.COLORS.nogc}] {response.error.message}",
channel=schannel
)
@@ -21,33 +28,41 @@ def thread_subscribe(uplink: 'Jsonrpc') -> None:
message = response.error.message
if code == 0:
uplink.Protocol.send_priv_msg(
await uplink.ctx.Irc.Protocol.send_priv_msg(
nick_from=snickname,
msg=f"[{uplink.Config.COLORS.green}JSONRPC{uplink.Config.COLORS.nogc}] Stream is OFF",
msg=f"[{uplink.ctx.Config.COLORS.green}JSONRPC{uplink.ctx.Config.COLORS.nogc}] Stream is OFF",
channel=schannel
)
uplink.is_streaming = False
else:
uplink.Protocol.send_priv_msg(
await uplink.ctx.Irc.Protocol.send_priv_msg(
nick_from=snickname,
msg=f"[{uplink.Config.COLORS.red}JSONRPC{uplink.Config.COLORS.nogc}] Stream has crashed! {code} - {message}",
msg=f"[{uplink.ctx.Config.COLORS.red}JSONRPC{uplink.ctx.Config.COLORS.nogc}] Stream has crashed! {code} - {message}",
channel=schannel
)
uplink.is_streaming = False
def thread_unsubscribe(uplink: 'Jsonrpc') -> None:
async def thread_unsubscribe(uplink: 'Jsonrpc') -> None:
response = asyncio.run(uplink.LiveRpc.unsubscribe())
uplink.Logs.debug("[JSONRPC UNLOAD] Unsubscribe from the stream!")
snickname = uplink.ctx.Config.SERVICE_NICKNAME
schannel = uplink.ctx.Config.SERVICE_CHANLOG
if not uplink.is_streaming:
await uplink.ctx.Irc.Protocol.send_priv_msg(nick_from=snickname,
msg=f"[{uplink.ctx.Config.COLORS.green}JSONRPC INFO{uplink.ctx.Config.COLORS.nogc}] IRCd Json-rpc is already off!",
channel=schannel
)
return None
response = await uplink.LiveRpc.unsubscribe()
uplink.ctx.Logs.debug("[JSONRPC UNLOAD] Unsubscribe from the stream!")
uplink.is_streaming = False
uplink.update_configuration('jsonrpc', 0)
snickname = uplink.Config.SERVICE_NICKNAME
schannel = uplink.Config.SERVICE_CHANLOG
code = response.error.code
message = response.error.message
if code != 0:
uplink.Protocol.send_priv_msg(
await uplink.ctx.Irc.Protocol.send_priv_msg(
nick_from=snickname,
msg=f"[{uplink.Config.COLORS.red}JSONRPC ERROR{uplink.Config.COLORS.nogc}] {message} ({code})",
msg=f"[{uplink.ctx.Config.COLORS.red}JSONRPC ERROR{uplink.ctx.Config.COLORS.nogc}] {message} ({code})",
channel=schannel
)