Viewing File: /opt/imunify360/venv/versions/imunify-core-8.12.1-2/defence360agent/utils/check_lock.py

import random
import time


def check_lock(check_lock_period: int, lock_file, jitter: bool = False):
    if not lock_file.exists():
        lock_file.parent.mkdir(parents=True, exist_ok=True)
        if jitter:
            delay = random.randrange(int(check_lock_period))
            lock_file.write_text(str(time.time() + delay + check_lock_period))
            return delay
        lock_file.write_text(str(time.time() + check_lock_period))
        return 0

    if (time_left := is_period_passed(check_lock_period, lock_file)) <= 0:
        lock_file.write_text(str(time.time() + check_lock_period))
        return 0
    else:
        return time_left


def is_period_passed(period, lock_file):
    try:
        when_to_run = float(lock_file.read_text())
    except (FileNotFoundError, ValueError):
        return 0
    return when_to_run - time.time()
Back to Directory File Manager