#!/usr/bin/python
# Copyright European Organization for Nuclear Research (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
#
# Authors:
# - Cedric Serfon, <cedric.serfon@cern.ch>, 2014

'''
Automatix (Dataset injector) Daemon
'''

import argparse
import signal

from rucio.tests.daemons.Automatix import run, stop


if __name__ == "__main__":

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

    parser = argparse.ArgumentParser()
    parser.add_argument("--run-once", action="store_true", default=False, help='Runs one loop iteration')
    parser.add_argument("--input-file", action="store", default=" /opt/rucio/etc/automatix.json", type=str, help='Automatix configuration')
    parser.add_argument("--threads-per-process", action="store", default=1, type=int, help='Total number of workers per process')

    args = parser.parse_args()

    print 'Start Automatix'
    try:
        run(total_workers=args.threads_per_process, once=args.run_once, inputfile=args.input_file)
    except KeyboardInterrupt:
        stop()
