Add job path to Job initialization for better context in logging

This commit is contained in:
Théophile Diot 2024-12-30 15:36:43 +00:00
parent 2e5b84a566
commit 54bb80763b
No known key found for this signature in database
GPG key ID: FA995104A0BA376A
21 changed files with 29 additions and 49 deletions

View file

@ -32,7 +32,7 @@ try:
LOGGER.info("Backup feature is disabled, skipping backup ...")
sys_exit(0)
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
last_backup = loads(JOB.get_cache("backup.json") or "{}")
last_backup_date = last_backup.get("date", None)

View file

@ -97,7 +97,7 @@ try:
LOGGER.info("Blacklist is not activated, skipping downloads...")
sys_exit(0)
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
if not any(url for urls in services_blacklist_urls.values() for url in urls.values()):
LOGGER.warning("No blacklist URL is configured, nothing to do...")

View file

@ -38,7 +38,7 @@ try:
bunkernet_path = Path(sep, "var", "cache", "bunkerweb", "bunkernet")
bunkernet_path.mkdir(parents=True, exist_ok=True)
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
# Create empty file in case it doesn't exist
ip_list_path = bunkernet_path.joinpath("ip.list")

View file

@ -33,7 +33,7 @@ try:
sys_exit(0)
# Get ID from cache
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
bunkernet_id = JOB.get_cache("instance.id")
# Register instance

View file

@ -16,7 +16,7 @@ from jobs import Job # type: ignore
from logger import setup_logger # type: ignore
LOGGER = setup_logger("CUSTOM-CERT", getenv("LOG_LEVEL", "INFO"))
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
def check_cert(cert_file: Union[Path, bytes], key_file: Union[Path, bytes], first_server: str) -> Tuple[bool, Union[str, BaseException]]:

View file

@ -97,7 +97,7 @@ try:
LOGGER.info("Greylist is not activated, skipping downloads...")
sys_exit(0)
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
if not any(url for urls in services_greylist_urls.values() for url in urls.values()):
LOGGER.warning("No greylist URL is configured, nothing to do...")

View file

@ -16,7 +16,7 @@ status = 0
try:
# Restoring the backup failover configuration
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
except BaseException as e:
status = 2
LOGGER.error(f"Exception while running failover-backup.py :\n{e}")

View file

@ -50,7 +50,7 @@ try:
else:
LOGGER.warning("Unable to check if the temporary mmdb file is the latest version, downloading it anyway...")
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
if dl_mmdb:
job_cache = JOB.get_cache("asn.mmdb", with_info=True, with_data=True)

View file

@ -50,7 +50,7 @@ try:
else:
LOGGER.warning("Unable to check if the temporary mmdb file is the latest version, downloading it anyway...")
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
if dl_mmdb:
job_cache = JOB.get_cache("country.mmdb", with_info=True, with_data=True)

View file

@ -230,7 +230,7 @@ try:
"scaleway": ScalewayProvider,
}
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
# ? Restore data from db cache of certbot-renew job
JOB.restore_cache(job_name="certbot-renew")

View file

@ -42,7 +42,7 @@ try:
LOGGER.info("Let's Encrypt is not activated, skipping renew...")
sys_exit(0)
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
process = Popen(
[

View file

@ -26,7 +26,7 @@ try:
LOGGER.info("Skipping the sending of anonymous report (disabled)")
sys_exit(status)
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
if JOB.is_cached_file("last_report.json", "day"):
LOGGER.info("Skipping the sending of anonymous report (already sent today)")
sys_exit(0)

View file

@ -18,7 +18,7 @@ LOGGER_OPENSSL = setup_logger("DEFAULT-SERVER-CERT.openssl", getenv("LOG_LEVEL",
status = 0
try:
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
cert_path = Path(sep, "var", "cache", "bunkerweb", "misc")
if not JOB.is_cached_file("default-server-cert.pem", "month") or not JOB.is_cached_file("default-server-cert.key", "month"):

View file

@ -45,7 +45,7 @@ try:
LOGGER.info("Core Rule Set (CRS) nightly is not being used, skipping download...")
sys_exit(0)
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
LOGGER.info("Checking if Core Rule Set (CRS) nightly needs to be downloaded...")

View file

@ -93,7 +93,7 @@ try:
LOGGER.warning("No service is using a compatible Core Rule Set (CRS) version with the plugins (4 or nightly), skipping download...")
sys_exit(0)
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
downloaded_plugins: Dict[str, Set[str]] = {}
service_plugins: Dict[str, Set[str]] = {service: set() for service in services}

View file

@ -74,7 +74,7 @@ try:
LOGGER.info("RealIP is not activated, skipping download...")
sys_exit(0)
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
if not any(services_realip_urls.values()):
LOGGER.warning("No URL configured, nothing to do...")

View file

@ -19,7 +19,7 @@ from logger import setup_logger # type: ignore
from jobs import Job # type: ignore
LOGGER = setup_logger("self-signed", getenv("LOG_LEVEL", "INFO"))
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
status = 0

View file

@ -97,7 +97,7 @@ try:
LOGGER.info("Whitelist is not activated, skipping downloads...")
sys_exit(0)
JOB = Job(LOGGER)
JOB = Job(LOGGER, __file__)
if not any(url for urls in services_whitelist_urls.values() for url in urls.values()):
LOGGER.warning("No whitelist URL is configured, nothing to do...")

View file

@ -26,12 +26,12 @@ EXPIRE_TIME = {
class Job:
def __init__(self, logger: Optional[Logger] = None, db=None, *, job_name: str = "", deprecated: bool = False):
def __init__(self, logger: Logger, job_path: str, db=None, *, deprecated: bool = False):
"""Initialize Job class."""
unique_id = getattr(__name__, "unique_env_id", None)
if unique_id:
plugin_id = getenv(f"{unique_id}_PLUGIN_ID", "")
job_name = job_name or getenv(f"{unique_id}_JOB_NAME", "")
if job_path:
job_path = Path(job_path)
plugin_id = job_path.parent.parent.name
job_name = job_path.stem
else:
frame = currentframe()
if not frame:
@ -41,8 +41,6 @@ class Job:
if not source_path.exists():
raise ValueError("source_file could not be determined.")
elif not logger and not db:
raise ValueError("Either logger or db must be provided.")
plugin_id = source_path.parent.parent.name
job_name = job_name or source_path.name.replace(".py", "")

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager, suppress
from contextlib import suppress
from datetime import datetime
from functools import partial
from glob import glob
@ -14,7 +14,6 @@ from pathlib import Path
import re
from types import ModuleType
from typing import Any, Dict, List, Optional
from uuid import uuid4
import schedule
from schedule import Job
from sys import path as sys_path
@ -30,21 +29,6 @@ from logger import setup_logger # type: ignore
from ApiCaller import ApiCaller # type: ignore
@contextmanager
def unique_env_context(**kwargs):
"""Context manager for setting and cleaning up unique-prefixed environment variables."""
unique_id = uuid4().hex # Generate a unique identifier
prefixed_keys = {f"{unique_id}_{key}": str(value) for key, value in kwargs.items()}
try:
# Set environment variables with unique prefixes
environ.update(prefixed_keys)
yield unique_id # Provide the unique ID for retrieval
finally:
# Clean up environment variables
for key in prefixed_keys:
environ.pop(key, None)
class JobScheduler(ApiCaller):
def __init__(
self,
@ -174,11 +158,9 @@ class JobScheduler(ApiCaller):
module_dir = dirname(path)
sys_path.insert(0, module_dir)
try:
with unique_env_context(**kwargs) as unique_id:
spec = spec_from_file_location(name, path)
module = module_from_spec(spec)
setattr(module, "unique_env_id", unique_id)
spec.loader.exec_module(module)
spec = spec_from_file_location(name, path)
module = module_from_spec(spec)
spec.loader.exec_module(module)
finally:
sys_path.remove(module_dir)
@ -188,7 +170,7 @@ class JobScheduler(ApiCaller):
ret = -1
start_date = datetime.now().astimezone()
try:
self.__exec_plugin_module(join(path, "jobs", file), name, PLUGIN_ID=plugin, JOB_NAME=name)
self.__exec_plugin_module(join(path, "jobs", file), name)
ret = 1
except SystemExit as e:
ret = e.code if isinstance(e.code, int) else 1

View file

@ -495,7 +495,7 @@ if __name__ == "__main__":
SCHEDULER = JobScheduler(environ, LOGGER, db=Database(LOGGER, sqlalchemy_string=dotenv_env.get("DATABASE_URI", getenv("DATABASE_URI", None)))) # type: ignore
JOB = Job(LOGGER, SCHEDULER.db)
JOB = Job(LOGGER, __file__, SCHEDULER.db)
APPLYING_CHANGES.set()