Exemplo n.º 1
0
def process_periodic_calls():
    """Long running thread processing next periodic calls

    :return:
    """
    while True:
        calls_to_process = get_next_periodic_calls()
        while not calls_to_process:
            calls = api.get_periodic_calls()

            now = datetime.datetime.now()
            nearest = min([c.execution_time for c in calls])

            time_to_sleep = (nearest - now).total_seconds()
            time_to_sleep = time_to_sleep if time_to_sleep > 0 else 1

            LOG.debug("Sleeping for %s s..." % time_to_sleep)

            time.sleep(time_to_sleep)
            calls_to_process = get_next_periodic_calls()

        for call in calls_to_process:
            func = utils.import_class(call.target_method)
            arguments = json.loads(call.arguments)

            api.update_periodic_call(
                call.name,
                {
                    'execution_time': get_next_time(
                        call.pattern, call.execution_time
                    ),
                    'processing': True
                }
            )
            SEMAPHORES[call.id].acquire()
            t = eventlet.spawn(func, **arguments)
            t.link(end_processing, call)