mirror of
https://github.com/titanscouting/tra-superscript.git
synced 2025-03-03 13:25:47 +00:00
removed: daemonization,
socket messaging added: CLI option to specify config file Not working, requires data.py changes in competition branch
This commit is contained in:
parent
6b070c7b08
commit
34f0b3f10c
@ -149,19 +149,21 @@ __author__ = (
|
|||||||
|
|
||||||
# imports:
|
# imports:
|
||||||
|
|
||||||
|
from distutils.command.config import config
|
||||||
import os, sys, time
|
import os, sys, time
|
||||||
import pymongo # soon to be deprecated
|
import pymongo # soon to be deprecated
|
||||||
import traceback
|
import traceback
|
||||||
import warnings
|
import warnings
|
||||||
from config import Configuration, ConfigurationError
|
from config import Configuration, ConfigurationError
|
||||||
from data import get_previous_time, set_current_time, check_new_database_matches
|
from data import get_previous_time, set_current_time, check_new_database_matches, clear_metrics
|
||||||
from interface import Logger
|
from interface import Logger
|
||||||
from module import Match, Metric, Pit
|
from module import Match, Metric, Pit
|
||||||
import zmq
|
import zmq
|
||||||
|
|
||||||
config_path = "config.json"
|
|
||||||
|
|
||||||
def main(logger, verbose, profile, debug, socket_send = None):
|
|
||||||
|
#def main(logger, verbose, profile, debug, socket_send = None):
|
||||||
|
def main(logger, verbose, profile, debug, config_path):
|
||||||
|
|
||||||
def close_all():
|
def close_all():
|
||||||
if "client" in locals():
|
if "client" in locals():
|
||||||
@ -180,32 +182,32 @@ def main(logger, verbose, profile, debug, socket_send = None):
|
|||||||
loop_start = time.time()
|
loop_start = time.time()
|
||||||
|
|
||||||
logger.info("current time: " + str(loop_start))
|
logger.info("current time: " + str(loop_start))
|
||||||
socket_send("current time: " + str(loop_start))
|
#socket_send("current time: " + str(loop_start))
|
||||||
|
|
||||||
config = Configuration(config_path)
|
config = Configuration(config_path)
|
||||||
|
|
||||||
logger.info("found and loaded config at <" + config_path + ">")
|
logger.info("found and loaded config at <" + config_path + ">")
|
||||||
socket_send("found and loaded config at <" + config_path + ">")
|
#socket_send("found and loaded config at <" + config_path + ">")
|
||||||
|
|
||||||
apikey, tbakey = config.database, config.tba
|
apikey, tbakey = config.database, config.tba
|
||||||
|
|
||||||
logger.info("found and loaded database and tba keys")
|
logger.info("found and loaded database and tba keys")
|
||||||
socket_send("found and loaded database and tba keys")
|
#socket_send("found and loaded database and tba keys")
|
||||||
|
|
||||||
client = pymongo.MongoClient(apikey)
|
client = pymongo.MongoClient(apikey)
|
||||||
|
|
||||||
logger.info("established connection to database")
|
logger.info("established connection to database")
|
||||||
socket_send("established connection to database")
|
#socket_send("established connection to database")
|
||||||
|
|
||||||
previous_time = get_previous_time(client)
|
previous_time = get_previous_time(client)
|
||||||
|
|
||||||
logger.info("analysis backtimed to: " + str(previous_time))
|
logger.info("analysis backtimed to: " + str(previous_time))
|
||||||
socket_send("analysis backtimed to: " + str(previous_time))
|
#socket_send("analysis backtimed to: " + str(previous_time))
|
||||||
|
|
||||||
config.resolve_config_conflicts(logger, client)
|
config.resolve_config_conflicts(logger, client)
|
||||||
|
|
||||||
config_modules, competition = config.modules, config.competition
|
config_modules, competition = config.modules, config.competition
|
||||||
|
clear_metrics(client, config.competition)
|
||||||
for m in config_modules:
|
for m in config_modules:
|
||||||
if m in modules:
|
if m in modules:
|
||||||
start = time.time()
|
start = time.time()
|
||||||
@ -215,7 +217,7 @@ def main(logger, verbose, profile, debug, socket_send = None):
|
|||||||
continue
|
continue
|
||||||
current_module.run()
|
current_module.run()
|
||||||
logger.info(m + " module finished in " + str(time.time() - start) + " seconds")
|
logger.info(m + " module finished in " + str(time.time() - start) + " seconds")
|
||||||
socket_send(m + " module finished in " + str(time.time() - start) + " seconds")
|
#socket_send(m + " module finished in " + str(time.time() - start) + " seconds")
|
||||||
if debug:
|
if debug:
|
||||||
logger.save_module_to_file(m, current_module.data, current_module.results) # logging flag check done in logger
|
logger.save_module_to_file(m, current_module.data, current_module.results) # logging flag check done in logger
|
||||||
|
|
||||||
@ -224,8 +226,8 @@ def main(logger, verbose, profile, debug, socket_send = None):
|
|||||||
|
|
||||||
logger.info("closed threads and database client")
|
logger.info("closed threads and database client")
|
||||||
logger.info("finished all tasks in " + str(time.time() - loop_start) + " seconds, looping")
|
logger.info("finished all tasks in " + str(time.time() - loop_start) + " seconds, looping")
|
||||||
socket_send("closed threads and database client")
|
#socket_send("closed threads and database client")
|
||||||
socket_send("finished all tasks in " + str(time.time() - loop_start) + " seconds, looping")
|
#socket_send("finished all tasks in " + str(time.time() - loop_start) + " seconds, looping")
|
||||||
|
|
||||||
if profile:
|
if profile:
|
||||||
return 0
|
return 0
|
||||||
@ -236,33 +238,33 @@ def main(logger, verbose, profile, debug, socket_send = None):
|
|||||||
event_delay = config["variable"]["event-delay"]
|
event_delay = config["variable"]["event-delay"]
|
||||||
if event_delay:
|
if event_delay:
|
||||||
logger.info("loop delayed until database returns new matches")
|
logger.info("loop delayed until database returns new matches")
|
||||||
socket_send("loop delayed until database returns new matches")
|
#socket_send("loop delayed until database returns new matches")
|
||||||
new_match = False
|
new_match = False
|
||||||
while not new_match:
|
while not new_match:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
new_match = check_new_database_matches(client, competition)
|
new_match = check_new_database_matches(client, competition)
|
||||||
logger.info("database returned new matches")
|
logger.info("database returned new matches")
|
||||||
socket_send("database returned new matches")
|
#socket_send("database returned new matches")
|
||||||
else:
|
else:
|
||||||
loop_delay = float(config["variable"]["loop-delay"])
|
loop_delay = float(config["variable"]["loop-delay"])
|
||||||
remaining_time = loop_delay - (time.time() - loop_start)
|
remaining_time = loop_delay - (time.time() - loop_start)
|
||||||
if remaining_time > 0:
|
if remaining_time > 0:
|
||||||
logger.info("loop delayed by " + str(remaining_time) + " seconds")
|
logger.info("loop delayed by " + str(remaining_time) + " seconds")
|
||||||
socket_send("loop delayed by " + str(remaining_time) + " seconds")
|
#socket_send("loop delayed by " + str(remaining_time) + " seconds")
|
||||||
time.sleep(remaining_time)
|
time.sleep(remaining_time)
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
close_all()
|
close_all()
|
||||||
logger.info("detected KeyboardInterrupt, exiting")
|
logger.info("detected KeyboardInterrupt, exiting")
|
||||||
socket_send("detected KeyboardInterrupt, exiting")
|
#socket_send("detected KeyboardInterrupt, exiting")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
except ConfigurationError as e:
|
except ConfigurationError as e:
|
||||||
str_e = "".join(traceback.format_exception(e))
|
str_e = "".join(traceback.format_exception(e))
|
||||||
logger.error("encountered a configuration error: " + str(e))
|
logger.error("encountered a configuration error: " + str(e))
|
||||||
logger.error(str_e)
|
logger.error(str_e)
|
||||||
socket_send("encountered a configuration error: " + str(e))
|
#socket_send("encountered a configuration error: " + str(e))
|
||||||
socket_send(str_e)
|
#socket_send(str_e)
|
||||||
close_all()
|
close_all()
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
@ -270,12 +272,12 @@ def main(logger, verbose, profile, debug, socket_send = None):
|
|||||||
str_e = "".join(traceback.format_exception(e))
|
str_e = "".join(traceback.format_exception(e))
|
||||||
logger.error("encountered an exception while running")
|
logger.error("encountered an exception while running")
|
||||||
logger.error(str_e)
|
logger.error(str_e)
|
||||||
socket_send("encountered an exception while running")
|
#socket_send("encountered an exception while running")
|
||||||
socket_send(str_e)
|
#socket_send(str_e)
|
||||||
close_all()
|
close_all()
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
def start(pid_path, verbose, profile, debug):
|
def start(pid_path, verbose, profile, debug, config_path):
|
||||||
|
|
||||||
if profile:
|
if profile:
|
||||||
|
|
||||||
@ -287,7 +289,8 @@ def start(pid_path, verbose, profile, debug):
|
|||||||
import cProfile, pstats, io
|
import cProfile, pstats, io
|
||||||
profile = cProfile.Profile()
|
profile = cProfile.Profile()
|
||||||
profile.enable()
|
profile.enable()
|
||||||
exit_code = main(logger, verbose, profile, debug, socket_send = send)
|
#exit_code = main(logger, verbose, profile, debug, socket_send = send)
|
||||||
|
exit_code = main(logger, verbose, profile, debug, config_path)
|
||||||
profile.disable()
|
profile.disable()
|
||||||
f = open("profile.txt", 'w+')
|
f = open("profile.txt", 'w+')
|
||||||
ps = pstats.Stats(profile, stream = f).sort_stats('cumtime')
|
ps = pstats.Stats(profile, stream = f).sort_stats('cumtime')
|
||||||
@ -301,7 +304,8 @@ def start(pid_path, verbose, profile, debug):
|
|||||||
|
|
||||||
logger = Logger(verbose, profile, debug)
|
logger = Logger(verbose, profile, debug)
|
||||||
|
|
||||||
exit_code = main(logger, verbose, profile, debug, socket_send = send)
|
#exit_code = main(logger, verbose, profile, debug, socket_send = send)
|
||||||
|
exit_code = main(logger, verbose, profile, debug, config_path)
|
||||||
sys.exit(exit_code)
|
sys.exit(exit_code)
|
||||||
|
|
||||||
elif debug:
|
elif debug:
|
||||||
@ -311,93 +315,34 @@ def start(pid_path, verbose, profile, debug):
|
|||||||
|
|
||||||
logger = Logger(verbose, profile, debug)
|
logger = Logger(verbose, profile, debug)
|
||||||
|
|
||||||
exit_code = main(logger, verbose, profile, debug, socket_send = send)
|
#exit_code = main(logger, verbose, profile, debug, socket_send = send)
|
||||||
|
exit_code = main(logger, verbose, profile, debug, config_path)
|
||||||
sys.exit(exit_code)
|
sys.exit(exit_code)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
logfile = "logfile.log"
|
pass # must be vebose, debug or profile
|
||||||
|
|
||||||
f = open(logfile, 'w+')
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
e = open('errorlog.log', 'w+')
|
|
||||||
with daemon.DaemonContext(
|
|
||||||
working_directory = os.getcwd(),
|
|
||||||
pidfile = pidfile.TimeoutPIDLockFile(pid_path),
|
|
||||||
stderr = e
|
|
||||||
):
|
|
||||||
|
|
||||||
context = zmq.Context()
|
|
||||||
socket = context.socket(zmq.PUB)
|
|
||||||
socket.bind("tcp://*:5678")
|
|
||||||
socket.send(b'status')
|
|
||||||
|
|
||||||
def send(msg):
|
|
||||||
socket.send(bytes("status: " + msg, "utf-8"))
|
|
||||||
|
|
||||||
logger = Logger(verbose, profile, debug, file = logfile)
|
|
||||||
|
|
||||||
exit_code = main(logger, verbose, profile, debug, socket_send = send)
|
|
||||||
|
|
||||||
socket.close()
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
sys.exit(exit_code)
|
|
||||||
|
|
||||||
def stop(pid_path):
|
|
||||||
try:
|
|
||||||
pf = open(pid_path, 'r')
|
|
||||||
pid = int(pf.read().strip())
|
|
||||||
pf.close()
|
|
||||||
except IOError:
|
|
||||||
sys.stderr.write("pidfile at <" + pid_path + "> does not exist. Daemon not running?\n")
|
|
||||||
return
|
|
||||||
|
|
||||||
try:
|
|
||||||
while True:
|
|
||||||
os.kill(pid, SIGTERM)
|
|
||||||
time.sleep(0.01)
|
|
||||||
except OSError as err:
|
|
||||||
err = str(err)
|
|
||||||
if err.find("No such process") > 0:
|
|
||||||
if os.path.exists(pid_path):
|
|
||||||
os.remove(pid_path)
|
|
||||||
else:
|
|
||||||
traceback.print_exc(file = sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
def restart(pid_path):
|
|
||||||
stop(pid_path)
|
|
||||||
start(pid_path, False, False, False)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
if sys.platform.startswith("win"):
|
config_path = 'config.json'
|
||||||
start(None, verbose = True)
|
|
||||||
|
|
||||||
|
if len(sys.argv) == 2:
|
||||||
|
pass
|
||||||
|
elif len(sys.argv) == 3:
|
||||||
|
config_path = sys.argv[2]
|
||||||
else:
|
else:
|
||||||
import daemon
|
print("usage: %s verbose|profile|debug" % sys.argv[0])
|
||||||
from daemon import pidfile
|
sys.exit(2)
|
||||||
from signal import SIGTERM
|
|
||||||
pid_path = "tra-daemon.pid"
|
if 'verbose' == sys.argv[1]:
|
||||||
if len(sys.argv) == 2:
|
start(None, True, False, False, config_path = config_path)
|
||||||
if 'start' == sys.argv[1]:
|
elif 'profile' == sys.argv[1]:
|
||||||
start(pid_path, False, False, False)
|
start(None, True, False, False, config_path = config_path)
|
||||||
elif 'stop' == sys.argv[1]:
|
elif 'debug' == sys.argv[1]:
|
||||||
stop(pid_path)
|
start(None, True, False, False, config_path = config_path)
|
||||||
elif 'restart' == sys.argv[1]:
|
else:
|
||||||
restart(pid_path)
|
print("usage: %s verbose|profile|debug" % sys.argv[0])
|
||||||
elif 'verbose' == sys.argv[1]:
|
sys.exit(2)
|
||||||
start(None, True, False, False)
|
|
||||||
elif 'profile' == sys.argv[1]:
|
sys.exit(0)
|
||||||
start(None, False, True, False)
|
|
||||||
elif 'debug' == sys.argv[1]:
|
|
||||||
start(None, False, False, True)
|
|
||||||
else:
|
|
||||||
print("usage: %s start|stop|restart|verbose|profile|debug" % sys.argv[0])
|
|
||||||
sys.exit(2)
|
|
||||||
sys.exit(0)
|
|
||||||
else:
|
|
||||||
print("usage: %s start|stop|restart|verbose|profile|debug" % sys.argv[0])
|
|
||||||
sys.exit(2)
|
|
Loading…
x
Reference in New Issue
Block a user