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.

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