14.2.45. 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.check_index(cfg: camcops_server.cc_modules.cc_config.CamcopsConfig, show_all_bad: bool = False) → bool[source]

Checks the server task index for validity.

Parameters:
Returns:

are the indexes all good?

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_data_dictionary(filename: str, recipient_name: str, cris: bool = False) → None[source]

Writes a data dictionary for the CRATE anonymisation tool. See camcops_server.cc_export.write_crate_data_dictionary().

Parameters:
  • filename – destination filename
  • recipient_name – export recipient name
  • cris – make DD for CRIS, not CRATE
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, show_requests: bool = True, show_request_immediately: bool = True, show_response: bool = True, show_timing: bool = True) → Router[source]

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

Parameters:
  • debug_toolbar – Add the Pyramid debug toolbar?
  • reverse_proxied_config – An optional cardinal_pythonlib.wsgi.reverse_proxied_mw.ReverseProxiedConfig object giving details about a reverse proxy configuration (or details that there isn’t one)
  • debug_reverse_proxy – Show debugging information about the reverse proxy middleware, if such middleware is required?
  • show_requests – Write incoming requests to the Python log?
  • show_request_immediately – [Applicable if show_requests] Show the request immediately, so it’s written to the log before the WSGI app does its processing, and is guaranteed to be visible even if the WSGI app hangs? The only reason to use False is probably if you intend to show response and/or timing information and you want to minimize the number of lines written to the log; in this case, only a single line is written to the log (after the wrapped WSGI app has finished processing).
  • show_response – [Applicable if show_requests] Show the HTTP response code?
  • show_timing – [Applicable if show_requests] Show the time that the wrapped WSGI app took?
Returns:

the WSGI app

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: Union[str, NoneType], ssl_private_key: Union[str, NoneType], 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: Union[str, NoneType], ssl_private_key: Union[str, NoneType], 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).