# -*- coding: utf-8 -*-

import time
import puka
import argparse


import logging
from infcommon import utils
from infrabbitmq import factory as infrabbitmq_factory
from infrabbitmq.rabbitmq import RabbitMQError
from infrabbitmq.events_names import (
    TICK_1_SECOND,
    TICK_1_MINUTE,
    TICK_2_MINUTES,
    TICK_5_MINUTES,
    TICK_60_MINUTES,
)


def publish_event(publisher, event, network, secs, mins):
    logging.info("publish event {} {}".format(event, secs))
    publisher.publish(event, network, data={'tick': secs, 'mins': mins})


def main(network):
    publisher = infrabbitmq_factory.event_publisher_json_serializer()
    secs = 0
    mins = 0

    rabbitmq_exceptions = (RabbitMQError, puka.AMQPError, KeyError,)
    while True:
        time.sleep(1)
        secs += 1

        utils.do_stuff_with_exponential_backoff(rabbitmq_exceptions,
            publish_event,
            publisher, TICK_1_SECOND, network, secs, mins)

        if secs % 60 == 0:
            mins += 1
            secs = 0

            utils.do_stuff_with_exponential_backoff(rabbitmq_exceptions,
                    publish_event,
                    publisher, TICK_1_MINUTE, network, secs, mins)

            if mins % 2 == 0:
                utils.do_stuff_with_exponential_backoff(rabbitmq_exceptions,
                    publish_event,
                    publisher, TICK_2_MINUTES, network, secs, mins)

            if mins % 5 == 0:
                utils.do_stuff_with_exponential_backoff(rabbitmq_exceptions,
                    publish_event,
                    publisher, TICK_5_MINUTES, network, secs, mins)

            if mins % 60 == 0:
                utils.do_stuff_with_exponential_backoff(rabbitmq_exceptions,
                    publish_event,
                    publisher, TICK_60_MINUTES, network, secs, mins)


if __name__ == '__main__':
    try:
        parser = argparse.ArgumentParser()
        parser.add_argument('-n', '--network', action='store', required=True, help='Network name (ilo, c2k, ...)')
        args = parser.parse_args()
        network = args.network.split('-')[0]

        main(network)
    except Exception as exc:
        logging.critical("Ticker Fails: {}".format(exc))

[((27, 16, 27, 69), 'infrabbitmq.factory.event_publisher_json_serializer', 'infrabbitmq_factory.event_publisher_json_serializer', ({}, {}), '()', True, 'from infrabbitmq import factory as infrabbitmq_factory\n'), ((33, 8, 33, 21), 'time.sleep', 'time.sleep', ({(33, 19, 33, 20): '(1)'}, {}), '(1)', False, 'import time\n'), ((36, 8, 38, 58), 'infcommon.utils.do_stuff_with_exponential_backoff', 'utils.do_stuff_with_exponential_backoff', ({(36, 48, 36, 67): 'rabbitmq_exceptions', (37, 12, 37, 25): 'publish_event', (38, 12, 38, 21): 'publisher', (38, 23, 38, 36): 'TICK_1_SECOND', (38, 38, 38, 45): 'network', (38, 47, 38, 51): 'secs', (38, 53, 38, 57): 'mins'}, {}), '(rabbitmq_exceptions, publish_event,\n    publisher, TICK_1_SECOND, network, secs, mins)', False, 'from infcommon import utils\n'), ((66, 17, 66, 42), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ({}, {}), '()', False, 'import argparse\n'), ((44, 12, 46, 66), 'infcommon.utils.do_stuff_with_exponential_backoff', 'utils.do_stuff_with_exponential_backoff', ({(44, 52, 44, 71): 'rabbitmq_exceptions', (45, 20, 45, 33): 'publish_event', (46, 20, 46, 29): 'publisher', (46, 31, 46, 44): 'TICK_1_MINUTE', (46, 46, 46, 53): 'network', (46, 55, 46, 59): 'secs', (46, 61, 46, 65): 'mins'}, {}), '(rabbitmq_exceptions, publish_event,\n    publisher, TICK_1_MINUTE, network, secs, mins)', False, 'from infcommon import utils\n'), ((49, 16, 51, 67), 'infcommon.utils.do_stuff_with_exponential_backoff', 'utils.do_stuff_with_exponential_backoff', ({(49, 56, 49, 75): 'rabbitmq_exceptions', (50, 20, 50, 33): 'publish_event', (51, 20, 51, 29): 'publisher', (51, 31, 51, 45): 'TICK_2_MINUTES', (51, 47, 51, 54): 'network', (51, 56, 51, 60): 'secs', (51, 62, 51, 66): 'mins'}, {}), '(rabbitmq_exceptions, publish_event,\n    publisher, TICK_2_MINUTES, network, secs, mins)', False, 'from infcommon import utils\n'), ((54, 16, 56, 67), 'infcommon.utils.do_stuff_with_exponential_backoff', 'utils.do_stuff_with_exponential_backoff', ({(54, 56, 54, 75): 'rabbitmq_exceptions', (55, 20, 55, 33): 'publish_event', (56, 20, 56, 29): 'publisher', (56, 31, 56, 45): 'TICK_5_MINUTES', (56, 47, 56, 54): 'network', (56, 56, 56, 60): 'secs', (56, 62, 56, 66): 'mins'}, {}), '(rabbitmq_exceptions, publish_event,\n    publisher, TICK_5_MINUTES, network, secs, mins)', False, 'from infcommon import utils\n'), ((59, 16, 61, 68), 'infcommon.utils.do_stuff_with_exponential_backoff', 'utils.do_stuff_with_exponential_backoff', ({(59, 56, 59, 75): 'rabbitmq_exceptions', (60, 20, 60, 33): 'publish_event', (61, 20, 61, 29): 'publisher', (61, 31, 61, 46): 'TICK_60_MINUTES', (61, 48, 61, 55): 'network', (61, 57, 61, 61): 'secs', (61, 63, 61, 67): 'mins'}, {}), '(rabbitmq_exceptions, publish_event,\n    publisher, TICK_60_MINUTES, network, secs, mins)', False, 'from infcommon import utils\n')]