Skip to content

Commit

Permalink
feature: add job to autoclean QDT expired resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Jan 23, 2024
1 parent b47cb13 commit 19d2f51
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
8 changes: 6 additions & 2 deletions qgis_deployment_toolbelt/jobs/generic_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
from sys import platform as opersys

# package
from qgis_deployment_toolbelt.constants import OS_CONFIG, get_qdt_working_directory
from qgis_deployment_toolbelt.constants import (
OS_CONFIG,
get_qdt_logs_folder,
get_qdt_working_directory,
)
from qgis_deployment_toolbelt.exceptions import (
JobOptionBadName,
JobOptionBadValue,
Expand Down Expand Up @@ -60,7 +64,7 @@ def __init__(self) -> None:
f"repositories/{getenv('QDT_TMP_RUNNING_SCENARIO_ID', 'default')}"
)
self.qdt_plugins_folder = self.qdt_working_folder.joinpath("plugins")

self.qdt_logs_folder = get_qdt_logs_folder()
# destination profiles folder
self.qgis_profiles_path: Path = Path(OS_CONFIG.get(opersys).profiles_path)
if not self.qgis_profiles_path.exists():
Expand Down
85 changes: 85 additions & 0 deletions qgis_deployment_toolbelt/jobs/job_autoclean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#! python3 # noqa: E265

"""
QDT autocleaner.
Author: Julien Moura (https://github.com/guts)
"""


# #############################################################################
# ########## Libraries #############
# ##################################

# Standard library
import logging
from pathlib import Path

# package
from qgis_deployment_toolbelt.jobs.generic_job import GenericJob
from qgis_deployment_toolbelt.utils.file_stats import is_file_older_than
from qgis_deployment_toolbelt.utils.trash_or_delete import move_files_to_trash_or_delete

# #############################################################################
# ########## Globals ###############
# ##################################

# logs
logger = logging.getLogger(__name__)


# #############################################################################
# ########## Classes ###############
# ##################################


class JobAutoclean(GenericJob):
"""
Job to clean expired QDT resources (logs, plugins archives...) which are older than
a specified frequency.
"""

ID: str = "qdt-autoclean"
OPTIONS_SCHEMA: dict = {
"delay": {
"type": int,
"required": False,
"default": 730,
"possible_values": range(1, 1000),
"condition": None,
},
}

def __init__(self, options: dict) -> None:
"""Instantiate the class.
:param dict options: job options.
"""
super().__init__()
self.options: dict = self.validate_options(options)

def run(self) -> None:
"""Execute job logic."""
li_files_to_be_deleted: list[Path] = []

# clean logs
for log_file in self.qdt_logs_folder.glob("*.log"):
if is_file_older_than(
local_file_path=log_file,
expiration_rotating_hours=self.options.get("delay", 730),
):
logger.debug(f"Autoclean - LOGS - Outdated file: {log_file}")
li_files_to_be_deleted.append(log_file)

# clean plugins archives
for plugin_archive in self.qdt_plugins_folder.glob("*.zip"):
if is_file_older_than(
local_file_path=plugin_archive,
expiration_rotating_hours=self.options.get("delay", 730),
):
logger.debug(
f"Autoclean - PLUGIN ARCHIVE - Outdated file: {plugin_archive}"
)
li_files_to_be_deleted.append(plugin_archive)

move_files_to_trash_or_delete(files_to_trash=li_files_to_be_deleted)

0 comments on commit 19d2f51

Please sign in to comment.