from worker import writer, scanTemplates
from utils.manager import TrainManager
import time
from db import engine
from sqlmodel import Session
from models import Logrun,Usefuel, Template
import cachetool
import inspect
from disclog import postLog,postGeneric

def filler(posrr,posm) -> str:
    start=time.time()
    
    manager = TrainManager(posrr=posrr,
                           posm=posm)
    run = True

    postGeneric([("info","API init success! Logger started.")],"Startup")
  
    while run:
        try:
            manager.fetch()
            if len(manager.out) > 0:

                writer.delay(manager.out,"action")
                manager.out = []

        except Exception as e:
            postLog(e,"error",f"{inspect.stack()[0][3]}:{inspect.stack()[0][2]}")
            time.sleep(30)

    return f"{(time.time()-start)} total time"


if __name__ == "__main__":
    startup = True

    while startup:
        try:
            with Session(engine) as session:
                
                toptemp = session.query(Template).order_by(Template.template_id.desc()).first()
                posrrr = session.query(Logrun).order_by(Logrun.action_seq.desc()).first()
                posmr = session.query(Usefuel).order_by(Usefuel.action_seq.desc()).first()
            
            if toptemp:
                print("skipping init")
            else:
                cachetool.set_cache(f"last_templates",1622316652000)
                cachetool.set_cache(f"last_assets",1622316652000)
                scanTemplates()
                time.sleep(1200)
                
            if posrrr: posrr = posrrr.action_seq
            else: posrr = 1642127
            if posmr: posm = posmr.action_seq
            else: posm = 981927   

            filler(posrr,posm)
        except Exception as e:
                
                postLog(e,"warn",f"{inspect.stack()[0][3]}:{inspect.stack()[0][2]}")
                time.sleep(30)
sqlmodel.Session models.Logrun.action_seq.desc worker.writer.delay worker.scanTemplates models.Template.template_id.desc time.sleep disclog.postGeneric time.time models.Usefuel.action_seq.desc utils.manager.TrainManager cachetool.set_cache inspect.stack
[((301, 312), 'time.time', 'time.time', ([], {}), '()\n', (310, 312), False, 'import time\n'), ((332, 368), 'utils.manager.TrainManager', 'TrainManager', ([], {'posrr': 'posrr', 'posm': 'posm'}), '(posrr=posrr, posm=posm)\n', (344, 368), False, 'from utils.manager import TrainManager\n'), ((416, 487), 'disclog.postGeneric', 'postGeneric', (["[('info', 'API init success! Logger started.')]", '"""Startup"""'], {}), "([('info', 'API init success! Logger started.')], 'Startup')\n", (427, 487), False, 'from disclog import postLog, postGeneric\n'), ((599, 634), 'worker.writer.delay', 'writer.delay', (['manager.out', '"""action"""'], {}), "(manager.out, 'action')\n", (611, 634), False, 'from worker import writer, scanTemplates\n'), ((793, 807), 'time.sleep', 'time.sleep', (['(30)'], {}), '(30)\n', (803, 807), False, 'import time\n'), ((824, 835), 'time.time', 'time.time', ([], {}), '()\n', (833, 835), False, 'import time\n'), ((954, 969), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (961, 969), False, 'from sqlmodel import Session\n'), ((1386, 1439), 'cachetool.set_cache', 'cachetool.set_cache', (['f"""last_templates"""', '(1622316652000)'], {}), "(f'last_templates', 1622316652000)\n", (1405, 1439), False, 'import cachetool\n'), ((1455, 1505), 'cachetool.set_cache', 'cachetool.set_cache', (['f"""last_assets"""', '(1622316652000)'], {}), "(f'last_assets', 1622316652000)\n", (1474, 1505), False, 'import cachetool\n'), ((1521, 1536), 'worker.scanTemplates', 'scanTemplates', ([], {}), '()\n', (1534, 1536), False, 'from worker import writer, scanTemplates\n'), ((1553, 1569), 'time.sleep', 'time.sleep', (['(1200)'], {}), '(1200)\n', (1563, 1569), False, 'import time\n'), ((1932, 1946), 'time.sleep', 'time.sleep', (['(30)'], {}), '(30)\n', (1942, 1946), False, 'import time\n'), ((1058, 1085), 'models.Template.template_id.desc', 'Template.template_id.desc', ([], {}), '()\n', (1083, 1085), False, 'from models import Logrun, Usefuel, Template\n'), ((1151, 1175), 'models.Logrun.action_seq.desc', 'Logrun.action_seq.desc', ([], {}), '()\n', (1173, 1175), False, 'from models import Logrun, Usefuel, Template\n'), ((1241, 1266), 'models.Usefuel.action_seq.desc', 'Usefuel.action_seq.desc', ([], {}), '()\n', (1264, 1266), False, 'from models import Logrun, Usefuel, Template\n'), ((732, 747), 'inspect.stack', 'inspect.stack', ([], {}), '()\n', (745, 747), False, 'import inspect\n'), ((756, 771), 'inspect.stack', 'inspect.stack', ([], {}), '()\n', (769, 771), False, 'import inspect\n'), ((1867, 1882), 'inspect.stack', 'inspect.stack', ([], {}), '()\n', (1880, 1882), False, 'import inspect\n'), ((1891, 1906), 'inspect.stack', 'inspect.stack', ([], {}), '()\n', (1904, 1906), False, 'import inspect\n')]