|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# --- BEGIN_HEADER --- |
| 5 | +# |
| 6 | +# grid_janitor - daemon to handle recurring tasks like clean up and updates |
| 7 | +# Copyright (C) 2003-2025 The MiG Project by the Science HPC Center at UCPH |
| 8 | +# |
| 9 | +# This file is part of MiG. |
| 10 | +# |
| 11 | +# MiG is free software: you can redistribute it and/or modify |
| 12 | +# it under the terms of the GNU General Public License as published by |
| 13 | +# the Free Software Foundation; either version 2 of the License, or |
| 14 | +# (at your option) any later version. |
| 15 | +# |
| 16 | +# MiG is distributed in the hope that it will be useful, |
| 17 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | +# GNU General Public License for more details. |
| 20 | +# |
| 21 | +# You should have received a copy of the GNU General Public License |
| 22 | +# along with this program; if not, write to the Free Software |
| 23 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 24 | +# |
| 25 | +# -- END_HEADER --- |
| 26 | +# |
| 27 | + |
| 28 | +"""Daemon to take care of various recurring tasks like clean up, cache updates |
| 29 | +and pruning of pending requests. |
| 30 | +""" |
| 31 | + |
| 32 | +from __future__ import absolute_import, print_function |
| 33 | + |
| 34 | +import multiprocessing |
| 35 | +import os |
| 36 | +import signal |
| 37 | +import sys |
| 38 | +import time |
| 39 | + |
| 40 | +from mig.shared.conf import get_configuration_object |
| 41 | +from mig.shared.logger import daemon_logger, register_hangup_handler |
| 42 | + |
| 43 | +stop_running = multiprocessing.Event() |
| 44 | +(configuration, logger) = (None, None) |
| 45 | + |
| 46 | + |
| 47 | +def stop_handler(sig, frame): |
| 48 | + """A simple signal handler to quit on Ctrl+C (SIGINT) in main""" |
| 49 | + # Print blank line to avoid mix with Ctrl-C line |
| 50 | + print("") |
| 51 | + stop_running.set() |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + # Force no log init since we use separate logger |
| 56 | + configuration = get_configuration_object(skip_log=True) |
| 57 | + |
| 58 | + log_level = configuration.loglevel |
| 59 | + if sys.argv[1:] and sys.argv[1] in ["debug", "info", "warning", "error"]: |
| 60 | + log_level = sys.argv[1] |
| 61 | + |
| 62 | + # Use separate logger |
| 63 | + |
| 64 | + logger = daemon_logger("janitor", configuration.user_janitor_log, log_level) |
| 65 | + configuration.logger = logger |
| 66 | + |
| 67 | + # Allow e.g. logrotate to force log re-open after rotates |
| 68 | + register_hangup_handler(configuration) |
| 69 | + |
| 70 | + # Allow clean shutdown on SIGINT only to main process |
| 71 | + signal.signal(signal.SIGINT, stop_handler) |
| 72 | + |
| 73 | + if not configuration.site_enable_janitor: |
| 74 | + err_msg = "Janitor support is disabled in configuration!" |
| 75 | + logger.error(err_msg) |
| 76 | + print(err_msg) |
| 77 | + sys.exit(1) |
| 78 | + |
| 79 | + print( |
| 80 | + """This is the MiG janitor daemon which cleans up stale state data, |
| 81 | +updates internal caches and prunes pending requests. |
| 82 | +
|
| 83 | +Set the MIG_CONF environment to the server configuration path |
| 84 | +unless it is available in mig/server/MiGserver.conf |
| 85 | +""" |
| 86 | + ) |
| 87 | + |
| 88 | + main_pid = os.getpid() |
| 89 | + print("Starting janitor daemon - Ctrl-C to quit") |
| 90 | + logger.info("(%s) Starting Janitor daemon" % main_pid) |
| 91 | + |
| 92 | + logger.debug("(%s) Starting main loop" % main_pid) |
| 93 | + print("%s: Start main loop" % os.getpid()) |
| 94 | + while not stop_running.is_set(): |
| 95 | + try: |
| 96 | + time.sleep(1) |
| 97 | + except KeyboardInterrupt: |
| 98 | + stop_running.set() |
| 99 | + # NOTE: we can't be sure if SIGINT was sent to only main process |
| 100 | + # so we make sure to propagate to monitor child |
| 101 | + print("Interrupt requested - shutdown") |
| 102 | + except Exception as exc: |
| 103 | + logger.error( |
| 104 | + "(%s) Caught unexpected exception: %s" % (os.getpid(), exc) |
| 105 | + ) |
| 106 | + |
| 107 | + print("Janitor daemon shutting down") |
| 108 | + logger.info("(%s) Janitor daemon shutting down" % main_pid) |
| 109 | + |
| 110 | + sys.exit(0) |
0 commit comments