Example #1
0
    def check_out(s, next_api_time=None):
        if next_api_time is None:
            next_api_time = sqm.now()

        bots = (s.session.query(s.sqlBotInfo).filter(
            s.sqlBotInfo.last_seen > sqm.now() -
            sqm.secs(s.fallout_secs)).order_by(s.sqlBotInfo.name))

        idx = [i for i in range(len(list(bots))) if s.name == bots[i].name][0]

        next_run = (idx + 1) % len(list(bots))

        s.session.merge(
            s.sqlGlobalInfo(id=1,
                            next_run=next_run,
                            next_api_time=next_api_time))
        s.session.commit()
Example #2
0
    def is_my_turn(s):
        #Make sure you set the time every time you check
        s.check_in()

        bots = (s.session.query(s.sqlBotInfo).filter(
            s.sqlBotInfo.last_seen > sqm.now() -
            sqm.secs(s.fallout_secs)).order_by(s.sqlBotInfo.name))

        idx = [i for i in range(len(list(bots))) if s.name == bots[i].name][0]

        next_run = (s.session.query(
            s.sqlGlobalInfo).filter(s.sqlGlobalInfo.id == 1))[0].next_run

        if idx == next_run % len(list(bots)):
            return True
        return False
Example #3
0
    def __init__(s, name=None):
        if name is None:
            name = uuid.uuid4().hex

        s.name = name
        s.engine = engine = sq.create_engine('sqlite:///botinfo.db',
                                             echo=False)
        s.Base.metadata.create_all(s.engine, checkfirst=True)
        Session = sq.orm.sessionmaker(bind=engine)
        s.session = Session()
        s.remove_dead()
        s.check_in()

        globinfo = (s.session.query(
            s.sqlGlobalInfo).filter(s.sqlGlobalInfo.id == 1))
        if not len(list(globinfo)):
            s.session.merge(
                s.sqlGlobalInfo(id=1, next_run=0, next_api_time=sqm.now()))
        s.session.commit()
Example #4
0
 def check_in(s):
     s.session.merge(s.sqlBotInfo(name=s.name, last_seen=sqm.now()))
     s.session.commit()
Example #5
0
 def remove_dead(s):
     for bot in (s.session.query(
             s.sqlBotInfo).filter(s.sqlBotInfo.last_seen < sqm.now() -
                                  sqm.secs(s.fallout_secs))):
         s.session.delete(bot)
     s.session.commit()
Example #6
0
 def is_api_ready(s):
     return (s.session.query(s.sqlGlobalInfo).filter(
         s.sqlGlobalInfo.id == 1)[0].next_api_time <= sqm.now())