#!python
import time

import importlib
import pkgutil

from startme import StartMe, meta
import startme.mods

def iter_namespace(ns_pkg):
    # Specifying the second argument (prefix) to iter_modules makes the
    # returned name an absolute name instead of a relative one. This allows
    # import_module to work without having to do additional modification to
    # the name.
    return pkgutil.iter_modules(ns_pkg.__path__, ns_pkg.__name__ + ".")


def main():

    mods = {
        name: importlib.import_module(name)
        for finder, name, ispkg
        in iter_namespace(startme.mods)
    }

    smjobs = [ cls() for cls in meta.__inheritors__ ]

    #
    # on_start
    #
    for j in smjobs:
        j.on_start()

    #
    # prepare schedule
    # 

    sch = {
        j: j.reschedule()
        for j in smjobs 
    }

    #
    # loop over schedule
    #

    while True:
        sch = {k:v for k,v in sch.items() if v is not None}
        if not sch:
            return 

        now = time.time()
        for j, jtime in sch.items():
            if  now>=jtime:
                j.on_schedule()
                sch[j] = j.reschedule()


        exectime = sch[min(sch, key=sch.get)]
        time.sleep(exectime - time.time())


main()