dbt-copilot-python 0.1.5.dev1__tar.gz → 0.1.5.dev2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: dbt-copilot-python
3
- Version: 0.1.5.dev1
3
+ Version: 0.1.5.dev2
4
4
  Summary: Helper functions to run Django and Flask applications in AWS Copilot/ECS.
5
5
  Author: Department for Business and Trade Platform Team
6
6
  Author-email: sre-team@digital.trade.gov.uk
@@ -16,6 +16,9 @@ Requires-Dist: opentelemetry-exporter-otlp (==1.22.0)
16
16
  Requires-Dist: opentelemetry-instrumentation-wsgi (==0.43b0)
17
17
  Requires-Dist: opentelemetry-propagator-aws-xray (>=1.0.1,<2.0.0)
18
18
  Requires-Dist: opentelemetry-sdk-extension-aws (>=2.0.1,<3.0.0)
19
+ Requires-Dist: pytest-celery (>=0.0.0,<0.0.1)
20
+ Requires-Dist: pytest-mock (>=3.12.0,<4.0.0)
21
+ Requires-Dist: redis (>=5.0.1,<6.0.0)
19
22
  Requires-Dist: requests (>=2.31.0,<3.0.0)
20
23
  Description-Content-Type: text/markdown
21
24
 
@@ -29,7 +32,7 @@ A set of utility functions for running Django & Flask apps in AWS ECS via AWS Co
29
32
 
30
33
  ### Installation
31
34
 
32
- ```
35
+ ```shell
33
36
  pip install dbt-copilot-python
34
37
  ```
35
38
 
@@ -39,7 +42,7 @@ pip install dbt-copilot-python
39
42
 
40
43
  Add the ECS container IP to `ALLOWED_HOSTS` setting so that the Application Load Balancer (ALB) healthcheck will succeed:
41
44
 
42
- ```
45
+ ```python
43
46
  from dbt_copilot_python.network import setup_allowed_hosts
44
47
 
45
48
  ALLOWED_HOSTS = [...]
@@ -49,6 +52,17 @@ ALLOWED_HOSTS = setup_allowed_hosts(ALLOWED_HOSTS)
49
52
 
50
53
  #### Celery health check
51
54
 
55
+ Add the health check in your application's Celery config file...
56
+
57
+ ```python
58
+ from dbt_copilot_python.celery_health_check import healthcheck
59
+
60
+ celery_app = Celery("application_name")
61
+ ...
62
+
63
+ celery_app = healthcheck.setup(celery_app)
64
+ ```
65
+
52
66
  Add the health check to the Celery worker service in `docker-compose.yml`...
53
67
 
54
68
  ```yaml
@@ -79,7 +93,7 @@ To configure the `DATABASES` setting from an RDS JSON object stored in AWS Secre
79
93
 
80
94
  Note: This is dependent on the [`dj-database-url`](https://pypi.org/project/dj-database-url/) package which can be installed via `pip install dj-database-url`.
81
95
 
82
- ```
96
+ ```python
83
97
  import dj_database_url
84
98
 
85
99
  from dbt_copilot_python.database import database_url_from_env
@@ -93,7 +107,7 @@ To configure the `DATABASES` setting from an RDS JSON object stored in AWS Secre
93
107
 
94
108
  2. Configure the `DATABASES` setting to use a dictionary containing the settings:
95
109
 
96
- ```
110
+ ```python
97
111
  from dbt-copilot-python.database import database_from_env
98
112
 
99
113
  DATABASES = database_from_env("DATABASE_ENV_VAR_KEY")
@@ -107,13 +121,13 @@ To configure the `DATABASES` setting from an RDS JSON object stored in AWS Secre
107
121
 
108
122
  ### Install dependencies & pre-commit hooks
109
123
 
110
- ```
124
+ ```shell
111
125
  poetry install && poetry run pre-commit install
112
126
  ```
113
127
 
114
128
  ### Run the tests
115
129
 
116
- ```
130
+ ```shell
117
131
  poetry run pytest
118
132
  ```
119
133
 
@@ -128,7 +142,7 @@ To publish the Python package `dbt-copilot-python`, you will need an API token.
128
142
 
129
143
  Update the version, as the same version cannot be published to PyPi.
130
144
 
131
- ```
145
+ ```shell
132
146
  poetry version patch
133
147
  ```
134
148
 
@@ -136,7 +150,7 @@ More options for the `version` command can be found in the [Poetry documentation
136
150
 
137
151
  Build the Python package.
138
152
 
139
- ```
153
+ ```shell
140
154
  poetry build
141
155
  ```
142
156
 
@@ -144,7 +158,7 @@ Publish the Python package.
144
158
 
145
159
  _Note: Make sure your Pull Request (PR) is approved and contains the version upgrade in `pyproject.toml` before publishing the package._
146
160
 
147
- ```
161
+ ```shell
148
162
  poetry publish
149
163
  ```
150
164
 
@@ -8,7 +8,7 @@ A set of utility functions for running Django & Flask apps in AWS ECS via AWS Co
8
8
 
9
9
  ### Installation
10
10
 
11
- ```
11
+ ```shell
12
12
  pip install dbt-copilot-python
13
13
  ```
14
14
 
@@ -18,7 +18,7 @@ pip install dbt-copilot-python
18
18
 
19
19
  Add the ECS container IP to `ALLOWED_HOSTS` setting so that the Application Load Balancer (ALB) healthcheck will succeed:
20
20
 
21
- ```
21
+ ```python
22
22
  from dbt_copilot_python.network import setup_allowed_hosts
23
23
 
24
24
  ALLOWED_HOSTS = [...]
@@ -28,6 +28,17 @@ ALLOWED_HOSTS = setup_allowed_hosts(ALLOWED_HOSTS)
28
28
 
29
29
  #### Celery health check
30
30
 
31
+ Add the health check in your application's Celery config file...
32
+
33
+ ```python
34
+ from dbt_copilot_python.celery_health_check import healthcheck
35
+
36
+ celery_app = Celery("application_name")
37
+ ...
38
+
39
+ celery_app = healthcheck.setup(celery_app)
40
+ ```
41
+
31
42
  Add the health check to the Celery worker service in `docker-compose.yml`...
32
43
 
33
44
  ```yaml
@@ -58,7 +69,7 @@ To configure the `DATABASES` setting from an RDS JSON object stored in AWS Secre
58
69
 
59
70
  Note: This is dependent on the [`dj-database-url`](https://pypi.org/project/dj-database-url/) package which can be installed via `pip install dj-database-url`.
60
71
 
61
- ```
72
+ ```python
62
73
  import dj_database_url
63
74
 
64
75
  from dbt_copilot_python.database import database_url_from_env
@@ -72,7 +83,7 @@ To configure the `DATABASES` setting from an RDS JSON object stored in AWS Secre
72
83
 
73
84
  2. Configure the `DATABASES` setting to use a dictionary containing the settings:
74
85
 
75
- ```
86
+ ```python
76
87
  from dbt-copilot-python.database import database_from_env
77
88
 
78
89
  DATABASES = database_from_env("DATABASE_ENV_VAR_KEY")
@@ -86,13 +97,13 @@ To configure the `DATABASES` setting from an RDS JSON object stored in AWS Secre
86
97
 
87
98
  ### Install dependencies & pre-commit hooks
88
99
 
89
- ```
100
+ ```shell
90
101
  poetry install && poetry run pre-commit install
91
102
  ```
92
103
 
93
104
  ### Run the tests
94
105
 
95
- ```
106
+ ```shell
96
107
  poetry run pytest
97
108
  ```
98
109
 
@@ -107,7 +118,7 @@ To publish the Python package `dbt-copilot-python`, you will need an API token.
107
118
 
108
119
  Update the version, as the same version cannot be published to PyPi.
109
120
 
110
- ```
121
+ ```shell
111
122
  poetry version patch
112
123
  ```
113
124
 
@@ -115,7 +126,7 @@ More options for the `version` command can be found in the [Poetry documentation
115
126
 
116
127
  Build the Python package.
117
128
 
118
- ```
129
+ ```shell
119
130
  poetry build
120
131
  ```
121
132
 
@@ -123,7 +134,7 @@ Publish the Python package.
123
134
 
124
135
  _Note: Make sure your Pull Request (PR) is approved and contains the version upgrade in `pyproject.toml` before publishing the package._
125
136
 
126
- ```
137
+ ```shell
127
138
  poetry publish
128
139
  ```
129
140
 
@@ -1,29 +1,30 @@
1
1
  # See https://medium.com/ambient-innovation/health-checks-for-celery-in-kubernetes-cf3274a3e106
2
2
 
3
3
  import sys
4
+ from datetime import UTC
4
5
  from datetime import datetime
5
6
 
6
7
  from celery import signals
7
- from dateutil.tz import tz
8
8
 
9
9
  from .const import HEARTBEAT_FILE
10
10
  from .const import READINESS_FILE
11
- from .liveness_probe import LivenessProbe
11
+ from .heartbeat import HeartBeat
12
12
 
13
13
 
14
- def worker_ready(**_):
14
+ def on_worker_ready(**_):
15
15
  READINESS_FILE.touch()
16
16
 
17
17
 
18
- def worker_shutdown(**_):
18
+ def on_worker_shutdown(**_):
19
19
  READINESS_FILE.unlink(missing_ok=True)
20
20
 
21
21
 
22
22
  def setup(celery_app=None):
23
- signals.worker_ready.connect(worker_ready)
24
- signals.worker_shutdown.connect(worker_shutdown)
23
+ signals.worker_ready.connect(on_worker_ready)
25
24
 
26
- celery_app.steps["worker"].add(LivenessProbe)
25
+ signals.worker_shutdown.connect(on_worker_shutdown)
26
+
27
+ celery_app.steps["worker"].add(HeartBeat)
27
28
  return celery_app
28
29
 
29
30
 
@@ -37,7 +38,7 @@ def check_health():
37
38
  sys.exit(1)
38
39
 
39
40
  heartbeat_timestamp = float(HEARTBEAT_FILE.read_text())
40
- current_timestamp = datetime.timestamp(datetime.now(tz=tz.UTC))
41
+ current_timestamp = datetime.now(UTC).timestamp()
41
42
  time_diff = current_timestamp - heartbeat_timestamp
42
43
  if time_diff > 60:
43
44
  print(
@@ -1,13 +1,12 @@
1
+ from datetime import UTC
1
2
  from datetime import datetime
2
3
 
3
4
  from celery import bootsteps
4
- from dateutil.tz import tz
5
5
 
6
6
  from .const import HEARTBEAT_FILE
7
7
 
8
8
 
9
- # Todo: Should we call this "HeartBeat"?
10
- class LivenessProbe(bootsteps.StartStopStep):
9
+ class HeartBeat(bootsteps.StartStopStep):
11
10
  requires = {"celery.worker.components:Timer"}
12
11
 
13
12
  def __init__(self, parent, **kwargs):
@@ -27,4 +26,4 @@ class LivenessProbe(bootsteps.StartStopStep):
27
26
  HEARTBEAT_FILE.unlink(missing_ok=True)
28
27
 
29
28
  def update_heartbeat_file(self, worker):
30
- HEARTBEAT_FILE.write_text(str(datetime.timestamp(datetime.now(tz=tz.UTC))))
29
+ HEARTBEAT_FILE.write_text(str(datetime.timestamp(datetime.now(UTC))))
@@ -3,7 +3,7 @@ line-length = 100
3
3
 
4
4
  [tool.poetry]
5
5
  name = "dbt-copilot-python"
6
- version = "0.1.5-dev1"
6
+ version = "0.1.5-dev2"
7
7
  description = "Helper functions to run Django and Flask applications in AWS Copilot/ECS."
8
8
  authors = ["Department for Business and Trade Platform Team <sre-team@digital.trade.gov.uk>"]
9
9
  readme = "README.md"
@@ -18,6 +18,9 @@ opentelemetry-instrumentation-wsgi = "0.43b0"
18
18
  opentelemetry-propagator-aws-xray = "^1.0.1"
19
19
  opentelemetry-sdk-extension-aws = "^2.0.1"
20
20
  requests = "^2.31.0"
21
+ pytest-celery = "^0.0.0"
22
+ redis = "^5.0.1"
23
+ pytest-mock = "^3.12.0"
21
24
 
22
25
  [tool.poetry.group.dev.dependencies]
23
26
  pre-commit = "^3.3.3"