def auto_start(): while not _quit: now = time.time() cnt = 0 for each in accountdb.scan('PENDING'): if each['nextime'] <= now: cnt += 1 if cnt > 5: break gevent.spawn(run_task, each) time.sleep(60)
def web_app(environ, start_response): request = Request(environ) if request.path == '/': template = ('<html><body>' '<form method=POST action="/invite">invite <input name=id /> ' 'count<input name=count value=10 /> name<input name=name /><input type=submit /></form>' '<form method=POST action="/add">add <input name="id" /><input name="pwd" /> ' 'group:<input name=group /><input type=submit /></form>' '<pre>%s</pre></body></html>') content = [] content.append('<h1>RUNNING</h1><hr />') now = time.time() for cur in accountdb.scan('RUNNING'): cur['mintime'] = datetime.datetime.fromtimestamp(cur['intime']) cur['mnextime'] = cur['nextime'] - time.time() content.append('%(mintime)s <a href="/log?id=%(id)s">%(id)s</a>:%(invite)s %(name)s lv:%(lv)s ' 'rounds:%(rounds)s <a href="/stop?id=%(id)s">stop</a>' % cur) content.append('<h1>PENDING</h1><hr />') for cur in accountdb.scan('PENDING'): cur['mintime'] = datetime.datetime.fromtimestamp(cur['intime']) cur['mnextime'] = cur['nextime'] - time.time() content.append('%(mintime)s <a href="/log?id=%(id)s">%(id)s</a>:%(invite)s %(name)s lv:%(lv)s ' 'rounds:%(rounds)s %(mnextime)s <a href="/run?id=%(id)s&done=0">run</a> ' '<a href="/rm?id=%(id)s">del</a>' % cur) content.append('<h1>DONE</h1><hr />') done = [] for cur in accountdb.scan('DONE'): cur['mintime'] = datetime.datetime.fromtimestamp(cur['intime']) cur['mnextime'] = cur['nextime'] - time.time() content.append('%(mintime)s <a href="/log?id=%(id)s">%(id)s</a>:%(invite)s %(name)s lv:%(lv)s ' 'rounds:%(rounds)s' % cur) content.append(' '.join(done)) content.append('<h1>FAILED</h1><hr />') for cur in accountdb.scan('FAILED'): cur['mintime'] = datetime.datetime.fromtimestamp(cur['intime']) cur['mnextime'] = cur['nextime'] - time.time() content.append('%(mintime)s <a href="/log?id=%(id)s">%(id)s</a>:%(invite)s %(name)s lv:%(lv)s ' 'rounds:%(rounds)s <a href="/run?id=%(id)s&done=0">run</a>' % cur) # return start_response("200 OK", [("Content-Type", "text/html")]) return template % '\n'.join([x.encode('utf8') for x in content]) elif request.path == '/add': _id = request.POST['id'] pwd = request.POST['pwd'] group = request.POST['group'] accountdb.add(_id, pwd, group=group) start_response("302 FOUND", [("Location", "/")]) return 'redirect' elif request.path == '/invite': invitation_id = request.POST['id'] count = request.POST['count'] name_prefix = request.POST.get('name') def do(invitation_id, count): for _ in range(count): try: ret = regist.all_in_one(invitation_id, name_prefix) except Exception, e: ret = [e, ] finally: message = ' '.join([str(x) for x in ret]) print message with open('/tmp/libma.%s.log' % invitation_id, 'a') as fp: fp.write(message) fp.write('\n')
else: start_response("302 FOUND", [("Location", "/")]) return 'redirect' elif request.path == '/log': _id = request.GET['id'] try: with open('/tmp/libma.%s.log' % _id, 'r') as fp: start_response("200 OK", [("Content-Type", "text/plain")]) return fp.readlines() except IOError, e: start_response("404 NOT FOUND", []) return '404' elif request.path == '/quit': global _quit _quit = True for each in running_set: stop_set.add(each) start_response("302 FOUND", [("Location", "/")]) return 'redirect' else: start_response("404 NOT FOUND", []) return '404' if __name__ == '__main__': for each in accountdb.scan('RUNNING'): accountdb.update(each['id'], status='PENDING') server = gevent.pywsgi.WSGIServer(("", 8888), web_app) gevent.spawn(auto_start) server.serve_forever()