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
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. os.environ['DEBUG'] = 'False'
  9. base = os.path.abspath(os.path.dirname(__file__))
  10. sys.path.append(base)
  11. manage_path = os.path.join(base, 'manage.py')
  12. parser = argparse.ArgumentParser(description='doccano, text annotation for machine learning practitioners.')
  13. def number_of_workers():
  14. return (multiprocessing.cpu_count() * 2) + 1
  15. def run_on_nix(args):
  16. import gunicorn.app.base
  17. import gunicorn.util
  18. class StandaloneApplication(gunicorn.app.base.BaseApplication):
  19. def __init__(self, options=None):
  20. self.options = options or {}
  21. super().__init__()
  22. def load_config(self):
  23. config = {key: value for key, value in self.options.items()
  24. if key in self.cfg.settings and value is not None}
  25. for key, value in config.items():
  26. self.cfg.set(key.lower(), value)
  27. def load(self):
  28. return gunicorn.util.import_app('app.wsgi')
  29. options = {
  30. 'bind': '%s:%s' % ('0.0.0.0', args.port),
  31. 'workers': number_of_workers(),
  32. 'chdir': base
  33. }
  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([sys.executable, manage_path, 'create_admin',
  47. '--username', args.username,
  48. '--password', args.password,
  49. '--email', args.email,
  50. '--noinput'], shell=False)
  51. def command_run_webserver(args):
  52. print(f'Starting server with port {args.port}.')
  53. if platform.system() == 'Windows':
  54. run_on_windows(args)
  55. else:
  56. run_on_nix(args)
  57. def command_run_task_queue(args):
  58. print('Starting task queue.')
  59. app.worker_main(
  60. argv=[
  61. '--app=app',
  62. '--workdir={}'.format(base),
  63. 'worker',
  64. '--loglevel=info',
  65. '--concurrency={}'.format(args.concurrency),
  66. ]
  67. )
  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()