Refactor new program class

This commit is contained in:
EstrellaXD
2023-05-17 21:35:56 +08:00
parent 0bc25fe00b
commit 6498614b1b
4 changed files with 24 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
import os
import signal
import logging
import asyncio
from fastapi.exceptions import HTTPException
@@ -13,20 +14,20 @@ program = Program()
@router.on_event("startup")
async def startup():
await program.startup()
async def on_startup():
asyncio.create_task(program.startup())
@router.on_event("shutdown")
async def shutdown():
await program.stop()
def shutdown():
program.stop()
logger.info("Stopping program...")
@router.get("/api/v1/restart", tags=["program"])
async def restart():
try:
await program.restart()
program.restart()
return {"status": "ok"}
except Exception as e:
logger.debug(e)
@@ -37,7 +38,7 @@ async def restart():
@router.get("/api/v1/start", tags=["program"])
async def start():
try:
await program.start()
program.start()
return {"status": "ok"}
except Exception as e:
logger.debug(e)
@@ -47,7 +48,7 @@ async def start():
@router.get("/api/v1/stop", tags=["program"])
async def stop():
await program.stop()
program.stop()
return {"status": "ok"}
@@ -61,7 +62,7 @@ async def status():
@router.get("/api/v1/shutdown", tags=["program"])
async def shutdown_program():
await program.stop()
program.stop()
logger.info("Shutting down program...")
os.kill(os.getpid(), signal.SIGINT)
return {"status": "ok"}

View File

@@ -35,11 +35,13 @@ class Checker:
@staticmethod
def check_torrents() -> bool:
with RequestContent() as req:
torrents = req.get_torrents(settings.rss_link)
if torrents:
return True
else:
return False
try:
torrents = req.get_torrents(settings.rss_link)
if torrents:
return True
except AttributeError:
pass
return False
@staticmethod
def check_first_run() -> bool:

View File

@@ -21,9 +21,9 @@ class Program(RenameThread, RSSThread):
async def startup(self):
self.__start_info()
await self.start()
self.start()
async def start(self):
def start(self):
self.stop_event.clear()
settings.load()
if self.enable_renamer:
@@ -34,7 +34,7 @@ class Program(RenameThread, RSSThread):
logger.info("RSS started.")
return {"status": "Program started."}
async def stop(self):
def stop(self):
if self.is_running:
self.stop_event.set()
self.rename_stop()
@@ -42,7 +42,7 @@ class Program(RenameThread, RSSThread):
else:
return {"status": "Program is not running."}
async def restart(self):
await self.stop()
await self.start()
def restart(self):
self.stop()
self.start()
return {"status": "Program restarted."}

View File

@@ -1,4 +1,5 @@
import threading
import asyncio
from module.checker import Checker
@@ -10,6 +11,7 @@ class ProgramStatus(Checker):
self.lock = threading.Lock()
self._downloader_status = False
self._torrents_status = False
self.event = asyncio.Event()
@property
def is_running(self):