mirror of
https://github.com/netinvent/npbackup.git
synced 2025-09-13 00:16:21 +08:00
Move concurrency checks to runner
This commit is contained in:
parent
8b8c568720
commit
68ace1820b
2 changed files with 41 additions and 17 deletions
|
@ -12,8 +12,6 @@ from pathlib import Path
|
|||
import atexit
|
||||
from argparse import ArgumentParser
|
||||
from datetime import datetime
|
||||
import tempfile
|
||||
import pidfile
|
||||
import ofunctions.logger_utils
|
||||
from ofunctions.process import kill_childs
|
||||
from npbackup.path_helper import CURRENT_DIR
|
||||
|
@ -39,7 +37,6 @@ except ImportError:
|
|||
|
||||
|
||||
LOG_FILE = os.path.join(CURRENT_DIR, "{}.log".format(__intname__))
|
||||
PID_FILE = os.path.join(tempfile.gettempdir(), "{}.pid".format(__intname__))
|
||||
|
||||
|
||||
logger = ofunctions.logger_utils.logger_get_logger(LOG_FILE, debug=_DEBUG)
|
||||
|
@ -316,22 +313,11 @@ This is free software, and you are welcome to redistribute it under certain cond
|
|||
elif args.has_recent_snapshot:
|
||||
cli_args["operation"] = "has_recent_snapshot"
|
||||
|
||||
|
||||
if cli_args["operation"]:
|
||||
locking_operations = ["backup", "repair", "forget", "prune", "raw", "unlock"]
|
||||
# Program entry
|
||||
if cli_args["operation"] in locking_operations:
|
||||
try:
|
||||
with pidfile.PIDFile(PID_FILE):
|
||||
entrypoint(**cli_args)
|
||||
except pidfile.AlreadyRunningError:
|
||||
logger.critical("Backup process already running. Will not continue.")
|
||||
# EXIT_CODE 21 = current backup process already running
|
||||
sys.exit(21)
|
||||
else:
|
||||
entrypoint(**cli_args)
|
||||
entrypoint(**cli_args)
|
||||
else:
|
||||
logger.warning("No operation has been requested")
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
|
|
|
@ -13,6 +13,8 @@ __build__ = "2023083101"
|
|||
from typing import Optional, Callable, Union, List
|
||||
import os
|
||||
import logging
|
||||
import tempfile
|
||||
import pidfile
|
||||
import queue
|
||||
from datetime import datetime, timedelta
|
||||
from functools import wraps
|
||||
|
@ -362,10 +364,34 @@ class NPBackupRunner:
|
|||
return fn(self, *args, **kwargs)
|
||||
|
||||
return wrapper
|
||||
|
||||
def check_concurrency(fn: Callable):
|
||||
"""
|
||||
Make sure there we don't allow concurrent actions
|
||||
"""
|
||||
@wraps(fn)
|
||||
def wrapper(self, *args, **kwargs):
|
||||
locking_operations = ["backup", "repair", "forget", "prune", "raw", "unlock"]
|
||||
if fn.__name__ in locking_operations:
|
||||
pid_file = os.path.join(tempfile.gettempdir(), "{}.pid".format(__intname__))
|
||||
try:
|
||||
with pidfile.PIDFile(pid_file):
|
||||
# pylint: disable=E1102 (not-callable)
|
||||
result = fn(self, *args, **kwargs)
|
||||
except pidfile.AlreadyRunningError:
|
||||
# pylint: disable=E1101 (no-member)
|
||||
self.write_logs("There is already an operation {fn.__name__} running. Will not continue", level="critical")
|
||||
return False
|
||||
else:
|
||||
# pylint: disable=E1102 (not-callable)
|
||||
result = fn(self, *args, **kwargs)
|
||||
return result
|
||||
|
||||
return wrapper
|
||||
|
||||
def catch_exceptions(fn: Callable):
|
||||
"""
|
||||
Catch any exception and log it
|
||||
Catch any exception and log it so we don't loose exceptions in thread
|
||||
"""
|
||||
|
||||
@wraps(fn)
|
||||
|
@ -582,6 +608,7 @@ class NPBackupRunner:
|
|||
@threaded
|
||||
@close_queues
|
||||
@exec_timer
|
||||
@check_concurrency
|
||||
@has_permission
|
||||
@is_ready
|
||||
@apply_config_to_restic_runner
|
||||
|
@ -596,6 +623,7 @@ class NPBackupRunner:
|
|||
@threaded
|
||||
@close_queues
|
||||
@exec_timer
|
||||
@check_concurrency
|
||||
@has_permission
|
||||
@is_ready
|
||||
@apply_config_to_restic_runner
|
||||
|
@ -617,6 +645,7 @@ class NPBackupRunner:
|
|||
@threaded
|
||||
@close_queues
|
||||
@exec_timer
|
||||
@check_concurrency
|
||||
@has_permission
|
||||
@is_ready
|
||||
@apply_config_to_restic_runner
|
||||
|
@ -632,6 +661,7 @@ class NPBackupRunner:
|
|||
@threaded
|
||||
@close_queues
|
||||
@exec_timer
|
||||
@check_concurrency
|
||||
@has_permission
|
||||
@is_ready
|
||||
@apply_config_to_restic_runner
|
||||
|
@ -678,6 +708,7 @@ class NPBackupRunner:
|
|||
@threaded
|
||||
@close_queues
|
||||
@exec_timer
|
||||
@check_concurrency
|
||||
@has_permission
|
||||
@is_ready
|
||||
@apply_config_to_restic_runner
|
||||
|
@ -863,6 +894,7 @@ class NPBackupRunner:
|
|||
@threaded
|
||||
@close_queues
|
||||
@exec_timer
|
||||
@check_concurrency
|
||||
@has_permission
|
||||
@is_ready
|
||||
@apply_config_to_restic_runner
|
||||
|
@ -879,6 +911,7 @@ class NPBackupRunner:
|
|||
@threaded
|
||||
@close_queues
|
||||
@exec_timer
|
||||
@check_concurrency
|
||||
@has_permission
|
||||
@is_ready
|
||||
@apply_config_to_restic_runner
|
||||
|
@ -924,6 +957,7 @@ class NPBackupRunner:
|
|||
@threaded
|
||||
@close_queues
|
||||
@exec_timer
|
||||
@check_concurrency
|
||||
@has_permission
|
||||
@is_ready
|
||||
@apply_config_to_restic_runner
|
||||
|
@ -945,6 +979,7 @@ class NPBackupRunner:
|
|||
@threaded
|
||||
@close_queues
|
||||
@exec_timer
|
||||
@check_concurrency
|
||||
@has_permission
|
||||
@is_ready
|
||||
@apply_config_to_restic_runner
|
||||
|
@ -964,6 +999,7 @@ class NPBackupRunner:
|
|||
@threaded
|
||||
@close_queues
|
||||
@exec_timer
|
||||
@check_concurrency
|
||||
@has_permission
|
||||
@is_ready
|
||||
@apply_config_to_restic_runner
|
||||
|
@ -978,6 +1014,7 @@ class NPBackupRunner:
|
|||
@threaded
|
||||
@close_queues
|
||||
@exec_timer
|
||||
@check_concurrency
|
||||
@has_permission
|
||||
@is_ready
|
||||
@apply_config_to_restic_runner
|
||||
|
@ -990,6 +1027,7 @@ class NPBackupRunner:
|
|||
@threaded
|
||||
@close_queues
|
||||
@exec_timer
|
||||
@check_concurrency
|
||||
@has_permission
|
||||
@is_ready
|
||||
@apply_config_to_restic_runner
|
||||
|
|
Loading…
Add table
Reference in a new issue