Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor start script to improve organization of Gunicorn and Uvicorn server settings #30

Merged
merged 1 commit into from
Apr 5, 2021

Conversation

br3ndonland
Copy link
Owner

Description

The core logic for running the Uvicorn and Gunicorn+Uvicorn servers is located in start.py. The start.start_server() method is what actually starts the servers. Gunicorn and Uvicorn are configured differently, so the start.start_server() method ended up getting quite complex in order to handle these differences.

This PR will refactor the configuration options passed to Gunicorn and Uvicorn into separate functions. The result is a start script with the same programming API and almost exactly the same line length, but improved readability and separation of concerns.

Changes

  • Refactor Gunicorn and Uvicorn options into separate functions, start.set_gunicorn_options() and start.set_uvicorn_options().
  • Remove start.set_conf_path(): this function was written in a general way to find either Gunicorn or logging configuration files. Starting with ff9155a, it became used only for Gunicorn configuration files. Now that the configuration options for Gunicorn are in a separate function, the logic from set_conf_path() can be moved there.
  • Simplify logger type annotations: simply import logging and annotate logger objects passed in as function arguments with logging.Logger instead of having a separate import for from logging import Logger.
  • Reorganize test_start.py to more clearly reflect order of start.py

Related

#2
#3
ff9155a

#2
#3
ff9155a

The core logic for running the Uvicorn and Gunicorn+Uvicorn servers is
located in start.py. The `start.start_server()` method is what actually
starts the servers. Uvicorn and Gunicorn are configured differently, so
and the `start.start_server()` method ended up getting quite complex in
order to handle these differences.

This commit will refactor the configuration options passed to Gunicorn
and Uvicorn into separate functions. The result is a start script with
the same programming API and almost exactly the same line length, but
improved readability and separation of concerns.

- Refactor Gunicorn and Uvicorn options into separate functions,
  `start.set_gunicorn_options()` and `start.set_uvicorn_options()`.
- Remove `start.set_conf_path()`: this function was written in a general
  way to find either Gunicorn or logging configuration files. Starting
  with ff9155a, it became used only for Gunicorn configuration files.
  Now that the configuration options for Gunicorn are in a separate
  function, the logic from `set_conf_path()` can be moved there.
- Simplify logger type annotations: simply `import logging` and annotate
  functions with `logging.Logger` instead of having a separate import
  for `from logging import Logger`.
- Reorganize test_start.py to more clearly reflect order of start.py
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Apr 4, 2021

Sourcery Code Quality Report

✅  Merging this PR will increase code quality in the affected files by 0.46%.

Quality metrics Before After Change
Complexity 0.89 ⭐ 0.93 ⭐ 0.04 👎
Method Length 61.58 🙂 62.23 🙂 0.65 👎
Working memory 9.87 😞 9.66 🙂 -0.21 👍
Quality 72.27% 🙂 72.73% 🙂 0.46% 👍
Other metrics Before After Change
Lines 649 717 68
Changed files Quality Before Quality After Quality Change
inboard/start.py 66.07% 🙂 68.61% 🙂 2.54% 👍
tests/test_start.py 73.42% 🙂 73.66% 🙂 0.24% 👍

Here are some functions in these files that still need a tune-up:

File Function Complexity Length Working Memory Quality Recommendation
tests/test_start.py TestStartServer.test_start_server_uvicorn_reload_dirs 2 ⭐ 167 😞 16 ⛔ 50.22% 🙂 Try splitting into smaller methods. Extract out complex expressions
inboard/start.py configure_logging 6 ⭐ 130 😞 11 😞 57.33% 🙂 Try splitting into smaller methods. Extract out complex expressions
tests/test_start.py TestStartServer.test_start_server_uvicorn_gunicorn_custom_config 0 ⭐ 143 😞 13 😞 58.48% 🙂 Try splitting into smaller methods. Extract out complex expressions
tests/test_start.py TestStartServer.test_start_server_uvicorn_gunicorn 0 ⭐ 117 🙂 14 😞 60.15% 🙂 Extract out complex expressions
tests/test_start.py TestStartServer.test_start_server_uvicorn 0 ⭐ 109 🙂 12 😞 64.16% 🙂 Extract out complex expressions

Legend and Explanation

The emojis denote the absolute quality of the code:

  • ⭐ excellent
  • 🙂 good
  • 😞 poor
  • ⛔ very poor

The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.


Please see our documentation here for details on how these metrics are calculated.

We are actively working on this report - lots more documentation and extra metrics to come!

Let us know what you think of it by mentioning @sourcery-ai in a comment.

@codecov
Copy link

codecov bot commented Apr 4, 2021

Codecov Report

Merging #30 (ad39efe) into develop (75676bb) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##           develop       #30   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            9         9           
  Lines          253       256    +3     
=========================================
+ Hits           253       256    +3     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
inboard/start.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 75676bb...ad39efe. Read the comment docs.

@br3ndonland br3ndonland merged commit 54b0f25 into develop Apr 5, 2021
@br3ndonland br3ndonland deleted the refactor-start-script branch April 5, 2021 00:05
br3ndonland added a commit that referenced this pull request Apr 18, 2021
#30

This commit will add some minor improvements to the refactor of the
Gunicorn and Uvicorn setup in start.py (added in #30).

- Pass `app_module` into `start.set_gunicorn_options()`, rather than
  appending later when running the server
- Read environment variables earlier in `start.set_uvicorn_options()` to
  provide consistent syntax when returning the dictionary at the end:
  this strategy seems opposed to the one taken in `gunicorn_conf.py`
  (read environment variables when they are used, rather than before),
  but the end result is similar (consistent syntax in the settings).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant