use signal.signal to handle SIGINT

Change-Id: I49962eb2caa92170df6d758b40c53f35cc3c54bb
This commit is contained in:
Kun Huang 2015-11-14 21:49:52 +08:00
parent 7a778c5bff
commit 28dec3510d

View File

@ -20,34 +20,33 @@ TODO:
config key-word arguments for each tracer config key-word arguments for each tracer
""" """
worker_pid = None
task_uuid = None
out = None
ag = None
def read_from_ag(ag): def read_from_ag(ag):
# wrong impl. here, need read from config or db instead # wrong impl. here, need read from config or db instead
from scalpels.client.utils import tracers_map as agents_map from scalpels.client.utils import tracers_map as agents_map
data_dir = db_api.setup_config_get()["data_dir"].rstrip("/") data_dir = db_api.setup_config_get()["data_dir"].rstrip("/")
return agents_map.get(ag) % data_dir return agents_map.get(ag) % data_dir
def main(): def handle_int(signal, frame):
task_uuid, ag = sys.argv[1], sys.argv[2] print "[LOG] xxx is interupted"
cmd = read_from_ag(ag) stop_tracer()
print "[LOG] running CMD: %s" % cmd save_result_to_task()
sys.exit(0)
worker = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
out = []
try:
while True:
t = worker.stdout.readline()
if not len(t):
break
_t = (time.time(), t.strip())
out.append(_t)
except KeyboardInterrupt:
print "[LOG] %s is interupted" % ag
def stop_tracer():
global worker_pid
# psutil is much more professional... I have to use it instead # psutil is much more professional... I have to use it instead
# this kill is to script process # this kill is to script process
worker_p = psutil.Process(worker.pid) worker_p = psutil.Process(worker_pid)
worker_p.send_signal(signal.SIGINT) worker_p.send_signal(signal.SIGINT)
def save_result_to_task():
global task_uuid
global out
parse_func = getattr(base, "parse_%s" % ag) parse_func = getattr(base, "parse_%s" % ag)
# TODO file lock is okay in localhost, here need redis for distributed # TODO file lock is okay in localhost, here need redis for distributed
@ -67,5 +66,27 @@ def main():
time.sleep(2) time.sleep(2)
co.stop() co.stop()
def main():
global worker_pid
global task_uuid
global out
global ag
signal.signal(signal.SIGINT, handle_int)
task_uuid, ag = sys.argv[1], sys.argv[2]
out = []
cmd = read_from_ag(ag)
print "[LOG] running CMD: %s" % cmd
worker = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
worker_pid = worker.pid
while True:
t = worker.stdout.readline()
if not len(t):
break
_t = (time.time(), t.strip())
out.append(_t)
if __name__ == "__main__": if __name__ == "__main__":
main() main()