예제 #1
0
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)
예제 #2
0
파일: cron.py 프로젝트: zhuyinhua/Nagi
 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)
예제 #3
0
파일: cron.py 프로젝트: zhuyinhua/Nagi
 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)
예제 #4
0
파일: cron.py 프로젝트: zhuyinhua/Nagi
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)
예제 #5
0
 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)
예제 #6
0
파일: hello.py 프로젝트: zhuyinhua/Nagi
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

예제 #7
0
파일: cron.py 프로젝트: zhuyinhua/Nagi
 def cancel_job(self, name):
     job = Thing('job').find(name)
     if job:
         Thing('job').delete(job)
예제 #8
0
파일: cron.py 프로젝트: zhuyinhua/Nagi
 def add_job(self, name, event):
     job = Job(None, None, name, event)
     return Thing('job').save(job)
예제 #9
0
파일: cron.py 프로젝트: zhuyinhua/Nagi
 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)