#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2012-2021 CERN
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Authors:
# - Vincent Garonne <vincent.garonne@cern.ch>, 2012-2018
# - Wen Guan <wen.guan@cern.ch>, 2014
# - Mario Lassnig <mario.lassnig@cern.ch>, 2019
# - Patrick Austin <patrick.austin@stfc.ac.uk>, 2020
# - Benedikt Ziemons <benedikt.ziemons@cern.ch>, 2020
# - Martin Barisits <martin.barisits@cern.ch>, 2021
# - Joel Dierkes <joel.dierkes@cern.ch>, 2021

'''
'''

import argparse
import signal

from rucio.common.utils import StoreAndDeprecateWarningAction
from rucio.daemons.reaper.reaper import run, stop


def get_parser():
    """
    Returns the argparse parser.
    """
    parser = argparse.ArgumentParser()
    parser.add_argument("--run-once", action="store_true", default=False,
                        help='Runs one loop iteration')
    parser.add_argument("--threads", action="store", default=1, type=int,
                        help='Concurrency control: number of threads')
    parser.add_argument("--chunk_size", "--chunk-size", new_option_string="--chunk-size", action=StoreAndDeprecateWarningAction, default=100, type=int,
                        help='The size used for a bulk deletion on on RSE')
    parser.add_argument('--sleep-time', action="store", default=60, type=int,
                        help='Minimum time between 2 consecutive cycles')
    parser.add_argument('--greedy', action="store_true", default=False,
                        help='To enable greedy mode. Deprecated, kept for compatibility reasons. Recommended way is to use RSE attribute greedyDeletion=True')
    parser.add_argument('--exclude-rses', action="store", default=None, type=str,
                        help='RSE expression to exclude')
    parser.add_argument('--include-rses', action="store", default=None, type=str,
                        help='RSE expression to include')
    parser.add_argument('--rses', nargs='+', type=str,
                        help='Explicit list of RSEs to include. If empty, it considers all RSEs')
    parser.add_argument('--vos', nargs='+', type=str,
                        help='Optional list of VOs to consider. Only used in multi-VO mode.')
    parser.add_argument("--delay-seconds", action="store", default=600, type=int,
                        help='The delay (seconds) to query replicas in BEING_DELETED state.')
    parser.add_argument('--scheme', action="store", default=None, type=str,
                        help='Force the reaper to use a particular protocol/scheme, e.g., mock')
    parser.add_argument("--auto_exclude_threshold", "--auto-exclude-threshhold", new_option_string="--auto-exclude-threshhold", action=StoreAndDeprecateWarningAction, default=100, type=int,
                        help='Number of service unavailable exceptions after which the RSE gets temporarily excluded.')
    parser.add_argument("--auto_exclude_timeout", "--auto-exclude-timeout", new_option_string="--auto-exclude-timeout", action=StoreAndDeprecateWarningAction, default=600, type=int,
                        help='Timeout for temporarily excluded RSEs.')

    return parser


if __name__ == "__main__":

    # Bind our callback to the SIGTERM signal and run the daemon:
    signal.signal(signal.SIGTERM, stop)

    parser = get_parser()
    args = parser.parse_args()
    try:
        run(threads=args.threads,
            chunk_size=args.chunk_size,
            include_rses=args.include_rses,
            exclude_rses=args.exclude_rses,
            rses=args.rses,
            vos=args.vos,
            once=args.run_once,
            greedy=args.greedy,
            scheme=args.scheme,
            delay_seconds=args.delay_seconds,
            sleep_time=args.sleep_time,
            auto_exclude_threshold=args.auto_exclude_threshold,
            auto_exclude_timeout=args.auto_exclude_timeout)
    except KeyboardInterrupt:
        stop()
