2022-05-24 01:40:49 +01:00
|
|
|
import pathlib
|
2022-05-28 01:52:32 +01:00
|
|
|
import subprocess
|
2022-07-09 17:13:39 +01:00
|
|
|
import asyncio
|
|
|
|
import os
|
2022-05-24 01:40:49 +01:00
|
|
|
|
|
|
|
HOME_DIR = str(pathlib.Path(os.getcwd()).parent.parent.resolve())
|
2022-07-09 17:13:39 +01:00
|
|
|
PARENT_DIR = str(pathlib.Path(__file__).parent.resolve())
|
|
|
|
|
|
|
|
LOG_LOCATION = "/tmp/fantastic.py.log"
|
2022-05-04 01:26:51 +01:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logging.basicConfig(
|
2022-07-09 17:13:39 +01:00
|
|
|
filename = LOG_LOCATION,
|
2022-05-04 01:26:51 +01:00
|
|
|
format = '%(asctime)s %(levelname)s %(message)s',
|
|
|
|
filemode = 'w',
|
|
|
|
force = True)
|
|
|
|
|
|
|
|
logger = logging.getLogger()
|
|
|
|
logger.setLevel(logging.DEBUG)
|
2022-07-09 17:13:39 +01:00
|
|
|
logging.info(f"Fantastic main.py https://github.com/NGnius/Fantastic")
|
2022-07-05 03:02:38 +01:00
|
|
|
|
2022-05-02 01:16:30 +01:00
|
|
|
class Plugin:
|
2022-07-09 17:13:39 +01:00
|
|
|
backend_proc = None
|
2022-05-02 01:16:30 +01:00
|
|
|
# Asyncio-compatible long-running code, executed in a task when the plugin is loaded
|
|
|
|
async def _main(self):
|
2022-05-04 01:26:51 +01:00
|
|
|
# startup
|
2023-02-24 02:15:33 +00:00
|
|
|
self.backend_proc = subprocess.Popen(
|
|
|
|
[PARENT_DIR + "/bin/backend"],
|
|
|
|
env = dict(os.environ))
|
2022-05-04 01:26:51 +01:00
|
|
|
while True:
|
2022-07-26 21:33:30 +01:00
|
|
|
await asyncio.sleep(1)
|
2023-02-24 02:15:33 +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
|