from config import config
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from time import sleep
import docker
import os


configuration = config[os.environ.get('NOPAQUE_CONFIG', 'development')]
configuration.init()
docker_client = docker.from_env()
engine = create_engine(configuration.SQLALCHEMY_DATABASE_URI)
Session = scoped_session(sessionmaker(bind=engine))


def run():
    from .tasks.check_corpora import check_corpora
    check_corpora_thread = check_corpora()
    from .tasks.check_jobs import check_jobs
    check_jobs_thread = check_jobs()
    from .tasks.notify import notify
    notify_thread = notify()

    while True:
        if not check_corpora_thread.is_alive():
            check_corpora_thread = check_corpora()
        if not check_jobs_thread.is_alive():
            check_jobs_thread = check_jobs()
        if not notify_thread.is_alive():
            notify_thread = notify()
        sleep(3)