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.

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