#!/usr/bin/env python3
#
# Run a test.  Just the test spec is provided on stdin.
#

import datetime
import json
import sys
import time

import pscheduler

logger = pscheduler.Log(prefix='tool-sleepbg', quiet=True)

input = pscheduler.json_load(exit_on_error=True);

# TODO: Validate the input

try:
    duration_iso = input['test']['spec']['duration']
except KeyError:
    pscheduler.fail('Unable to find duration in input')

duration = pscheduler.iso8601_as_timedelta(duration_iso)
if duration is None:
    pscheduler.fail_other(2, "Missing or invalid duration " + duration_iso)


try:
    interval_iso = input['test']['spec']['interval']
except KeyError:
    interval_iso = "PT1S"

interval = pscheduler.iso8601_as_timedelta(interval_iso)
if interval is None:
    pscheduler.fail_other(2, "Missing or invalid interval " + interval_iso)



# Perform the test

try:
    start_at = input['schedule']['start']
    logger.debug("Sleeping until %s", start_at)
    pscheduler.sleep_until(start_at)
    logger.debug("Starting")
except KeyError:
    pscheduler.fail("Unable to find start time in input")


# The output is just mocked up since there's no real tool being run.

results = {
    "succeeded": True,
    "time-slept": interval_iso
}


interval_secs = pscheduler.timedelta_as_seconds(interval)
end_time = pscheduler.time_now() + duration
next_output = pscheduler.time_now() + interval

emitter = pscheduler.RFC7464Emitter(sys.stdout)

while next_output < end_time:
    logger.debug("Sleeping %d", interval_secs)
    time.sleep(interval_secs)
    emitter(results)
    next_output += interval
    logger.debug("Wrote result")

pscheduler.succeed()
