def claim(self, limit, age=None): age= age or datetime.now() task_id = gen_task_id() db.execute('UPDATE cron SET task_id=%s, status=%s WHERE task_id IS NULL and next_run<=%s and status<%s LIMIT %s', (task_id, Task.RUNNING, age, Task.RUNNING, limit)) tasks = db.query('SELECT * FROM cron WHERE task_id=%s', (task_id,)) return [self._load(task) for task in tasks]
def setUp(self): setattr(db, '__connections', {}) db.setup('localhost', 'test', 'test', 'test', pool_opt={ 'minconn': 3, 'maxconn': 10 }, key='test') db.setup('localhost', 'test', 'test', 'test', pool_opt={ 'minconn': 3, 'maxconn': 10 }, key='test', slave=True) db.execute('DROP TABLE IF EXISTS `users`', key='test') res = db.execute("""CREATE TABLE `users` ( `uid` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, PRIMARY KEY (`uid`))""", key='test') rows = [] for _ in range(1, 10): rows.append('(%d , "name_%d")' % (_, _)) db.execute('INSERT INTO users VALUES ' + ', '.join(rows), key='test')
def claim(self, limit, age=None): age = age or datetime.now() task_id = gen_task_id() db.execute( 'UPDATE cron SET task_id=%s, status=%s WHERE task_id IS NULL and next_run<=%s and status<%s LIMIT %s', (task_id, Task.RUNNING, age, Task.RUNNING, limit)) tasks = db.query('SELECT * FROM cron WHERE task_id=%s', (task_id, )) return [self._load(task) for task in tasks]
def setUp(self): setattr(db, '__connections', {}) db.setup('localhost', 'test', 'test', 'eden', pool_opt={'minconn': 3, 'maxconn': 10}) self.user = User('username', 'email', 'real_name', 'password', 'active') db.execute('DELETE FROM users WHERE email=%s or email=%s', (self.user.email, 'email2')) Backend('user').save(self.user) self.uid = db.query_one('SELECT uid FROM users WHERE email=%s', (self.user.email,))[0]
def setUp(self): setattr(db, '__connections', {}) db.setup('localhost', 'test', 'test', 'test', pool_opt={'minconn': 3, 'maxconn': 10}, key='test') db.setup('localhost', 'test', 'test', 'test', pool_opt={'minconn': 3, 'maxconn': 10}, key='test', slave=True) db.execute('DROP TABLE IF EXISTS `users`', key='test') res = db.execute("""CREATE TABLE `users` ( `uid` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, PRIMARY KEY (`uid`))""", key='test') rows = [] for _ in range(1, 10): rows.append('(%d , "name_%d")' % (_, _)) db.execute('INSERT INTO users VALUES ' + ', '.join(rows), key='test')
def save(self, task): if task.data is not None: data = json_encode(task.data) else: data = None last_five_logs = json_encode(task.last_five_logs) if task.cron_id is None: return db.execute('INSERT INTO cron(task_id, name, action, data, event, next_run, last_run, run_times, attempts, status, created, last_five_logs) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', (task.task_id, task.name, task.action, data, task.event, task.next_run, task.last_run, task.run_times, task.attempts, task.status, task.created, last_five_logs)) return db.execute('INSERT INTO cron(task_id, name, action, data, event, next_run, last_run, run_times, attempts, status, created, last_five_logs) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) \ ON DUPLICATE KEY UPDATE cron_id=VALUES(cron_id), task_id=VALUES(task_id), event=VALUES(event), next_run=VALUES(next_run), \ last_run=VALUES(last_run), action=VALUES(action), data=VALUES(data),run_times=VALUES(run_times), attempts=VALUES(attempts), status=VALUES(status), last_five_logs=VALUES(last_five_logs)', (task.task_id, task.name, task.action, data, task.event, task.next_run, task.last_run, task.run_times, task.attempts, task.status, task.created, last_five_logs))
def save(self, user): return db.execute( "INSERT INTO users(uid, username, email, real_name, password, status, role, created) \ VALUES(%s, %s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE \ email=VALUES(email), password=VALUES(password), status=VALUES(status), role=VALUES(role), real_name=VALUES(real_name)", (user.uid, user.username, user.email, user.real_name, user.password, user.status, user.role, user.created))
def setUp(self): setattr(db, '__connections', {}) db.setup('localhost', 'test', 'test', 'eden', pool_opt={ 'minconn': 3, 'maxconn': 10 }) self.user = User('username', 'email', 'real_name', 'password', 'active') db.execute('DELETE FROM users WHERE email=%s or email=%s', (self.user.email, 'email2')) Backend('user').save(self.user) self.uid = db.query_one('SELECT uid FROM users WHERE email=%s', (self.user.email, ))[0]
def save(self, task): if task.data is not None: data = json_encode(task.data) else: data = None last_five_logs = json_encode(task.last_five_logs) if task.cron_id is None: return db.execute( 'INSERT INTO cron(task_id, name, action, data, event, next_run, last_run, run_times, attempts, status, created, last_five_logs) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', (task.task_id, task.name, task.action, data, task.event, task.next_run, task.last_run, task.run_times, task.attempts, task.status, task.created, last_five_logs)) return db.execute( 'INSERT INTO cron(task_id, name, action, data, event, next_run, last_run, run_times, attempts, status, created, last_five_logs) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) \ ON DUPLICATE KEY UPDATE cron_id=VALUES(cron_id), task_id=VALUES(task_id), event=VALUES(event), next_run=VALUES(next_run), \ last_run=VALUES(last_run), action=VALUES(action), data=VALUES(data),run_times=VALUES(run_times), attempts=VALUES(attempts), status=VALUES(status), last_five_logs=VALUES(last_five_logs)', (task.task_id, task.name, task.action, data, task.event, task.next_run, task.last_run, task.run_times, task.attempts, task.status, task.created, last_five_logs))
def delete_by_name(self, name): return db.execute('DELETE FROM cron WHERE name=%s', (name,))
def delete_by_task_id(self, task_id): return db.execute('DELETE FROM cron WHERE task_id=%s', (task_id,))
def delete_by_task_id(self, task_id): return db.execute('DELETE FROM cron WHERE task_id=%s', (task_id, ))
def delete_by_name(self, name): return db.execute('DELETE FROM cron WHERE name=%s', (name, ))
def delete(self, task): return db.execute('DELETE FROM cron WHERE cron_id=%s', (task.cron_id, ))
def tearDown(self): db.execute('DELETE FROM users')
def test_excute(self): res = db.execute('insert into users values(%s, %s)', [(100L, 'thomas'), (101L, 'animer')]) res = db.query('SELECT count(*) FROM users WHERE uid>=100') self.assertEqual(2, res[0][0])
def delete(self, task): return db.execute('DELETE FROM cron WHERE cron_id=%s', (task.cron_id,))
def save(self, user): return db.execute("INSERT INTO users(uid, username, email, real_name, password, status, role, created) \ VALUES(%s, %s, %s, %s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE \ email=VALUES(email), password=VALUES(password), status=VALUES(status), role=VALUES(role), real_name=VALUES(real_name)", (user.uid, user.username, user.email, user.real_name, user.password, user.status, user.role, user.created))
def tearDown(self): db.execute('DELETE FROM users', key='test')
def delete(self, user): return db.execute('DELETE FROM users WHERE uid=%s', (user.uid,))
def delete(self, user): return db.execute('DELETE FROM users WHERE uid=%s', (user.uid, ))
datefmt='%Y-%m-%d %H:%M:%S', filemode='a+') setdebug(False) db.setup('localhost', 'test', 'test', 'eden', pool_opt={ 'minconn': 3, 'maxconn': 10 }) app = App() app.add_task('task.test', get_date) scheduler = Scheduler(app, 20, 20, 100) db.execute('delete from cron') for i in range(100): if i % 2 == 0: print i action = 'task.not_found' else: action = 'task.test' scheduler.add_task('name_%d' % (i), 'every 2', action, datetime.now(), 'http://www.google.com', session=i) scheduler.run()
def clear_timeout_task(self, when): db.execute( 'UPDATE cron set task_id=NULL, status=%s WHERE task_id IS NOT NULL AND next_run<%s', (Task.SCHEDULED, when))
def get_date(url, session='xxx'): date = None try: r = urllib2.urlopen(url) date = r.info().dict['date'] except: LOGGER.info('open failed') LOGGER.info('session: %s, date:%s,', session, date) 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+') setdebug(False) db.setup('localhost', 'test', 'test', 'eden', pool_opt={'minconn': 3, 'maxconn': 10}) app = App() app.add_task('task.test', get_date) scheduler = Scheduler(app, 20, 20, 100) db.execute('delete from cron') for i in range(100): if i % 2 == 0: print i action = 'task.not_found' else: action = 'task.test' scheduler.add_task('name_%d' %(i), 'every 2', action, datetime.now(), 'http://www.google.com', session=i) scheduler.run()
def clear_timeout_task(self, when): db.execute('UPDATE cron set task_id=NULL, status=%s WHERE task_id IS NOT NULL AND next_run<%s', (Task.SCHEDULED, when))