13.2.17. camcops_server.camcops_server_core

camcops_server/camcops_server_core.py


Copyright (C) 2012-2019 Rudolf Cardinal (rudolf@pobox.com).

This file is part of CamCOPS.

CamCOPS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

CamCOPS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with CamCOPS. If not, see <http://www.gnu.org/licenses/>.


Main functions used by camcops_server.py

We split these off, because the imports can be very slow, and we want a rapid command-line response for simple commands.

Importing this module does the following:

  • ensure that all models are loaded;
  • provide log message around some of the slow imports.
camcops_server.camcops_server_core.cmd_export(recipient_names: List[str] = None, all_recipients: bool = False, via_index: bool = True) → None[source]

Send all outbound incremental export messages (e.g. HL7).

Parameters:
  • recipient_names – list of export recipient names (as per the config file)
  • all_recipients – use all recipients?
  • via_index – use the task index (faster)?
camcops_server.camcops_server_core.cmd_show_export_queue(recipient_names: List[str] = None, all_recipients: bool = False, via_index: bool = True, pretty: bool = False) → None[source]

Shows tasks that would be exported.

Parameters:
  • recipient_names – list of export recipient names (as per the config file)
  • all_recipients – use all recipients?
  • via_index – use the task index (faster)?
  • pretty – use str(task) not repr(task) (prettier, slower because it has to query the patient)
camcops_server.camcops_server_core.dev_cli() → None[source]

Fire up a developer debug command-line.

camcops_server.camcops_server_core.enable_user_cli(username: str = None) → bool[source]

Re-enable a locked user account from the command line.

camcops_server.camcops_server_core.ensure_database_is_ok() → None[source]

Opens a link to the database and checks it’s of the correct version (or otherwise raises an assertion error).

camcops_server.camcops_server_core.ensure_ok_for_webserver() → None[source]

Prerequisites for firing up the web server.

camcops_server.camcops_server_core.get_new_password_from_cli(username: str) → str[source]

Asks the user (via stdout/stdin) for a new password for the specified username. Returns the password.

camcops_server.camcops_server_core.get_username_from_cli(req: camcops_server.cc_modules.cc_request.CamcopsRequest, prompt: str, starting_username: str = '', must_exist: bool = False, must_not_exist: bool = False) → str[source]

Asks the user (via stdout/stdin) for a username.

Parameters:
  • req – CamcopsRequest object
  • prompt – textual prompt
  • starting_username – try this username and ask only if it fails tests
  • must_exist – the username must exist
  • must_not_exist – the username must not exist
Returns:

the username

camcops_server.camcops_server_core.join_url_fragments(*fragments) → str[source]

Combines fragments to make a URL.

(urllib.parse.urljoin doesn’t do what we want.)

camcops_server.camcops_server_core.launch_celery_beat(verbose: bool = False) → None[source]

Launch the Celery Beat scheduler.

(This can be combined with celery worker, but that’s not recommended; http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#starting-the-scheduler).

camcops_server.camcops_server_core.launch_celery_flower(address: str = '127.0.0.1', port: int = 5555) → None[source]

Launch the Celery Flower monitor.

camcops_server.camcops_server_core.launch_celery_workers(verbose: bool = False) → None[source]

Launch Celery workers.

See also advice in

camcops_server.camcops_server_core.make_superuser(username: str = None) → bool[source]

Make a superuser from the command line.

camcops_server.camcops_server_core.make_wsgi_app(debug_toolbar: bool = False, reverse_proxied_config: cardinal_pythonlib.wsgi.reverse_proxied_mw.ReverseProxiedConfig = None, debug_reverse_proxy: bool = False) → Router[source]

Makes and returns a WSGI application, attaching all our special methods.

QUESTION: how do we access the WSGI environment (passed to the WSGI app) from within a Pyramid request?

ANSWER:

Configurator.make_wsgi_app() calls Router.__init__()
and returns: app = Router(...)
The WSGI framework uses: response = app(environ, start_response)
which therefore calls: Router.__call__(environ, start_response)
which does:
      response = self.execution_policy(environ, self)
      return response(environ, start_response)
So something LIKE this will be called:
      Router.default_execution_policy(environ, router)
          with router.request_context(environ) as request:
              # ...
So the environ is handled by Router.request_context(environ)
which will call BaseRequest.__init__()
which does:
      d = self.__dict__
      d['environ'] = environ
so we should be able to use
      request.environ  # type: Dict[str, str]
camcops_server.camcops_server_core.precache() → None[source]

Populates the major caches.

camcops_server.camcops_server_core.print_database_title() → None[source]

Prints the database title (for the current config) to stdout.

camcops_server.camcops_server_core.reindex(cfg: camcops_server.cc_modules.cc_config.CamcopsConfig) → None[source]

Drops and regenerates the server task index.

Parameters:cfg – a camcops_server.cc_modules.cc_config.CamcopsConfig
camcops_server.camcops_server_core.reset_password(username: str = None) → bool[source]

Reset a password from the command line.

camcops_server.camcops_server_core.self_test(show_only: bool = False) → None[source]

Run all unit tests.

camcops_server.camcops_server_core.serve_cherrypy(application: Router, host: str, port: int, unix_domain_socket_filename: str, threads_start: int, threads_max: int, server_name: str, log_screen: bool, ssl_certificate: Optional[str], ssl_private_key: Optional[str], root_path: str) → None[source]

Start CherryPy server.

  • Multithreading.
  • Any platform.
camcops_server.camcops_server_core.serve_gunicorn(application: Router, host: str, port: int, unix_domain_socket_filename: str, num_workers: int, ssl_certificate: Optional[str], ssl_private_key: Optional[str], reload: bool = False, timeout_s: int = 30, debug_show_gunicorn_options: bool = False) → None[source]

Start Gunicorn server

camcops_server.camcops_server_core.test_serve_pyramid(application: Router, host: str = '127.0.0.1', port: int = 8000) → None[source]

Launches an extremely simple Pyramid web server (via wsgiref.make_server).