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.3 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 is_windows():
  16. return platform.system() == 'Windows'
  17. def run_on_nix(args):
  18. import gunicorn.app.base
  19. import gunicorn.util
  20. class StandaloneApplication(gunicorn.app.base.BaseApplication):
  21. def __init__(self, options=None):
  22. self.options = options or {}
  23. super().__init__()
  24. def load_config(self):
  25. config = {key: value for key, value in self.options.items()
  26. if key in self.cfg.settings and value is not None}
  27. for key, value in config.items():
  28. self.cfg.set(key.lower(), value)
  29. def load(self):
  30. return gunicorn.util.import_app('app.wsgi')
  31. options = {
  32. 'bind': '%s:%s' % ('0.0.0.0', args.port),
  33. 'workers': number_of_workers(),
  34. 'chdir': base
  35. }
  36. StandaloneApplication(options).run()
  37. def run_on_windows(args):
  38. from waitress import serve
  39. from app.wsgi import application
  40. serve(application, port=args.port)
  41. def command_db_init(args):
  42. print('Setup Database.')
  43. subprocess.call([sys.executable, manage_path, 'wait_for_db'], shell=False)
  44. subprocess.call([sys.executable, manage_path, 'migrate'], shell=False)
  45. subprocess.call([sys.executable, manage_path, 'create_roles'], shell=False)
  46. def command_user_create(args):
  47. print('Create admin user.')
  48. subprocess.call([sys.executable, manage_path, 'create_admin',
  49. '--username', args.username,
  50. '--password', args.password,
  51. '--email', args.email,
  52. '--noinput'], shell=False)
  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=app',
  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 user creation.
  80. parser_create_user = subparsers.add_parser('createuser', help='see `createuser -h`')
  81. parser_create_user.add_argument('--username', type=str, default='admin', help='admin username')
  82. parser_create_user.add_argument('--password', type=str, default='password', help='admin password')
  83. parser_create_user.add_argument('--email', type=str, default='example@example.com', help='admin email')
  84. parser_create_user.set_defaults(handler=command_user_create)
  85. # Create a parser for web server.
  86. parser_server = subparsers.add_parser('webserver', help='see `webserver -h`')
  87. parser_server.add_argument('--port', type=int, default=8000, help='port number')
  88. parser_server.set_defaults(handler=command_run_webserver)
  89. # Create a parser for task queue.
  90. parser_queue = subparsers.add_parser('task', help='see `task -h`')
  91. parser_queue.add_argument('--concurrency', type=int, default=2, help='concurrency')
  92. parser_queue.set_defaults(handler=command_run_task_queue)
  93. # Create a parser for help.
  94. parser_help = subparsers.add_parser('help', help='see `help -h`')
  95. parser_help.add_argument('command', help='command name which help is shown')
  96. parser_help.set_defaults(handler=command_help)
  97. # Dispatch handler.
  98. args = parser.parse_args()
  99. if hasattr(args, 'handler'):
  100. args.handler(args)
  101. else:
  102. # If specified unknown command, show help.
  103. parser.print_help()
  104. if __name__ == '__main__':
  105. main()