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.

149 lines
4.4 KiB

3 years ago
2 years ago
3 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import argparse
  2. import multiprocessing
  3. import os
  4. import platform
  5. import subprocess
  6. import sys
  7. from .app.celery import app
  8. os.environ["DEBUG"] = "False"
  9. os.environ["STANDALONE"] = "True"
  10. base = os.path.abspath(os.path.dirname(__file__))
  11. sys.path.append(base)
  12. manage_path = os.path.join(base, "manage.py")
  13. parser = argparse.ArgumentParser(description="doccano, text annotation for machine learning practitioners.")
  14. def number_of_workers():
  15. return (multiprocessing.cpu_count() * 2) + 1
  16. def is_windows():
  17. return platform.system() == "Windows"
  18. def run_on_nix(args):
  19. import gunicorn.app.base
  20. import gunicorn.util
  21. class StandaloneApplication(gunicorn.app.base.BaseApplication):
  22. def __init__(self, options=None):
  23. self.options = options or {}
  24. super().__init__()
  25. def load_config(self):
  26. config = {
  27. key: value for key, value in self.options.items() if key in self.cfg.settings and value is not None
  28. }
  29. for key, value in config.items():
  30. self.cfg.set(key.lower(), value)
  31. def load(self):
  32. return gunicorn.util.import_app("app.wsgi")
  33. options = {"bind": "%s:%s" % ("0.0.0.0", args.port), "workers": number_of_workers(), "chdir": base}
  34. StandaloneApplication(options).run()
  35. def run_on_windows(args):
  36. from waitress import serve
  37. from app.wsgi import application
  38. serve(application, port=args.port)
  39. def command_db_init(args):
  40. print("Setup Database.")
  41. subprocess.call([sys.executable, manage_path, "wait_for_db"], shell=False)
  42. subprocess.call([sys.executable, manage_path, "migrate"], shell=False)
  43. subprocess.call([sys.executable, manage_path, "create_roles"], shell=False)
  44. def command_user_create(args):
  45. print("Create admin user.")
  46. subprocess.call(
  47. [
  48. sys.executable,
  49. manage_path,
  50. "create_admin",
  51. "--username",
  52. args.username,
  53. "--password",
  54. args.password,
  55. "--email",
  56. args.email,
  57. "--noinput",
  58. ],
  59. shell=False,
  60. )
  61. def command_run_webserver(args):
  62. print(f"Starting server with port {args.port}.")
  63. if is_windows():
  64. run_on_windows(args)
  65. else:
  66. run_on_nix(args)
  67. def command_run_task_queue(args):
  68. print("Starting task queue.")
  69. argv = [
  70. "--app=app",
  71. "--workdir={}".format(base),
  72. "worker",
  73. "--loglevel=info",
  74. "--concurrency={}".format(args.concurrency),
  75. ]
  76. if is_windows():
  77. argv.append("--pool=solo")
  78. app.worker_main(argv=argv)
  79. def command_help(args):
  80. print(parser.parse_args([args.command, "--help"]))
  81. def main():
  82. # Create a command line parser.
  83. subparsers = parser.add_subparsers()
  84. # Create a parser for db initialization.
  85. parser_init = subparsers.add_parser("init", help="see `init -h`")
  86. parser_init.set_defaults(handler=command_db_init)
  87. # Create a parser for user creation.
  88. parser_create_user = subparsers.add_parser("createuser", help="see `createuser -h`")
  89. parser_create_user.add_argument("--username", type=str, default="admin", help="admin username")
  90. parser_create_user.add_argument("--password", type=str, default="password", help="admin password")
  91. parser_create_user.add_argument("--email", type=str, default="example@example.com", help="admin email")
  92. parser_create_user.set_defaults(handler=command_user_create)
  93. # Create a parser for web server.
  94. parser_server = subparsers.add_parser("webserver", help="see `webserver -h`")
  95. parser_server.add_argument("--port", type=int, default=8000, help="port number")
  96. parser_server.set_defaults(handler=command_run_webserver)
  97. # Create a parser for task queue.
  98. parser_queue = subparsers.add_parser("task", help="see `task -h`")
  99. parser_queue.add_argument("--concurrency", type=int, default=2, help="concurrency")
  100. parser_queue.set_defaults(handler=command_run_task_queue)
  101. # Create a parser for help.
  102. parser_help = subparsers.add_parser("help", help="see `help -h`")
  103. parser_help.add_argument("command", help="command name which help is shown")
  104. parser_help.set_defaults(handler=command_help)
  105. # Dispatch handler.
  106. args = parser.parse_args()
  107. if hasattr(args, "handler"):
  108. args.handler(args)
  109. else:
  110. # If specified unknown command, show help.
  111. parser.print_help()
  112. if __name__ == "__main__":
  113. main()