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.

148 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
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_migrate(args):
  51. print("Start migration.")
  52. management.call_command("migrate")
  53. def command_run_webserver(args):
  54. print(f"Starting server with port {args.port}.")
  55. if is_windows():
  56. run_on_windows(args)
  57. else:
  58. run_on_nix(args)
  59. def command_run_task_queue(args):
  60. print("Starting task queue.")
  61. argv = [
  62. "--app=config",
  63. "--workdir={}".format(base),
  64. "worker",
  65. "--loglevel=info",
  66. "--concurrency={}".format(args.concurrency),
  67. ]
  68. if is_windows():
  69. argv.append("--pool=solo")
  70. app.worker_main(argv=argv)
  71. def command_help(args):
  72. print(parser.parse_args([args.command, "--help"]))
  73. def main():
  74. # Create a command line parser.
  75. subparsers = parser.add_subparsers()
  76. # Create a parser for db initialization.
  77. parser_init = subparsers.add_parser("init", help="see `init -h`")
  78. parser_init.set_defaults(handler=command_db_init)
  79. # Create a parser for migration.
  80. parser_migration = subparsers.add_parser("migrate", help="Updates database schema.")
  81. parser_migration.set_defaults(handler=command_migrate)
  82. # Create a parser for user creation.
  83. parser_create_user = subparsers.add_parser("createuser", help="see `createuser -h`")
  84. parser_create_user.add_argument("--username", type=str, default="admin", help="admin username")
  85. parser_create_user.add_argument("--password", type=str, default="password", help="admin password")
  86. parser_create_user.add_argument("--email", type=str, default="example@example.com", help="admin email")
  87. parser_create_user.set_defaults(handler=command_user_create)
  88. # Create a parser for web server.
  89. parser_server = subparsers.add_parser("webserver", help="see `webserver -h`")
  90. parser_server.add_argument("--port", type=int, default=8000, help="port number")
  91. parser_server.set_defaults(handler=command_run_webserver)
  92. # Create a parser for task queue.
  93. parser_queue = subparsers.add_parser("task", help="see `task -h`")
  94. parser_queue.add_argument("--concurrency", type=int, default=2, help="concurrency")
  95. parser_queue.set_defaults(handler=command_run_task_queue)
  96. # Create a parser for help.
  97. parser_help = subparsers.add_parser("help", help="see `help -h`")
  98. parser_help.add_argument("command", help="command name which help is shown")
  99. parser_help.set_defaults(handler=command_help)
  100. # Dispatch handler.
  101. args = parser.parse_args()
  102. if hasattr(args, "handler"):
  103. args.handler(args)
  104. else:
  105. # If specified unknown command, show help.
  106. parser.print_help()
  107. if __name__ == "__main__":
  108. main()