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.

135 lines
4.2 KiB

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