Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix blocking I/O in shares Parser #13

Merged
merged 5 commits into from
Sep 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
run msg_parser as async task
  • Loading branch information
chris-mc1 committed Sep 3, 2024
commit 552423534ea970c978821321e09c00502c4117cf
10 changes: 6 additions & 4 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


class UnRAIDServer(object):
def __init__(self, mqtt_config, unraid_config):
def __init__(self, mqtt_config, unraid_config, loop: asyncio.AbstractEventLoop):
# Unraid config
unraid_host = unraid_config.get('host')
unraid_port = unraid_config.get('port')
Expand Down Expand Up @@ -50,6 +50,8 @@ def __init__(self, mqtt_config, unraid_config):
unraid_logger.setFormatter(unraid_logger_formatter)
self.logger.addHandler(unraid_logger)

self.loop = loop

def on_connect(self, client, flags, rc, properties):
self.logger.info('Successfully connected to mqtt server')

Expand Down Expand Up @@ -237,13 +239,13 @@ async def ws_connect(self):
if sub_channel not in self.mqtt_history:
self.logger.info(f'Create config for {sub_channel}')
self.mqtt_history[sub_channel] = (time.time() - self.scan_interval)
await msg_parser(self, msg_data, create_config=True)
self.loop.create_task(msg_parser(self, msg_data, create_config=True))

# Parse content
if self.scan_interval <= (time.time() - self.mqtt_history.get(sub_channel, time.time())):
self.logger.info(f'Parse data for {sub_channel}')
self.mqtt_history[sub_channel] = time.time()
await msg_parser(self, msg_data, create_config=False)
self.loop.create_task(msg_parser(self, msg_data, create_config=False))

except (httpx.ConnectTimeout, httpx.ConnectError):
self.logger.error('Failed to connect to unraid due to a timeout or connection issue...')
Expand Down Expand Up @@ -280,7 +282,7 @@ async def ws_connect(self):

# Create unraid instances
for unraid_config in config.get('unraid'):
UnRAIDServer(config.get('mqtt'), unraid_config)
UnRAIDServer(config.get('mqtt'), unraid_config, loop)

# Loop forever
loop.run_forever()