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.

121 lines
3.9 KiB

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