Mercurial > repos > melissacline > ucsc_xena_platform
comparison runXena.py @ 0:8bb037f88ed2
Uploaded
author | melissacline |
---|---|
date | Tue, 13 Jan 2015 23:37:23 -0500 |
parents | |
children | 14aaed60e07b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8bb037f88ed2 |
---|---|
1 #!/usr/bin/env python | |
2 # To kick off the script, run the following from the python directory: | |
3 # PYTHONPATH=`pwd` python testdaemon.py start | |
4 | |
5 #standard python libs | |
6 import logging | |
7 import os | |
8 import signal | |
9 import subprocess | |
10 import sys | |
11 import time | |
12 import traceback | |
13 import xena_utils as xena | |
14 | |
15 #third party libs | |
16 from daemon import runner | |
17 from lockfile import LockTimeout | |
18 | |
19 | |
20 class App(): | |
21 | |
22 def __init__(self): | |
23 xenaBaseDir = xena.baseDir() | |
24 if not os.path.exists(xenaBaseDir): | |
25 os.mkdir(xenaBaseDir) | |
26 self.pidfile_path = xenaBaseDir + "/xena-daemon.pid" | |
27 | |
28 self.stdin_path = '/dev/null' | |
29 self.stdout_path = '/dev/null' | |
30 self.stderr_path = '/dev/null' | |
31 self.pidfile_timeout = 5 | |
32 | |
33 def run(self): | |
34 while True: | |
35 #Main code goes here ... | |
36 #Note that logger level needs to be set to logging.DEBUG before this shows up in the logs | |
37 xenaCmdline = "java -jar %s -r %s/files -d %s/db -t %s/tmp --logfile %s/xena.log -p %s -H 0.0.0.0 --no-auto" | |
38 xenaBaseDir = xena.baseDir() | |
39 xenaCmd = xenaCmdline % (xena.jarPath(), xenaBaseDir, | |
40 xenaBaseDir, xenaBaseDir, | |
41 xenaBaseDir, xena.port()) | |
42 logger.debug("Invoking Xena VM with command %s" % (xenaCmd)) | |
43 xenaVm = subprocess.call(xenaCmd, shell=True) | |
44 logger.info("Starting Xena VM") | |
45 #logger.warn("Warning message") | |
46 #logger.error("Error message") | |
47 | |
48 | |
49 | |
50 def processListeningOnPort(portID): | |
51 cmd = "lsof -t -i :%s -sTCP:LISTEN" % portID | |
52 pid = subprocess.check_output(cmd, shell=True).rstrip() | |
53 return(int(pid)) | |
54 | |
55 | |
56 fp = open(sys.argv[2], "w") | |
57 | |
58 app = App() | |
59 logger = logging.getLogger("DaemonLog") | |
60 logger.setLevel(logging.DEBUG) | |
61 formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") | |
62 handler = logging.FileHandler(xena.baseDir() + "/xena-daemon.log") | |
63 handler.setFormatter(formatter) | |
64 logger.addHandler(handler) | |
65 | |
66 hostname = subprocess.check_output("hostname -f", shell=True).rstrip() | |
67 | |
68 # | |
69 # Check if there has been a Xena running on this system, and | |
70 # what its port number is or was. | |
71 # | |
72 xenaPort = xena.port() | |
73 if xenaPort == None: | |
74 # In this case, no Xena has been running on this sytem. | |
75 xenaIsRunning = False | |
76 else: | |
77 xenaIsRunning = xena.isRunning(xenaPort) | |
78 #rint "xena running", xenaIsRunning, "port", xenaPort | |
79 | |
80 | |
81 if sys.argv[1] == "status": | |
82 # | |
83 # When checking status, if Xena is running, then report that it's running | |
84 # with hostname and port. If it's not running, then report as such. | |
85 # | |
86 if xenaIsRunning: | |
87 fp.write("Xena VM currently running on %s:%s\n" % (hostname, xenaPort)) | |
88 fp.write("You can add %s:%s to Xena Data Hub\n" % (hostname, xenaPort)) | |
89 else: | |
90 fp.write("Xena VM is not currently running on %s\n" % (hostname)) | |
91 | |
92 elif sys.argv[1] == "start": | |
93 # | |
94 # When a start command came in, allocate a new port and prepare to start | |
95 # xena if it's not already running. If it is already running, then | |
96 # report as such. In either case, close the output file before this | |
97 # process goes into daemon mode. | |
98 # | |
99 if not xenaIsRunning: | |
100 xenaPort = xena.port(testIfAvailable=True, findNewPort=True) | |
101 fp.write("Starting Xena VM on %s:%s\n" % (hostname, xenaPort)) | |
102 fp.write("You can add %s:%s to Xena Data Hub\n" % (hostname, xenaPort)) | |
103 else: | |
104 fp.write("Xena VM already running on %s:%s\n" % (hostname, xenaPort)) | |
105 fp.write("You can add %s:%s to Xena Data Hub\n" % (hostname, xenaPort)) | |
106 fp.close() | |
107 | |
108 elif sys.argv[1] == "stop": | |
109 # | |
110 # When stopping Xena, if it's currently running, report that Xena | |
111 # is being terminated and clean up the port. If it's not running, | |
112 # report as such. | |
113 # | |
114 if xenaIsRunning: | |
115 fp.write("Terminating Xena VM on %s:%s\n" % (hostname, xenaPort)) | |
116 else: | |
117 fp.write("Xena VM is not currently running on %s\n" % (hostname)) | |
118 | |
119 else: | |
120 fp.write(("Error: Unexpected command %s" % sys.argv[1])) | |
121 | |
122 | |
123 # | |
124 # Here is where the starting and stopping of the Xena daemon takes place. | |
125 # | |
126 if sys.argv[1] == "start" or (sys.argv[1] == "stop" and xenaIsRunning): | |
127 daemon_runner = runner.DaemonRunner(app) | |
128 # This ensures that the logger file handle does not get closed during daemonization | |
129 daemon_runner.daemon_context.files_preserve=[handler.stream] | |
130 try: | |
131 daemon_runner.do_action() | |
132 except LockTimeout: | |
133 # Xena is already running. No need to do anything special, but this | |
134 # should be separated from the other exceptions. | |
135 pass | |
136 except: | |
137 exc_type, exc_value, exc_traceback = sys.exc_info() | |
138 lines = traceback.format_exception(exc_type, exc_value, exc_traceback) | |
139 allLines = ''.join('!! ' + line for line in lines) | |
140 fp.write("Unsuccessful: error %s\n" % allLines) | |
141 if sys.argv[1] == "stop": | |
142 # | |
143 # If the Xena stop command has been issued, then kill the Xena | |
144 # process. | |
145 # | |
146 xenaPid = processListeningOnPort(xenaPort) | |
147 logger.debug("Attempting to kill process with PID %d" % xenaPid) | |
148 os.kill(xenaPid, signal.SIGTERM) | |
149 if sys.argv[1] != "start": | |
150 fp.close() |