66
77# !/usr/bin/env python3
88import logging
9- from threading import Thread
109
1110from flask import Flask , request
1211import daemonize
13- from time import sleep
1412import json
1513import sys
1614import os
@@ -41,10 +39,13 @@ def __init__(self):
4139
4240 def main (self ):
4341 app = Flask (__name__ )
44- app .debug = False
45- app .use_reloader = True
42+ app .use_reloader = False
4643 self .perf = Perf ()
44+ self .set_app_loggers (app )
45+ self .create_app_endpoints (app )
46+ app .run (debug = False , host = "0.0.0.0" )
4747
48+ def set_app_loggers (self , app ):
4849 print ("Writting log to {}" .format (LOGNAME ))
4950 handler = logging .handlers .RotatingFileHandler (LOGNAME , maxBytes = 1024 * 1024 )
5051 logging .getLogger ("werkzeug" ).setLevel (logging .DEBUG )
@@ -53,10 +54,15 @@ def main(self):
5354 app .logger .addHandler (handler )
5455 self .perf .set_logger (app .logger )
5556
57+ def create_app_endpoints (self , app ):
5658 @app .before_first_request
5759 def before_first_request ():
5860 app .logger .setLevel (logging .INFO )
5961
62+ @app .get ("/ping" )
63+ def ping ():
64+ return json .dumps ({"result" : True })
65+
6066 @app .route ("/profiler/<profiler_name>/start/<pid>" , methods = ["POST" ])
6167 def profile_start (profiler_name , pid ):
6268 setup_process_number = 1
@@ -146,6 +152,7 @@ def profile_status(profiler_name, pid):
146152 def profile_stop (profiler_name , pid ):
147153 profile_res = self .perf .stop_profile ()
148154 profilers_artifacts_matrix = []
155+ bucket_location = "us-east-2"
149156 primary_id = 1
150157 total_primaries = 1
151158 if profile_res is True :
@@ -196,6 +203,7 @@ def profile_stop(profiler_name, pid):
196203 [profile_artifact ],
197204 S3_BUCKET_NAME ,
198205 s3_bucket_path ,
206+ bucket_location ,
199207 )
200208 s3_link = list (url_map .values ())[0 ]
201209 profilers_artifacts_matrix .append (
@@ -215,31 +223,26 @@ def profile_stop(profiler_name, pid):
215223 self .perf = Perf ()
216224 return json .dumps (status_dict )
217225
218- Thread (target = app .run ).start ()
219-
220- self .loop ()
221-
222- def loop (self ):
223- while True :
224- sleep (1 )
225-
226226
227227d = PerfDaemon ()
228228
229229
230230def main ():
231- global stop
232231 global d
233232
234- def start ():
233+ def start (foreground = False ):
235234 current_path = os .path .abspath (os .getcwd ())
236235 print (
237236 "Starting perfdaemon. PID file {}. Daemon workdir: {}" .format (
238237 PID_FILE , current_path
239238 )
240239 )
241240 daemonize .Daemonize (
242- app = "perfdaemon" , pid = PID_FILE , action = d .main , chdir = current_path
241+ app = "perfdaemon" ,
242+ pid = PID_FILE ,
243+ action = d .main ,
244+ chdir = current_path ,
245+ foreground = foreground ,
243246 ).start ()
244247
245248 def stop ():
@@ -250,7 +253,7 @@ def stop():
250253 os .system ("kill -9 %s" % pid )
251254
252255 def foreground ():
253- d . main ( )
256+ start ( True )
254257
255258 def usage ():
256259 print ("usage: start|stop|restart|foreground" )
0 commit comments