You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

100 lines
3.3 KiB

  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import socket
  5. import argparse
  6. import cherrypy
  7. import nilmdb.server
  8. def main():
  9. """Main entry point for the 'nilmdb-server' command line script"""
  10. parser = argparse.ArgumentParser(
  11. description='Run the NilmDB server',
  12. formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  13. parser.add_argument("-v", "--version", action="version",
  14. version=nilmdb.__version__)
  15. group = parser.add_argument_group("Standard options")
  16. group.add_argument('-a', '--address',
  17. help='Only listen on the given address',
  18. default='0.0.0.0')
  19. group.add_argument('-p', '--port', help='Listen on the given port',
  20. type=int, default=12380)
  21. group.add_argument('-d', '--database', help='Database directory',
  22. default="./db")
  23. group.add_argument('-q', '--quiet', help='Silence output',
  24. action='store_true')
  25. group.add_argument('-t', '--traceback',
  26. help='Provide tracebacks in client errors',
  27. action='store_true', default=False)
  28. group = parser.add_argument_group("Debug options")
  29. group.add_argument('-y', '--yappi', help='Run under yappi profiler and '
  30. 'invoke interactive shell afterwards',
  31. action='store_true')
  32. args = parser.parse_args()
  33. # Create database object. Needs to be serialized before passing
  34. # to the Server.
  35. db = nilmdb.utils.serializer_proxy(nilmdb.server.NilmDB)(args.database)
  36. # Configure the server
  37. if not args.quiet:
  38. cherrypy._cpconfig.environments['embedded']['log.screen'] = True
  39. server = nilmdb.server.Server(db,
  40. host=args.address,
  41. port=args.port,
  42. force_traceback=args.traceback)
  43. # Print info
  44. if not args.quiet:
  45. print("Version: %s" % nilmdb.__version__)
  46. print("Database: %s" % (os.path.realpath(args.database)))
  47. if args.address == '0.0.0.0' or args.address == '::':
  48. host = socket.getfqdn()
  49. else:
  50. host = args.address
  51. print("Server URL: http://%s:%d/" % (host, args.port))
  52. print("----")
  53. # Run it
  54. try:
  55. if args.yappi:
  56. print("Running in yappi")
  57. try:
  58. import yappi
  59. yappi.start()
  60. server.start(blocking=True)
  61. finally:
  62. yappi.stop()
  63. stats = yappi.get_func_stats()
  64. stats.sort("ttot")
  65. stats.print_all()
  66. try:
  67. from IPython import embed
  68. embed(header="Use the `yappi` or `stats` object to "
  69. "explore further, `quit` to exit")
  70. except ModuleNotFoundError:
  71. print("\nInstall ipython to explore further")
  72. else:
  73. server.start(blocking=True)
  74. except nilmdb.server.serverutil.CherryPyExit:
  75. print("Exiting due to CherryPy error", file=sys.stderr)
  76. raise
  77. finally:
  78. if not args.quiet:
  79. print("Closing database")
  80. db.close()
  81. if __name__ == "__main__":
  82. main()