mirror of
https://github.com/EstrellaXD/Auto_Bangumi.git
synced 2026-04-14 10:30:35 +08:00
Refactor new program class
This commit is contained in:
@@ -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"}
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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."}
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user