This repository has been archived on 2024-04-22. You can view files and clone it, but cannot push or open issues or pull requests.
caylon/main.py

29 lines
909 B
Python
Raw Normal View History

2022-07-30 19:58:49 +01:00
import pathlib
import subprocess
import asyncio
import os
2022-07-24 20:15:35 +01:00
2022-07-30 19:58:49 +01:00
HOME_DIR = str(pathlib.Path(os.getcwd()).parent.parent.resolve())
PARENT_DIR = str(pathlib.Path(__file__).parent.resolve())
2022-07-24 20:15:35 +01:00
2022-07-30 19:58:49 +01:00
class Plugin:
backend_proc = None
2022-07-24 20:15:35 +01:00
# Asyncio-compatible long-running code, executed in a task when the plugin is loaded
async def _main(self):
2022-07-30 19:58:49 +01:00
# startup
2023-03-10 02:16:29 +00:00
self.backend_proc = subprocess.Popen(
[PARENT_DIR + "/bin/backend", "--config", "./caylon.json"],
env = dict(os.environ))
2022-07-30 19:58:49 +01:00
while True:
2022-09-12 04:45:31 +01:00
await asyncio.sleep(1)
2023-03-10 02:16:29 +00:00
async def _unload(self):
# shutdown
if self.backend_proc is not None:
self.backend_proc.terminate()
try:
self.backend_proc.wait(timeout=5) # 5 seconds timeout
except subprocess.TimeoutExpired:
self.backend_proc.kill()
self.backend_proc = None