-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
146 lines (120 loc) · 4.26 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""
Remotescan: A media server library scanner and notification tool.
Remotescan monitors specified directories for changes (new files, deletions, etc.)
and triggers library scans on media servers like Plex, Emby, and Jellyfin.
Key Components:
- ApiManager: Handles communication with media server APIs.
- Remotescan Service: Monitors file system changes and triggers server scans.
- BlockingScheduler: Manages scheduled tasks.
- LogManager: Handles logging to files and console.
Configuration:
Remotescan is configured via a JSON file specified by the CONFIG_PATH
environment variable.
Usage:
1. Set the CONFIG_PATH environment variable to the location of your
configuration file.
2. Run the app.py script.
"""
REMOTE_SCAN_VERSION: str = "v2.1.0"
import sys
import os
import json
import signal
import time
from sys import platform
from typing import Any
from apscheduler.schedulers.blocking import BlockingScheduler
from api.api_manager import ApiManager
from common.log_manager import LogManager
from common import utils
from service.ServiceBase import ServiceBase
if platform == "linux":
from service.Remotescan import Remotescan
# Global Variables
api_manager: ApiManager = None
log_manager = LogManager(__name__)
scheduler = BlockingScheduler()
# Available Services
services: list[ServiceBase] = []
##########################
def handle_sigterm(signum: int, frame: Any):
"""Handles the SIGTERM signal for graceful shutdown."""
log_manager.get_logger().info("SIGTERM received, shutting down ...")
for service in services:
service.shutdown()
scheduler.shutdown(wait=True)
sys.exit(0)
def _create_services(data: dict):
"""Creates and returns a list of services based on the configuration."""
if platform == "linux":
if "remote_scan" in data:
services.append(
Remotescan(
api_manager,
data["remote_scan"],
log_manager.get_logger(),
scheduler
)
)
else:
log_manager.get_logger().error(
"Configuration file problem no remote_scan data found!"
)
def _do_nothing() -> None:
"""A dummy function to keep the scheduler alive."""
time.sleep(1)
conf_loc_path_file: str = ""
config_file_valid: bool = True
if "CONFIG_PATH" in os.environ:
conf_loc_path_file = os.environ["CONFIG_PATH"].rstrip("/")
else:
config_file_valid = False
if config_file_valid and os.path.exists(conf_loc_path_file):
try:
# Load the configuration file
with open(conf_loc_path_file, "r") as f:
data: dict = json.load(f)
# Set up signal termination handle
signal.signal(signal.SIGTERM, handle_sigterm)
# Configure the gotify logging
log_manager.configure_gotify(data)
log_manager.get_logger().info(
f"Starting Remotescan {REMOTE_SCAN_VERSION}"
)
# Create the API Manager
api_manager = ApiManager(data, log_manager.get_logger())
# Create the services
_create_services(data)
# Init the services
for service in services:
service.init_scheduler_jobs()
if len(services) > 0:
# Add a job to do nothing to keep the script alive
scheduler.add_job(
_do_nothing,
trigger="interval",
hours=24
)
# Start the scheduler for all jobs
scheduler.start()
except FileNotFoundError as e:
log_manager.get_logger().error(
f"Config file not found: {utils.get_tag('error', e)}"
)
except json.JSONDecodeError as e:
log_manager.get_logger().error(
f"Error decoding JSON in config file: {utils.get_tag('error', e)}"
)
except KeyError as e:
log_manager.get_logger().error(
f"Missing key in config file: {utils.get_tag('error', e)}"
)
except Exception as e:
log_manager.get_logger().error(
f"An unexpected error occurred: {utils.get_tag('error', e)}"
)
else:
log_manager.get_logger().error(
f"Error finding config file {conf_loc_path_file}\n"
)
# END Main script run