def leaderboard(leaderboard_id=None, name=None): """Returns Leaderboard if find """ if leaderboard_id: lb = Thing('leaderboard').find(leaderboard_id) elif name: lb = Thing('leaderboard').find_by_name(name) if lb: return Leaderboard(lb)
def fresh_job(self, job, error): try: if job.event_type == 'at': Thing('job').delete(job) return job.fresh() Thing('job').save(job) except Exception as e: LOGGER.error('Fresh job failed :%s', job)
def _work(self, job): LOGGER.info('Woking job: %s', job) error = False try: lb = Thing('leaderboard').find_by_name(job.name) if not lb: LOGGER.warning('Possiable leaderboard <%s> doesn\'t exists', job.name) return entrything = EntryThing(lb.adapter) if entrything: entrything.sort(lb.leaderboard_id) except Exception as e: cls, e, tb = exc_info() LOGGER.error('Leaderboard Cron job failed, unhandle error:%s', e) error = True self.fresh_job(job, error)
def setdebug(debug=False): level = logging.DEBUG if debug else logging.INFO logging.basicConfig(level=level, format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') if __name__ == '__main__': setdebug(False) db.setup('localhost', 'test', 'test', 'nagi', pool_opt={ 'minconn': 3, 'maxconn': 10 }) thing_setup() next_run = datetime.now() - timedelta(minutes=10) for cron_id in range(4, 11): name = 'name_' + str(cron_id) job = Job(cron_id, None, name, event='every 2', next_run=next_run) Thing('job').save(job) time.sleep(2) try: cron = Cron(5) cron.run() except KeyboardInterrupt: exit(1)
def test_add_and_cancel_job(self): cron = Cron() cron.add_job('cron_job', 'every 5') self.assertEqual(Thing('job').find('cron_job').name, 'cron_job') cron.cancel_job('cron_job') self.assertEqual(Thing('job').find('cron_job'), None)
from nagi import db from nagi.leaderboard import leaderboard from nagi.thing import thing_setup, Thing from nagi.model import Entry # setup db setting # pool_opt sets the db pool min connections and max connections db.setup('localhost', 'test', 'test', 'nagi', pool_opt={'minconn': 3, 'maxconn': 10}) # setup thing_setup, initilaize the thing_setup bind the data-mapper thing_setup() use the leaderboard api lb = leaderboard(leaderboard_id=2) # find leaderboard by leaderboard_id lb = leaderboard(name='name') # load leaderboard by name lb.rank_for_user(12) # rank a user by user id lb.rank_for_users([12, 2]) # rank users by users ################### DB AND Model # ################### # Entry and DB backed entry = Entry(23, 2, 23) Thing('entry').save(entry) entry = Thing('entry').find(2, 23) print entry
def cancel_job(self, name): job = Thing('job').find(name) if job: Thing('job').delete(job)
def add_job(self, name, event): job = Job(None, None, name, event) return Thing('job').save(job)
def claim(self): self.job_id = self.gen_job_id() db.execute( 'UPDATE cron SET job_id=%s WHERE job_id IS NULL AND next_run <= %s LIMIT %s', (self.job_id, datetime.now(), self.limit)) return Thing('job').find_by_job_id(self.job_id)