From 9c7deafa61c14d912d79527696b3197aefe62c5c Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Sun, 18 Oct 2015 22:34:55 +0800 Subject: [PATCH 01/20] adding python files --- ojbackend/ReadMe | 2 + ojbackend/ReadMe~ | 2 + ojbackend/curl_submit.py~ | 1 + ojbackend/dispatcher.log~ | 25 +++++++++++ ojbackend/dispatcher.py | 78 +++++++++++++++++++++++++++++++++ ojbackend/dispatcher.py~ | 81 +++++++++++++++++++++++++++++++++++ ojbackend/dispatcherFunc.py | 76 ++++++++++++++++++++++++++++++++ ojbackend/dispatcherFunc.py~ | 76 ++++++++++++++++++++++++++++++++ ojbackend/install.py | 40 +++++++++++++++++ ojbackend/install.py~ | 40 +++++++++++++++++ ojbackend/machine.config~ | 1 + ojbackend/machineInfo.config~ | 1 + ojbackend/ojdatabase.config~ | 4 ++ ojbackend/test.py~ | 13 ++++++ 14 files changed, 440 insertions(+) create mode 100644 ojbackend/ReadMe create mode 100644 ojbackend/ReadMe~ create mode 100644 ojbackend/curl_submit.py~ create mode 100644 ojbackend/dispatcher.log~ create mode 100644 ojbackend/dispatcher.py create mode 100644 ojbackend/dispatcher.py~ create mode 100644 ojbackend/dispatcherFunc.py create mode 100644 ojbackend/dispatcherFunc.py~ create mode 100644 ojbackend/install.py create mode 100644 ojbackend/install.py~ create mode 100644 ojbackend/machine.config~ create mode 100644 ojbackend/machineInfo.config~ create mode 100644 ojbackend/ojdatabase.config~ create mode 100644 ojbackend/test.py~ diff --git a/ojbackend/ReadMe b/ojbackend/ReadMe new file mode 100644 index 0000000..e121ab1 --- /dev/null +++ b/ojbackend/ReadMe @@ -0,0 +1,2 @@ +1. execute install.py to set up machine.config and ojdatase.config +2. execute dispatcher.py as daemon and it will check if there are new submissions need to be judged diff --git a/ojbackend/ReadMe~ b/ojbackend/ReadMe~ new file mode 100644 index 0000000..88bf771 --- /dev/null +++ b/ojbackend/ReadMe~ @@ -0,0 +1,2 @@ +1. execute install.py to set up machine.config and ojdatase.config +2. execute dispatcher.py as daemon diff --git a/ojbackend/curl_submit.py~ b/ojbackend/curl_submit.py~ new file mode 100644 index 0000000..92c61b1 --- /dev/null +++ b/ojbackend/curl_submit.py~ @@ -0,0 +1 @@ +print "HELLO WORLD" diff --git a/ojbackend/dispatcher.log~ b/ojbackend/dispatcher.log~ new file mode 100644 index 0000000..a7aec23 --- /dev/null +++ b/ojbackend/dispatcher.log~ @@ -0,0 +1,25 @@ +2015-10-09 22:27:04,177 ==========Dispatcher Started========== +2015-10-09 22:27:04,182 getMachine Success +2015-10-09 22:27:04,182 connect to database +2015-10-09 22:27:04,193 sidQuery Success! +2015-10-09 22:27:04,193 sid = 10, pid = 2 +2015-10-09 22:27:04,193 pidQuery Success! +2015-10-09 22:27:04,193 judgeSource = LOCAL, judgeType = LOCAL_NORMAL, judgeLanguage = CPP +2015-10-09 22:27:04,193 get idleMachine +2015-10-09 22:27:05,194 idleMachine = judge1 +2015-10-09 22:27:05,227 get judgeURL = 127.0.0.1/interface.py +2015-10-09 22:29:21,094 ==========Dispatcher Started========== :: +2015-10-09 22:29:21,100 getMachine Success :: +2015-10-09 22:29:21,100 connect to database :: +2015-10-09 22:29:48,812 ::==========Dispatcher Started========== +2015-10-09 22:29:48,817 ::getMachine Success +2015-10-09 22:29:48,817 ::connect to database +2015-10-09 22:31:37,054 ::==========Dispatcher Started========== +2015-10-09 22:31:37,060 ::getMachine Success +2015-10-09 22:31:37,060 ::connect to database +2015-10-09 22:32:51,215 ::==========Dispatcher Started========== +2015-10-09 22:32:51,219 ::getMachine Success +2015-10-09 22:32:51,219 ::connect to database +2015-10-09 22:33:16,031 ::==========Dispatcher Started========== +2015-10-09 22:33:16,038 ::getMachine Success +2015-10-09 22:33:16,038 ::connect to database diff --git a/ojbackend/dispatcher.py b/ojbackend/dispatcher.py new file mode 100644 index 0000000..15606bf --- /dev/null +++ b/ojbackend/dispatcher.py @@ -0,0 +1,78 @@ +""" + dispatcher.py + This process continuously checks whether there exist unjudged submissions. +""" + +from dispatcherFunc import * +import time +import logging +import subprocess, sys +logging.basicConfig(filename = 'dispatcher.log', level = logging.INFO, format = '%(asctime)s ::%(message)s',datafmt = '%m/%d/%Y %I:%M:%S %p') +logging.info('==========Dispatcher Started==========') + +machineInfo = getMachine() +if machineInfo != None: + initMachine(machineInfo) + logging.info('getMachine Success') +else: + logging.info('getMachine Error') + logging.info('Please check the file machineInfo.config to check the settings') + logging.info('==========Dispatcher Finished==========') + exit(0) + +dbIP, dbUser, dbPasswd, dbName = getdbInfo() +if dbIP == None or dbUser == None or dbPasswd == None or dbName == None: + logging.info('getdbInfo Error') + logging.info('Please check the settings in ojdatabase.config') + logging.info('==========Dispatcher Finished==========') + exit(0) +logging.info('connect to database') + +while True: + + DB = connectDB(dbIP, dbUser, dbPasswd, dbName) + if DB == None: + logging.info('connect database error') + logging.info('Please check the settings in ojdatabase.config') + logging.info('==========Dispatcher Finished==========') + exit(0) + cur = DB.cursor() + cur.execute("USE nthuoj;") + cur.execute("SELECT * FROM problem_submission WHERE status = 'WAIT' ORDER BY id ASC LIMIT 100;") + sidQuery = cur.fetchone() + sid = 1 + pid = 2 + judgeLanguage = 'cpp' + judgeURL = 'judgeURL' + if sidQuery != None: + sid = sidQuery[0] + pid = sidQuery[1] + logging.info('sidQuery Success!') + logging.info('sid = %d, pid = %d' % (sid, pid)) + cur.execute("SELECT * FROM problem_problem where id = '%d';" % pid) + pidQuery = cur.fetchone() + judgeSource = pidQuery[11] + judgeType = pidQuery[12] + judgeLanguage = pidQuery[13] + logging.info('pidQuery Success!') + logging.info('judgeSource = %s, judgeType = %s, judgeLanguage = %s' % (judgeSource, judgeType, judgeLanguage)) + + if judgeSource == "LOCAL": + idleMachine = None + logging.info('get idleMachine') + while(idleMachine == None): + idleMachine = getIdleMachine() + time.sleep(1) + logging.info('idleMachine = %s' % idleMachine) + cur.execute("UPDATE problem_submission SET status = 'JUDGING' WHERE id = '%d';" % sid) + DB.commit() + judgeIP = machineInfo[idleMachine] + judgeURL = judgeIP + "/interface.py" + logging.info('get judgeURL = %s' % judgeURL) + else : + cur.execute("UPDATE problem_submission SET status = 'JUDGING' WHERE id = '%d';" % sid) + logging.info('send info to other judge') + """ + to be continued + """ + time.sleep(1) diff --git a/ojbackend/dispatcher.py~ b/ojbackend/dispatcher.py~ new file mode 100644 index 0000000..9f70e13 --- /dev/null +++ b/ojbackend/dispatcher.py~ @@ -0,0 +1,81 @@ +""" + dispatcher.py + This process continuously checks whether there exist unjudged submissions. +""" + +from dispatcherFunc import * +import time +import logging +import subprocess, sys +logging.basicConfig(filename = 'dispatcher.log', level = logging.INFO, format = '%(asctime)s ::%(message)s',datafmt = '%m/%d/%Y %I:%M:%S %p') +logging.info('==========Dispatcher Started==========') + +machineInfo = getMachine() +if machineInfo != None: + initMachine(machineInfo) + logging.info('getMachine Success') +else: + logging.info('getMachine Error') + logging.info('Please check the file machineInfo.config to check the settings') + logging.info('==========Dispatcher Finished==========') + exit(0) + +dbIP, dbUser, dbPasswd, dbName = getdbInfo() +if dbIP == None or dbUser == None or dbPasswd == None or dbName == None: + logging.info('getdbInfo Error') + logging.info('Please check the settings in ojdatabase.config') + logging.info('==========Dispatcher Finished==========') + exit(0) +logging.info('connect to database') + +while True: + + DB = connectDB(dbIP, dbUser, dbPasswd, dbName) + if DB == None: + logging.info('connect database error') + logging.info('Please check the settings in ojdatabase.config') + logging.info('==========Dispatcher Finished==========') + exit(0) + cur = DB.cursor() + cur.execute("USE nthuoj;") + cur.execute("SELECT * FROM problem_submission WHERE status = 'WAIT' ORDER BY id ASC LIMIT 100;") + sidQuery = cur.fetchone() + sid = 1 + pid = 2 + judgeLanguage = 'cpp' + judgeURL = 'judgeURL' + #subprocess.call('python curl_submit.py %s %s %s %s' %(sid, pid, judgeLanguage, judgeURL), shell = True) + if sidQuery != None: + sid = sidQuery[0] + pid = sidQuery[1] + logging.info('sidQuery Success!') + logging.info('sid = %d, pid = %d' % (sid, pid)) + cur.execute("SELECT * FROM problem_problem where id = '%d';" % pid) + pidQuery = cur.fetchone() + judgeSource = pidQuery[11] + judgeType = pidQuery[12] + judgeLanguage = pidQuery[13] + logging.info('pidQuery Success!') + logging.info('judgeSource = %s, judgeType = %s, judgeLanguage = %s' % (judgeSource, judgeType, judgeLanguage)) + + if judgeSource == "LOCAL": + idleMachine = None + logging.info('get idleMachine') + while(idleMachine == None): + idleMachine = getIdleMachine() + time.sleep(1) + logging.info('idleMachine = %s' % idleMachine) + cur.execute("UPDATE problem_submission SET status = 'JUDGING' WHERE id = '%d';" % sid) + DB.commit() + judgeIP = machineInfo[idleMachine] + judgeURL = judgeIP + "/interface.py" + logging.info('get judgeURL = %s' % judgeURL) + + #subprocess.call('python curl_submit.py %s %s %s %s' %(sid, pid, judgeLanguage, judgeURL), shell = True) + else : + cur.execute("UPDATE problem_submission SET status = 'JUDGING' WHERE id = '%d';" % sid) + logging.info('send info to other judge') + """ + to be continued + """ + time.sleep(1) diff --git a/ojbackend/dispatcherFunc.py b/ojbackend/dispatcherFunc.py new file mode 100644 index 0000000..a7ba6ae --- /dev/null +++ b/ojbackend/dispatcherFunc.py @@ -0,0 +1,76 @@ +""" + dispatcherFunc.py + This file provides related functions for dispatcher.php. +""" +from bash import bash +import MySQLdb + + +def getMachine(): + #load judge information from judge.config + #the information include judge name, ip address + machineInfo = {} + with open("machineInfo.config",'r') as f: + try: + for line in f: + (machine, ip) = line.split() + machineInfo[machine] = ip + return machineInfo + except: + print "encounter troubles when opening machineInfo.config" + return None +def initMachine(machineInfo): + #create a dir and a file for machineStatus + #0 means availabe, 1 means occupied + try: + bash('test -d machineStatusDir && rm -r machineStatusDir') + bash('mkdir machineStatusDir') + with open("machineStatusDir/machineStatus.config",'w') as f: + for machineName in machineInfo.keys(): + f.write(machineName + " 0\n") + except: + print "encounter troubles when rm machineStatusDir or mkdir machineStatusDir" + return None +def getIdleMachine(): + #to get the machine which is available for judging + machinePosition = 0 + IdleMachine = '' + try: + with open("machineStatusDir/machineStatus.config",'r+') as f: + for line in f: + if(line.split()[1] == '0'): + machineName = line.split()[0] + f.seek(machinePosition + len(machineName) + 1, 0) + f.write('1') + IdleMachine = machineName + return IdleMachine + break + machinePosition = machinePosition + len(line) + return None + except: + return None +def getdbInfo(): + with open("ojdatabase.config",'r') as f: + try: + for line in f: + if(line.split()[0] == "host"): + dbIP = line.split()[2] + elif(line.split()[0] == "user"): + dbUser = line.split()[2] + elif(line.split()[0] == "passwd"): + dbPasswd = line.split()[2] + elif(line.split()[0] == "db"): + dbName = line.split()[2] + except: + print "Log dbInfo Error\nPlease check ojdatabase.config" + return None, None, None, None + return dbIP, dbUser, dbPasswd, dbName +def connectDB(dbIP, dbUser, dbPasswd, dbName): + try: + DB = MySQLdb.connect(host = dbIP, user = dbUser, passwd = dbPasswd, db = dbName) + print "connect db success\n" + return DB + except: + print "connect DB error\n" + return None + diff --git a/ojbackend/dispatcherFunc.py~ b/ojbackend/dispatcherFunc.py~ new file mode 100644 index 0000000..3aca448 --- /dev/null +++ b/ojbackend/dispatcherFunc.py~ @@ -0,0 +1,76 @@ +""" + dispatcherFunc.py + This file provides related functions for dispatcher.php. +""" +from bash import bash +import MySQLdb + + +def getMachine(): + #load judge information from judge.config + #the information include judge name, ip address + machineInfo = {} + with open("machineInfo.config",'r') as f: + try: + for line in f: + (machine, ip) = line.split() + machineInfo[machine] = ip + return machineInfo + except: + print "encounter troubles when opening machineInfo.config" + return None +def initMachine(machineInfo): + #create a dir and a file for machineStatus + #0 means availabe, 1 means occupied + try: + bash('test -d machineStatusDir && rm -r machineStatusDir') + bash('mkdir machineStatusDir') + with open("machineStatusDir/machineStatus.config",'w') as f: + for machineName in machineInfo.keys(): + f.write(machineName + " 0\n") + except: + print "encounter troubles when rm machineStatusDir or mkdir machineStatusDir" + return None +def getIdleMachine(): + #to get the machine which is available for judging + machinePosition = 0 + IdleMachine = '' + try: + with open("machineStatusDir/machineStatus.config",'r+') as f: + for line in f: + if(line.split()[1] == '0'): + machineName = line.split()[0] + f.seek(machinePosition + len(machineName) + 1, 0) + f.write('1') + IdleMachine = machineName + return IdleMachine + break + machinePosition = machinePosition + len(line) + return None + except: + return None +def getdbInfo(): + with open("ojdatabase.config",'r') as f: + try: + for line in f: + if(line.split()[0] == "host"): + dbIP = line.split()[2] + elif(line.split()[0] == "user"): + dbUser = line.split()[2] + elif(line.split()[0] == "passwd"): + dbPasswd = line.split()[2] + elif(line.split()[0] == "db"): + dbName = line.split()[2] + except: + print "Log dbInfo Error\nPlease check ojdatabase.config" + return None, None, None, None + return dbIP, dbUser, dbPasswd, dbName +def connectDB(dbIP, dbUser, dbPasswd, dbName): + try: + DB = MySQLdb.connect(host = dbIP, user = dbUser, passwd = dbPasswd, db = dbName) + print "connect success\n" + return DB + except: + print "connect DB error\n" + return None + diff --git a/ojbackend/install.py b/ojbackend/install.py new file mode 100644 index 0000000..acfd4fd --- /dev/null +++ b/ojbackend/install.py @@ -0,0 +1,40 @@ +""" + install.py + This file let users to set up machineInfo.config and ojdatabase.config file +""" + +from bash import bash +def set_MachineInfo(): + + bash('test -d machineInfo.config && rm -r machineInfo.config') + num = raw_input("Please input the total number of the judges:\n") + if num > 0: + try: + with open("machineInfo.config",'w') as f: + for i in range(0,int(num)): + judgeName = raw_input("Please input the judge's name:\n") + judgeIP = raw_input("Please input the judge's ip:\n") + f.write(judgeName + " " + judgeIP + "\n") + except: + print "Error occurs when setting machineInfo.config\n" + exit(0) + +def set_OjDatabase(): + bash('test -d ojdatabase.config && rm -r ojdatabase.config') + try: + with open("ojdatabase.config",'w') as f: + host = raw_input("Please input the ip of the database:\n") + user = raw_input("Please input the username:\n") + passwd = raw_input("Please input the password:\n") + db = raw_input("Please input the name of the database:\n") + f.write("host = " + host + "\n") + f.write("user = " + user + "\n") + f.write("passwd = " + passwd + "\n") + f.write("db = " + db + "\n") + except: + print "Error occurs when setting ojdatabase.config\n" + exit(0) + +set_OjDatabase() +set_MachineInfo() + diff --git a/ojbackend/install.py~ b/ojbackend/install.py~ new file mode 100644 index 0000000..ae5c9db --- /dev/null +++ b/ojbackend/install.py~ @@ -0,0 +1,40 @@ +""" + install.py + This file let users to set up machineInfo.config and ojdatabase.config file +""" + +from bash import bash +def set_MachineInfo(): + + bash('test -d machineInfo.config && rm -r machineInfo.config') + num = raw_input("Please input number of the judges:\n") + if num > 0: + try: + with open("machineInfo.config",'w') as f: + for i in range(0,int(num)): + judgeName = raw_input("Please input the judge's name:\n") + judgeIP = raw_input("Please input the judge's ip:\n") + f.write(judgeName + " " + judgeIP + "\n") + except: + print "Error occurs when setting machineInfo.config\n" + exit(0) + +def set_OjDatabase(): + bash('test -d ojdatabase.config && rm -r ojdatabase.config') + try: + with open("ojdatabase.config",'w') as f: + host = raw_input("Please input the ip of the database:\n") + user = raw_input("Please input the username:\n") + passwd = raw_input("Please input the password:\n") + db = raw_input("Please input the name of the database:\n") + f.write("host = " + host + "\n") + f.write("user = " + user + "\n") + f.write("passwd = " + passwd + "\n") + f.write("db = " + db + "\n") + except: + print "Error occurs when setting ojdatabase.config\n" + exit(0) + +set_OjDatabase() +set_MachineInfo() + diff --git a/ojbackend/machine.config~ b/ojbackend/machine.config~ new file mode 100644 index 0000000..ff9d6fd --- /dev/null +++ b/ojbackend/machine.config~ @@ -0,0 +1 @@ +judge1 127.0.0.1 diff --git a/ojbackend/machineInfo.config~ b/ojbackend/machineInfo.config~ new file mode 100644 index 0000000..7dbf262 --- /dev/null +++ b/ojbackend/machineInfo.config~ @@ -0,0 +1 @@ +judge1 1 2 diff --git a/ojbackend/ojdatabase.config~ b/ojbackend/ojdatabase.config~ new file mode 100644 index 0000000..503efea --- /dev/null +++ b/ojbackend/ojdatabase.config~ @@ -0,0 +1,4 @@ +host = 127.0.0.1 +user = root +passwd = +db = nthuoj diff --git a/ojbackend/test.py~ b/ojbackend/test.py~ new file mode 100644 index 0000000..caa21c5 --- /dev/null +++ b/ojbackend/test.py~ @@ -0,0 +1,13 @@ +import logging + +with open("machineStatusDir/machineStatus.config",'r+') as f: + for machine in f: + #print machine.split()[1] + if(machine.split()[1] == '0'): + #print "HEY" + machineName = machine.split()[0] + machinePosition = machine.find(machineName) + #print machinePosition + f.seek(machinePosition+len(machineName), 0) + f.write('1') + break From 351e9a04474c93dede08928acf7e22c9da54813a Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Sun, 18 Oct 2015 22:49:30 +0800 Subject: [PATCH 02/20] Delete ReadMe --- ojbackend/ReadMe | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 ojbackend/ReadMe diff --git a/ojbackend/ReadMe b/ojbackend/ReadMe deleted file mode 100644 index e121ab1..0000000 --- a/ojbackend/ReadMe +++ /dev/null @@ -1,2 +0,0 @@ -1. execute install.py to set up machine.config and ojdatase.config -2. execute dispatcher.py as daemon and it will check if there are new submissions need to be judged From 542acbb7962164a92024f3efda628df8431ff6a3 Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Sun, 18 Oct 2015 22:49:37 +0800 Subject: [PATCH 03/20] Delete ReadMe~ --- ojbackend/ReadMe~ | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 ojbackend/ReadMe~ diff --git a/ojbackend/ReadMe~ b/ojbackend/ReadMe~ deleted file mode 100644 index 88bf771..0000000 --- a/ojbackend/ReadMe~ +++ /dev/null @@ -1,2 +0,0 @@ -1. execute install.py to set up machine.config and ojdatase.config -2. execute dispatcher.py as daemon From ae88fa788f2cd4e6a449bc2a440ec38da3e1e5bd Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Sun, 18 Oct 2015 22:50:15 +0800 Subject: [PATCH 04/20] Delete curl_submit.py~ --- ojbackend/curl_submit.py~ | 1 - 1 file changed, 1 deletion(-) delete mode 100644 ojbackend/curl_submit.py~ diff --git a/ojbackend/curl_submit.py~ b/ojbackend/curl_submit.py~ deleted file mode 100644 index 92c61b1..0000000 --- a/ojbackend/curl_submit.py~ +++ /dev/null @@ -1 +0,0 @@ -print "HELLO WORLD" From 1fac1f9e170ba13e2d5813725874869ace9f37e4 Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Sun, 18 Oct 2015 22:55:05 +0800 Subject: [PATCH 05/20] Delete dispatcher.log~ --- ojbackend/dispatcher.log~ | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 ojbackend/dispatcher.log~ diff --git a/ojbackend/dispatcher.log~ b/ojbackend/dispatcher.log~ deleted file mode 100644 index a7aec23..0000000 --- a/ojbackend/dispatcher.log~ +++ /dev/null @@ -1,25 +0,0 @@ -2015-10-09 22:27:04,177 ==========Dispatcher Started========== -2015-10-09 22:27:04,182 getMachine Success -2015-10-09 22:27:04,182 connect to database -2015-10-09 22:27:04,193 sidQuery Success! -2015-10-09 22:27:04,193 sid = 10, pid = 2 -2015-10-09 22:27:04,193 pidQuery Success! -2015-10-09 22:27:04,193 judgeSource = LOCAL, judgeType = LOCAL_NORMAL, judgeLanguage = CPP -2015-10-09 22:27:04,193 get idleMachine -2015-10-09 22:27:05,194 idleMachine = judge1 -2015-10-09 22:27:05,227 get judgeURL = 127.0.0.1/interface.py -2015-10-09 22:29:21,094 ==========Dispatcher Started========== :: -2015-10-09 22:29:21,100 getMachine Success :: -2015-10-09 22:29:21,100 connect to database :: -2015-10-09 22:29:48,812 ::==========Dispatcher Started========== -2015-10-09 22:29:48,817 ::getMachine Success -2015-10-09 22:29:48,817 ::connect to database -2015-10-09 22:31:37,054 ::==========Dispatcher Started========== -2015-10-09 22:31:37,060 ::getMachine Success -2015-10-09 22:31:37,060 ::connect to database -2015-10-09 22:32:51,215 ::==========Dispatcher Started========== -2015-10-09 22:32:51,219 ::getMachine Success -2015-10-09 22:32:51,219 ::connect to database -2015-10-09 22:33:16,031 ::==========Dispatcher Started========== -2015-10-09 22:33:16,038 ::getMachine Success -2015-10-09 22:33:16,038 ::connect to database From 225ccf55519c7214454bfda01825d7dc76476f52 Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Sun, 18 Oct 2015 22:55:13 +0800 Subject: [PATCH 06/20] Delete dispatcher.py~ --- ojbackend/dispatcher.py~ | 81 ---------------------------------------- 1 file changed, 81 deletions(-) delete mode 100644 ojbackend/dispatcher.py~ diff --git a/ojbackend/dispatcher.py~ b/ojbackend/dispatcher.py~ deleted file mode 100644 index 9f70e13..0000000 --- a/ojbackend/dispatcher.py~ +++ /dev/null @@ -1,81 +0,0 @@ -""" - dispatcher.py - This process continuously checks whether there exist unjudged submissions. -""" - -from dispatcherFunc import * -import time -import logging -import subprocess, sys -logging.basicConfig(filename = 'dispatcher.log', level = logging.INFO, format = '%(asctime)s ::%(message)s',datafmt = '%m/%d/%Y %I:%M:%S %p') -logging.info('==========Dispatcher Started==========') - -machineInfo = getMachine() -if machineInfo != None: - initMachine(machineInfo) - logging.info('getMachine Success') -else: - logging.info('getMachine Error') - logging.info('Please check the file machineInfo.config to check the settings') - logging.info('==========Dispatcher Finished==========') - exit(0) - -dbIP, dbUser, dbPasswd, dbName = getdbInfo() -if dbIP == None or dbUser == None or dbPasswd == None or dbName == None: - logging.info('getdbInfo Error') - logging.info('Please check the settings in ojdatabase.config') - logging.info('==========Dispatcher Finished==========') - exit(0) -logging.info('connect to database') - -while True: - - DB = connectDB(dbIP, dbUser, dbPasswd, dbName) - if DB == None: - logging.info('connect database error') - logging.info('Please check the settings in ojdatabase.config') - logging.info('==========Dispatcher Finished==========') - exit(0) - cur = DB.cursor() - cur.execute("USE nthuoj;") - cur.execute("SELECT * FROM problem_submission WHERE status = 'WAIT' ORDER BY id ASC LIMIT 100;") - sidQuery = cur.fetchone() - sid = 1 - pid = 2 - judgeLanguage = 'cpp' - judgeURL = 'judgeURL' - #subprocess.call('python curl_submit.py %s %s %s %s' %(sid, pid, judgeLanguage, judgeURL), shell = True) - if sidQuery != None: - sid = sidQuery[0] - pid = sidQuery[1] - logging.info('sidQuery Success!') - logging.info('sid = %d, pid = %d' % (sid, pid)) - cur.execute("SELECT * FROM problem_problem where id = '%d';" % pid) - pidQuery = cur.fetchone() - judgeSource = pidQuery[11] - judgeType = pidQuery[12] - judgeLanguage = pidQuery[13] - logging.info('pidQuery Success!') - logging.info('judgeSource = %s, judgeType = %s, judgeLanguage = %s' % (judgeSource, judgeType, judgeLanguage)) - - if judgeSource == "LOCAL": - idleMachine = None - logging.info('get idleMachine') - while(idleMachine == None): - idleMachine = getIdleMachine() - time.sleep(1) - logging.info('idleMachine = %s' % idleMachine) - cur.execute("UPDATE problem_submission SET status = 'JUDGING' WHERE id = '%d';" % sid) - DB.commit() - judgeIP = machineInfo[idleMachine] - judgeURL = judgeIP + "/interface.py" - logging.info('get judgeURL = %s' % judgeURL) - - #subprocess.call('python curl_submit.py %s %s %s %s' %(sid, pid, judgeLanguage, judgeURL), shell = True) - else : - cur.execute("UPDATE problem_submission SET status = 'JUDGING' WHERE id = '%d';" % sid) - logging.info('send info to other judge') - """ - to be continued - """ - time.sleep(1) From b9cccefa14ada6014f1fd140784cbedee4101b6f Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Sun, 18 Oct 2015 22:55:21 +0800 Subject: [PATCH 07/20] Delete dispatcherFunc.py~ --- ojbackend/dispatcherFunc.py~ | 76 ------------------------------------ 1 file changed, 76 deletions(-) delete mode 100644 ojbackend/dispatcherFunc.py~ diff --git a/ojbackend/dispatcherFunc.py~ b/ojbackend/dispatcherFunc.py~ deleted file mode 100644 index 3aca448..0000000 --- a/ojbackend/dispatcherFunc.py~ +++ /dev/null @@ -1,76 +0,0 @@ -""" - dispatcherFunc.py - This file provides related functions for dispatcher.php. -""" -from bash import bash -import MySQLdb - - -def getMachine(): - #load judge information from judge.config - #the information include judge name, ip address - machineInfo = {} - with open("machineInfo.config",'r') as f: - try: - for line in f: - (machine, ip) = line.split() - machineInfo[machine] = ip - return machineInfo - except: - print "encounter troubles when opening machineInfo.config" - return None -def initMachine(machineInfo): - #create a dir and a file for machineStatus - #0 means availabe, 1 means occupied - try: - bash('test -d machineStatusDir && rm -r machineStatusDir') - bash('mkdir machineStatusDir') - with open("machineStatusDir/machineStatus.config",'w') as f: - for machineName in machineInfo.keys(): - f.write(machineName + " 0\n") - except: - print "encounter troubles when rm machineStatusDir or mkdir machineStatusDir" - return None -def getIdleMachine(): - #to get the machine which is available for judging - machinePosition = 0 - IdleMachine = '' - try: - with open("machineStatusDir/machineStatus.config",'r+') as f: - for line in f: - if(line.split()[1] == '0'): - machineName = line.split()[0] - f.seek(machinePosition + len(machineName) + 1, 0) - f.write('1') - IdleMachine = machineName - return IdleMachine - break - machinePosition = machinePosition + len(line) - return None - except: - return None -def getdbInfo(): - with open("ojdatabase.config",'r') as f: - try: - for line in f: - if(line.split()[0] == "host"): - dbIP = line.split()[2] - elif(line.split()[0] == "user"): - dbUser = line.split()[2] - elif(line.split()[0] == "passwd"): - dbPasswd = line.split()[2] - elif(line.split()[0] == "db"): - dbName = line.split()[2] - except: - print "Log dbInfo Error\nPlease check ojdatabase.config" - return None, None, None, None - return dbIP, dbUser, dbPasswd, dbName -def connectDB(dbIP, dbUser, dbPasswd, dbName): - try: - DB = MySQLdb.connect(host = dbIP, user = dbUser, passwd = dbPasswd, db = dbName) - print "connect success\n" - return DB - except: - print "connect DB error\n" - return None - From 5751922f33a9cb3e75df7c6766554886ec1d2be3 Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Sun, 18 Oct 2015 22:55:28 +0800 Subject: [PATCH 08/20] Delete install.py~ --- ojbackend/install.py~ | 40 ---------------------------------------- 1 file changed, 40 deletions(-) delete mode 100644 ojbackend/install.py~ diff --git a/ojbackend/install.py~ b/ojbackend/install.py~ deleted file mode 100644 index ae5c9db..0000000 --- a/ojbackend/install.py~ +++ /dev/null @@ -1,40 +0,0 @@ -""" - install.py - This file let users to set up machineInfo.config and ojdatabase.config file -""" - -from bash import bash -def set_MachineInfo(): - - bash('test -d machineInfo.config && rm -r machineInfo.config') - num = raw_input("Please input number of the judges:\n") - if num > 0: - try: - with open("machineInfo.config",'w') as f: - for i in range(0,int(num)): - judgeName = raw_input("Please input the judge's name:\n") - judgeIP = raw_input("Please input the judge's ip:\n") - f.write(judgeName + " " + judgeIP + "\n") - except: - print "Error occurs when setting machineInfo.config\n" - exit(0) - -def set_OjDatabase(): - bash('test -d ojdatabase.config && rm -r ojdatabase.config') - try: - with open("ojdatabase.config",'w') as f: - host = raw_input("Please input the ip of the database:\n") - user = raw_input("Please input the username:\n") - passwd = raw_input("Please input the password:\n") - db = raw_input("Please input the name of the database:\n") - f.write("host = " + host + "\n") - f.write("user = " + user + "\n") - f.write("passwd = " + passwd + "\n") - f.write("db = " + db + "\n") - except: - print "Error occurs when setting ojdatabase.config\n" - exit(0) - -set_OjDatabase() -set_MachineInfo() - From 2b84e6735f3a4211484a94de1715ab5ce7406664 Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Sun, 18 Oct 2015 22:55:34 +0800 Subject: [PATCH 09/20] Delete machine.config~ --- ojbackend/machine.config~ | 1 - 1 file changed, 1 deletion(-) delete mode 100644 ojbackend/machine.config~ diff --git a/ojbackend/machine.config~ b/ojbackend/machine.config~ deleted file mode 100644 index ff9d6fd..0000000 --- a/ojbackend/machine.config~ +++ /dev/null @@ -1 +0,0 @@ -judge1 127.0.0.1 From e95b04bef5fdc85ca14e7c75d0e5ae5b87f9219f Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Sun, 18 Oct 2015 22:55:39 +0800 Subject: [PATCH 10/20] Delete machineInfo.config~ --- ojbackend/machineInfo.config~ | 1 - 1 file changed, 1 deletion(-) delete mode 100644 ojbackend/machineInfo.config~ diff --git a/ojbackend/machineInfo.config~ b/ojbackend/machineInfo.config~ deleted file mode 100644 index 7dbf262..0000000 --- a/ojbackend/machineInfo.config~ +++ /dev/null @@ -1 +0,0 @@ -judge1 1 2 From d8d4bf732b6dd3aca73021a20e2dda932522465d Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Sun, 18 Oct 2015 22:55:45 +0800 Subject: [PATCH 11/20] Delete ojdatabase.config~ --- ojbackend/ojdatabase.config~ | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 ojbackend/ojdatabase.config~ diff --git a/ojbackend/ojdatabase.config~ b/ojbackend/ojdatabase.config~ deleted file mode 100644 index 503efea..0000000 --- a/ojbackend/ojdatabase.config~ +++ /dev/null @@ -1,4 +0,0 @@ -host = 127.0.0.1 -user = root -passwd = -db = nthuoj From 3d8b8fe3041437360e9cc15ed74b38a9306fd208 Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Sun, 18 Oct 2015 22:55:51 +0800 Subject: [PATCH 12/20] Delete test.py~ --- ojbackend/test.py~ | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 ojbackend/test.py~ diff --git a/ojbackend/test.py~ b/ojbackend/test.py~ deleted file mode 100644 index caa21c5..0000000 --- a/ojbackend/test.py~ +++ /dev/null @@ -1,13 +0,0 @@ -import logging - -with open("machineStatusDir/machineStatus.config",'r+') as f: - for machine in f: - #print machine.split()[1] - if(machine.split()[1] == '0'): - #print "HEY" - machineName = machine.split()[0] - machinePosition = machine.find(machineName) - #print machinePosition - f.seek(machinePosition+len(machineName), 0) - f.write('1') - break From f6eadcdce6893369f29aea4ca55a18151326b407 Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Sun, 18 Oct 2015 22:56:43 +0800 Subject: [PATCH 13/20] Create ReadMe --- ojbackend/ReadMe | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 ojbackend/ReadMe diff --git a/ojbackend/ReadMe b/ojbackend/ReadMe new file mode 100644 index 0000000..e121ab1 --- /dev/null +++ b/ojbackend/ReadMe @@ -0,0 +1,2 @@ +1. execute install.py to set up machine.config and ojdatase.config +2. execute dispatcher.py as daemon and it will check if there are new submissions need to be judged From 2eecd1309050ee08d81cf5000f0606a1b5e4004a Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Sun, 18 Oct 2015 23:02:41 +0800 Subject: [PATCH 14/20] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 911ce17..c454b85 100644 --- a/README.md +++ b/README.md @@ -1,2 +1 @@ -NTHUOJ_backend -============== +python code is in the ojbackend directory From 52d5103319fa439cc601213af99fbe5951b2db6d Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Mon, 19 Oct 2015 23:17:15 +0800 Subject: [PATCH 15/20] Update dispatcherFunc.py --- ojbackend/dispatcherFunc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ojbackend/dispatcherFunc.py b/ojbackend/dispatcherFunc.py index a7ba6ae..7e39ad8 100644 --- a/ojbackend/dispatcherFunc.py +++ b/ojbackend/dispatcherFunc.py @@ -1,6 +1,6 @@ """ dispatcherFunc.py - This file provides related functions for dispatcher.php. + This file provides related functions for dispatcher.py. """ from bash import bash import MySQLdb From 9a1b12c5c8b6bb89f95f6db5afe17be321018cd2 Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Mon, 9 Nov 2015 02:04:14 +0800 Subject: [PATCH 16/20] Update dispatcher.py modify dispatcher.py. Not done --- ojbackend/dispatcher.py | 52 +++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/ojbackend/dispatcher.py b/ojbackend/dispatcher.py index 15606bf..f79c263 100644 --- a/ojbackend/dispatcher.py +++ b/ojbackend/dispatcher.py @@ -7,7 +7,10 @@ import time import logging import subprocess, sys -logging.basicConfig(filename = 'dispatcher.log', level = logging.INFO, format = '%(asctime)s ::%(message)s',datafmt = '%m/%d/%Y %I:%M:%S %p') +logging.basicConfig(filename = 'dispatcher.log', + level = logging.INFO, + format = '%(asctime)s ::%(message)s', + datafmt = '%m/%d/%Y %I:%M:%S %p') logging.info('==========Dispatcher Started==========') machineInfo = getMachine() @@ -36,9 +39,15 @@ logging.info('Please check the settings in ojdatabase.config') logging.info('==========Dispatcher Finished==========') exit(0) + submissionStat = " \ + SELECT * FROM problem_submission \ + WHERE status = \'WAIT\' \ + ORDER BY id ASC \ + LIMIT 100; \ + " cur = DB.cursor() cur.execute("USE nthuoj;") - cur.execute("SELECT * FROM problem_submission WHERE status = 'WAIT' ORDER BY id ASC LIMIT 100;") + cur.execute(submissionStat) sidQuery = cur.fetchone() sid = 1 pid = 2 @@ -49,30 +58,49 @@ pid = sidQuery[1] logging.info('sidQuery Success!') logging.info('sid = %d, pid = %d' % (sid, pid)) - cur.execute("SELECT * FROM problem_problem where id = '%d';" % pid) + problemID = " \ + SELECT * FROM problem_problem where id = \'%d\'; \ + " + cur.execute(problemID % pid) pidQuery = cur.fetchone() judgeSource = pidQuery[11] judgeType = pidQuery[12] judgeLanguage = pidQuery[13] logging.info('pidQuery Success!') - logging.info('judgeSource = %s, judgeType = %s, judgeLanguage = %s' % (judgeSource, judgeType, judgeLanguage)) + logging.info('judgeSource = %s, judgeType = %s, judgeLanguage = %s' + % (judgeSource, judgeType, judgeLanguage)) if judgeSource == "LOCAL": idleMachine = None logging.info('get idleMachine') - while(idleMachine == None): - idleMachine = getIdleMachine() + while idleMachine == None: time.sleep(1) - logging.info('idleMachine = %s' % idleMachine) - cur.execute("UPDATE problem_submission SET status = 'JUDGING' WHERE id = '%d';" % sid) + idleMachine = getIdleMachine() + logging.info('idleMachine = %s' % idleMachine) + updatesidStat = " \ + UPDATE problem_submission \ + SET status = \'JUDGING\' \ + WHERE id = \'%d\'; \ + " + cur.execute(updatesidStat % sid) DB.commit() judgeIP = machineInfo[idleMachine] judgeURL = judgeIP + "/interface.py" logging.info('get judgeURL = %s' % judgeURL) else : - cur.execute("UPDATE problem_submission SET status = 'JUDGING' WHERE id = '%d';" % sid) + updatesidStat = " \ + UPDATE problem_submission \ + SET status = \'JUDGING\' \ + WHERE id = \'%d\'; \ + " + cur.execute(updatesidStat % sid) logging.info('send info to other judge') - """ - to be continued - """ + arg = judgeLanguage + " " + pid + " " + judgeURL + " " + sid + + if os.path.exists("/var/nthuoj/outsideConnection/sendToOtherJudge.sh"): + handle = popen("/var/nthuoj/outsideConnection/sendToOtherJudge.sh " + + arg + " & >> output.html", "w") + pclose(handle) + else : + logging.info('Send to other judge ERROR(No Judge)') time.sleep(1) From 94789d12b6118b0f8178e2b59f70c41bc3c9bb14 Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Mon, 9 Nov 2015 02:05:39 +0800 Subject: [PATCH 17/20] Update dispatcherFunc.py modify dispatcherFunc.py --- ojbackend/dispatcherFunc.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ojbackend/dispatcherFunc.py b/ojbackend/dispatcherFunc.py index 7e39ad8..9d12656 100644 --- a/ojbackend/dispatcherFunc.py +++ b/ojbackend/dispatcherFunc.py @@ -1,6 +1,6 @@ """ dispatcherFunc.py - This file provides related functions for dispatcher.py. + This file provides related functions for dispatcher.php. """ from bash import bash import MySQLdb @@ -19,6 +19,8 @@ def getMachine(): except: print "encounter troubles when opening machineInfo.config" return None + + def initMachine(machineInfo): #create a dir and a file for machineStatus #0 means availabe, 1 means occupied @@ -31,6 +33,8 @@ def initMachine(machineInfo): except: print "encounter troubles when rm machineStatusDir or mkdir machineStatusDir" return None + + def getIdleMachine(): #to get the machine which is available for judging machinePosition = 0 @@ -49,6 +53,8 @@ def getIdleMachine(): return None except: return None + + def getdbInfo(): with open("ojdatabase.config",'r') as f: try: @@ -65,6 +71,8 @@ def getdbInfo(): print "Log dbInfo Error\nPlease check ojdatabase.config" return None, None, None, None return dbIP, dbUser, dbPasswd, dbName + + def connectDB(dbIP, dbUser, dbPasswd, dbName): try: DB = MySQLdb.connect(host = dbIP, user = dbUser, passwd = dbPasswd, db = dbName) @@ -73,4 +81,3 @@ def connectDB(dbIP, dbUser, dbPasswd, dbName): except: print "connect DB error\n" return None - From 1b5a00af950520805ede21666d8af1c8a30b0349 Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Fri, 11 Dec 2015 15:23:11 +0800 Subject: [PATCH 18/20] Update dispatcher.py --- ojbackend/dispatcher.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ojbackend/dispatcher.py b/ojbackend/dispatcher.py index f79c263..74959cc 100644 --- a/ojbackend/dispatcher.py +++ b/ojbackend/dispatcher.py @@ -49,11 +49,8 @@ cur.execute("USE nthuoj;") cur.execute(submissionStat) sidQuery = cur.fetchone() - sid = 1 - pid = 2 - judgeLanguage = 'cpp' - judgeURL = 'judgeURL' - if sidQuery != None: + + while sidQuery != None: sid = sidQuery[0] pid = sidQuery[1] logging.info('sidQuery Success!') @@ -103,4 +100,7 @@ pclose(handle) else : logging.info('Send to other judge ERROR(No Judge)') + + sidQuery = cur.fetchone() time.sleep(1) + From a450e5a8d88043c0a875954ca2cf4b6eff93b966 Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Fri, 15 Jan 2016 16:31:45 +0800 Subject: [PATCH 19/20] Update dispatcherFunc.py --- ojbackend/dispatcherFunc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ojbackend/dispatcherFunc.py b/ojbackend/dispatcherFunc.py index 9d12656..05910fd 100644 --- a/ojbackend/dispatcherFunc.py +++ b/ojbackend/dispatcherFunc.py @@ -1,6 +1,6 @@ """ dispatcherFunc.py - This file provides related functions for dispatcher.php. + This file provides related functions for dispatcher.py. """ from bash import bash import MySQLdb From bd6e8aec2e5e07e8b359e537703f9fc5396ea0fa Mon Sep 17 00:00:00 2001 From: shawn1128 Date: Fri, 15 Jan 2016 16:42:30 +0800 Subject: [PATCH 20/20] Update dispatcher.py --- ojbackend/dispatcher.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ojbackend/dispatcher.py b/ojbackend/dispatcher.py index 74959cc..7a12c4e 100644 --- a/ojbackend/dispatcher.py +++ b/ojbackend/dispatcher.py @@ -30,10 +30,9 @@ logging.info('==========Dispatcher Finished==========') exit(0) logging.info('connect to database') - +DB = connectDB(dbIP, dbUser, dbPasswd, dbName) while True: - DB = connectDB(dbIP, dbUser, dbPasswd, dbName) if DB == None: logging.info('connect database error') logging.info('Please check the settings in ojdatabase.config')