def get(self): try: result = {'code': ECODE.SUCCESS, 'msg': EMSG.SUCCESS} stat_info = self.application.stat_info() result['stat_info']={} result['stat_info']['handlers_n']=stat_info['handlers'] uptime = stat_info['uptime'] up_day = uptime // 86400 up_hour = (uptime - up_day*86400) // 3600 up_minute = (uptime - up_day*86400 - up_hour*3600) // 60 up_second = uptime - up_day*86400 - up_hour*3600 - up_minute*60 result['stat_info']['uptime']="%ddays, %dhours, %dminute, %.3fseconds" %\ (up_day, up_hour, up_minute, up_second) self.finish(result) self.set_accesslog_item('status', 'SUCCESS') except BaseError as e: LOG.error(e, exc_info=True) self.finish({'code':e.e_code, 'msg': '%s' % e}) except Exception as e: LOG.error(e, exc_info=True) self.finish({'code':ECODE.DEFAULT, 'msg': 'Unknown'}) finally: self.write_accesslog()
def get(self): try: result = {'code': ECODE.SUCCESS, 'msg': EMSG.SUCCESS} stat_info = self.application.stat_info() result['stat_info'] = {} result['stat_info']['handlers_n'] = stat_info['handlers'] uptime = stat_info['uptime'] up_day = uptime // 86400 up_hour = (uptime - up_day * 86400) // 3600 up_minute = (uptime - up_day * 86400 - up_hour * 3600) // 60 up_second = uptime - up_day * 86400 - up_hour * 3600 - up_minute * 60 result['stat_info']['uptime'] = "%ddays, %dhours, %dminute, \ %.3fseconds" % ( up_day, up_hour, up_minute, up_second) self.finish(result) self.set_accesslog_item('status', 'SUCCESS') except BaseError as e: LOG.error(e, exc_info=True) self.finish({'code': e.e_code, 'msg': '%s' % e}) except Exception as e: LOG.error(e, exc_info=True) self.finish({'code': ECODE.DEFAULT, 'msg': 'Unknown'}) finally: self.write_accesslog()
def start(self): def kill_server(sig, frame): LOG.warning( 'Catch SIG: %d' % sig ) tornado.ioloop.IOLoop.instance().stop() # ignore Broken Pipe signal signal.signal(signal.SIGPIPE, signal.SIG_IGN); # catch kill signal signal.signal(signal.SIGINT, kill_server) signal.signal(signal.SIGQUIT, kill_server) signal.signal(signal.SIGTERM, kill_server) signal.signal(signal.SIGHUP, kill_server) for log_name in self.log_list: mownfish.util.log.setup(log_name) LOG.info('START TORNADO WEB SERVER ...') for key, value in sorted(options.items(), key=lambda d:d[0]): if key not in ('help', 'log_file_prefix', 'log_to_stderr') \ and value.value() is None: sys.stderr.write('must specify %s\n' % key) options.print_help() sys.exit(0) LOG.info('Options: (%s, %s)', key, value.value()) try: sockets = tornado.netutil.bind_sockets(options.port, address=options.bind_ip, backlog=128) if not options.multiports: task_id = tornado.process.fork_processes(options.num_process) http_server = \ tornado.httpserver.HTTPServer(xheaders=True, request_callback=self.application) http_server.add_sockets(sockets) self.prepare() tornado.ioloop.IOLoop.instance().start() http_server.stop() tornado.ioloop.IOLoop.instance().stop() LOG.info('STOP TORNADO WEB SERVER ...') except socket.error as e: LOG.warning('Socket Error: %s' % str(e)) except KeyboardInterrupt as e: LOG.warning('Gently Quit') except Exception as e: LOG.error('UnCaught Exception: %s' % e, exc_info=True)
def start(self): def kill_server(sig, frame): LOG.warning('Catch SIG: %d' % sig) tornado.ioloop.IOLoop.instance().stop() # ignore Broken Pipe signal signal.signal(signal.SIGPIPE, signal.SIG_IGN) # catch kill signal signal.signal(signal.SIGINT, kill_server) signal.signal(signal.SIGQUIT, kill_server) signal.signal(signal.SIGTERM, kill_server) signal.signal(signal.SIGHUP, kill_server) for log_name in self.log_list: mownfish.util.log.setup(log_name) LOG.info('START TORNADO WEB SERVER ...') for key, value in sorted(options.items(), key=lambda d: d[0]): if key not in ('help', 'log_file_prefix', 'log_to_stderr') \ and value.value() is None: sys.stderr.write('must specify %s\n' % key) options.print_help() sys.exit(0) LOG.info('Options: (%s, %s)', key, value.value()) try: sockets = tornado.netutil.bind_sockets(options.port, address=options.bind_ip, backlog=128) if not options.multiports: task_id = tornado.process.fork_processes(options.num_process) http_server = \ tornado.httpserver.HTTPServer(xheaders=True, request_callback=self.application) http_server.add_sockets(sockets) self.prepare() tornado.ioloop.IOLoop.instance().start() http_server.stop() tornado.ioloop.IOLoop.instance().stop() LOG.info('STOP TORNADO WEB SERVER ...') except socket.error as e: LOG.warning('Socket Error: %s' % str(e)) except KeyboardInterrupt as e: LOG.warning('Gently Quit') except Exception as e: LOG.error('UnCaught Exception: %s' % e, exc_info=True)
def get(self): try: # 只检查参数,不作业务逻辑处理 name = self._check_argument('name', expect_types=(str, unicode)) self.finish({'code': ECODE.SUCCESS, 'msg': u'Hello! %s' % name}) if __debug__: LOG.debug(self) except BaseError, e: LOG.error(e, exc_info=True) self.finish({'code':e.e_code, 'msg': '%s' % e})
def get(self): try: result = ('<h2>Hello Mownfish</h2>' ' It has never been so easy to create a tornado project') self.finish(result) self.set_accesslog_item('status', 'SUCCESS') except BaseError as e: LOG.error(e, exc_info=True) self.finish({'code': e.e_code, 'msg': '%s' % e}) except Exception as e: LOG.error(e, exc_info=True) self.finish({'code': ECODE.DEFAULT, 'msg': 'Unknown'}) finally: self.write_accesslog()
def start(self): def kill_server(sig, frame): LOG.warning('Catch SIG: %d' % sig) tornado.ioloop.IOLoop.instance().stop() # 忽略Broken Pipe信号 signal.signal(signal.SIGPIPE, signal.SIG_IGN) # 处理kill信号 signal.signal(signal.SIGINT, kill_server) signal.signal(signal.SIGQUIT, kill_server) signal.signal(signal.SIGTERM, kill_server) signal.signal(signal.SIGHUP, kill_server) LOG.info('START TORNADO WEB SERVER ...') for key, value in options.iteritems(): LOG.info('Options: (%s, %s)', key, value.value()) try: sockets = tornado.netutil.bind_sockets(options.port, address=options.bind_ip, backlog=128) http_server = \ tornado.httpserver.HTTPServer(xheaders=True, request_callback=TApplication()) http_server.add_sockets(sockets) tt = timer_task.TimerTask() tt.start() tornado.ioloop.IOLoop.instance().start() http_server.stop() tornado.ioloop.IOLoop.instance().close() LOG.info('STOP TORNADO WEB SERVER ...') except socket.error as e: LOG.warning('Socket Error: %s', str(e)) except KeyboardInterrupt as e: LOG.warning('Gently Quit') except Exception as e: LOG.error('UnCaught Exception: %s', e)
class HelloHandler(BaseHandler): @tornado.web.asynchronous def get(self): try: # 只检查参数,不作业务逻辑处理 name = self._check_argument('name', expect_types=(str, unicode)) self.finish({'code': ECODE.SUCCESS, 'msg': u'Hello! %s' % name}) if __debug__: LOG.debug(self) except BaseError, e: LOG.error(e, exc_info=True) self.finish({'code':e.e_code, 'msg': '%s' % e}) except Exception, e: LOG.error(e, exc_info=True) self.finish({'code':ECODE.DEFAULT, 'msg': 'Unknown'})