Add approval robot.

This commit is contained in:
zhuyj17 2016-09-25 21:06:46 +08:00
parent 8dfe21acd4
commit f24f38ead4
3 changed files with 31 additions and 3 deletions

View File

@ -1,3 +1,4 @@
import threading,datetime,random,time
from model import db,User,ApplyMsg from model import db,User,ApplyMsg
from userManager import administration_required from userManager import administration_required
@ -54,3 +55,28 @@ class ApplicationMgr:
applymsg.status = "Rejected" applymsg.status = "Rejected"
db.session.commit() db.session.commit()
return {"success":"true"} return {"success":"true"}
class ApprovalRobot(threading.Thread):
def __init__(self,maxtime=3600):
threading.Thread.__init__(self)
self.stop = False
self.interval = 20
self.maxtime = maxtime
def stop(self):
self.stop = True
def run(self):
while not self.stop:
applymsgs = ApplyMsg.query.filter_by(status="Processing").all()
for msg in applymsgs:
secs = (datetime.datetime.now() - msg.time).seconds
ranint = random.randint(self.interval,self.maxtime)
if secs >= ranint:
msg.status = "Agreed"
user = User.query.filter_by(username=msg.username).first()
if user is not None:
user.beans += msg.number
db.session.commit()
time.sleep(self.interval)

View File

@ -998,6 +998,8 @@ if __name__ == '__main__':
master_collector.start() master_collector.start()
logger.info("master_collector started") logger.info("master_collector started")
G_applicationmgr = beansapplicationmgr.ApplicationMgr() G_applicationmgr = beansapplicationmgr.ApplicationMgr()
approvalrbt = beansapplicationmgr.ApprovalRobot(120)
approvalrbt.start()
# server = http.server.HTTPServer((masterip, masterport), DockletHttpHandler) # server = http.server.HTTPServer((masterip, masterport), DockletHttpHandler)
logger.info("starting master server") logger.info("starting master server")

View File

@ -247,14 +247,14 @@ class ApplyMsg(db.Model):
number = db.Column(db.Integer) number = db.Column(db.Integer)
reason = db.Column(db.String(600)) reason = db.Column(db.String(600))
status = db.Column(db.String(10)) status = db.Column(db.String(10))
time = db.Column(db.String(10)) time = db.Column(db.DateTime(10))
def __init__(self,username, number, reason): def __init__(self,username, number, reason):
self.username = username self.username = username
self.number = number self.number = number
self.reason = reason self.reason = reason
self.status = "Processing" self.status = "Processing"
self.time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") self.time = datetime.now()
def __repr__(self): def __repr__(self):
return "{\"id\":\"%d\", \"username\":\"%s\", \"number\": \"%d\", \"reason\":\"%s\", \"status\":\"%s\", \"time\":\"%s\"}" % (self.id, self.username, self.number, self.reason, self.status, self.time) return "{\"id\":\"%d\", \"username\":\"%s\", \"number\": \"%d\", \"reason\":\"%s\", \"status\":\"%s\", \"time\":\"%s\"}" % (self.id, self.username, self.number, self.reason, self.status, self.time.strftime("%Y-%m-%d %H:%M:%S"))