Skip to content

Commit 7cdb493

Browse files
committed
feat: officially release clean feature
1 parent c376b66 commit 7cdb493

File tree

9 files changed

+177
-9
lines changed

9 files changed

+177
-9
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ RUN set -ex && \
3737

3838
# Install source
3939
COPY pact_broker $HOME/
40+
RUN mv $HOME/clean.sh /usr/local/bin/clean
4041

4142
RUN ln -s /pact_broker/script/db-migrate.sh /usr/local/bin/db-migrate
4243
RUN ln -s /pact_broker/script/db-version.sh /usr/local/bin/db-version
4344

4445
# Start Puma
4546
ENV RACK_ENV=production
4647
ENV PACT_BROKER_PORT_ENVIRONMENT_VARIABLE_NAME=PACT_BROKER_PORT
47-
ENV PACT_BROKER_DATABASE_BETA_CLEAN_ENABLED=false
48+
ENV PACT_BROKER_DATABASE_CLEAN_ENABLED=false
4849
ENV PACT_BROKER_DATABASE_CLEAN_CRON_SCHEDULE="0 2 15 * *"
4950
ENV PACT_BROKER_DATABASE_CLEAN_KEEP_VERSION_SELECTORS='[{ "latest": true, "tag": true }, { "max_age": 90 }]'
5051
ENV PACT_BROKER_DATABASE_CLEAN_DELETION_LIMIT=500

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,38 @@ Set the environment variable `PACT_BROKER_LOG_LEVEL` to one of `DEBUG`, `INFO`,
129129

130130
Documentation for the Pact Broker application itself can be found in the Pact Broker [docs][pact-broker-docs].
131131

132+
## Automatic data clean up
133+
134+
Performance can degrade when too much data accumulates in the Pact Broker. To read about the automatic data clean up feature, please see the [Maintenance](https://docs.pact.io/pact_broker/administration/maintenance) page of the Pact Broker documentation.
135+
136+
### Running the clean task on a cron schedule within the application container
137+
138+
If you have exactly one Pact Broker container running at a time, you can configure cron on the container to run the clean up.
139+
140+
* `PACT_BROKER_DATABASE_CLEAN_ENABLED`: set to `true` to enable the clean. Default is `false`.
141+
* `PACT_BROKER_DATABASE_CLEAN_CRON_SCHEDULE`: set to a cron schedule that will run when your Broker is under the least operational load. Default is 2:15am - `15 2 * * *`
142+
* `PACT_BROKER_DATABASE_CLEAN_DELETION_LIMIT`: The maximum number of records to delete at a time for each of the categories listed in the [Categories of removable data](https://docs.pact.io/pact_broker/administration/maintenance#categories-of-removable-data). Defaults to `500`.
143+
* `PACT_BROKER_DATABASE_CLEAN_OVERWRITTEN_DATA_MAX_AGE`: The maximum number of days to keep "overwritten" data as described in the [Categories of removable data](https://docs.pact.io/pact_broker/administration/maintenance#categories-of-removable-data)
144+
* `PACT_BROKER_DATABASE_CLEAN_KEEP_VERSION_SELECTORS`: a JSON string containing a list of the "keep" selectors described in [Configuring the keep selectors](https://docs.pact.io/pact_broker/administration/maintenance#configuring-the-keep-selectors) e.g `[{"latest": true, "tag": true}, {"max_age": 90}]` (remember to escape the quotes if necessary in your configuration files/console).
145+
* `PACT_BROKER_DATABASE_CLEAN_DRY_RUN`: defaults to `false`. Set to `true` to see the output of what *would* have been deleted if the task had run. This is helpful when experimenting with or fine tuning the clean feature. As nothing is deleted when in dry-run mode, the same output will be printed in the logs each time the task runs.
146+
147+
### Running the clean task from an external source
148+
149+
If you are running more than one Pact Broker Docker container at a time for the same database, then you will end up with two clean up tasks fighting with each other to delete the data. In this situation, it is best to run the clean task from an external location at a regular interval. To do this, run an instance of the pact-broker docker image with the entrypoint `clean`, the same database connection credentials as the application, and the same environment variables described in the section above *except the PACT_BROKER_DATABASE_CLEAN_ENABLED and PACT_BROKER_DATABASE_CLEAN_CRON_SCHEDULE* vars.
150+
151+
You can see a working example in the [docker-compose-clean.yml](./docker-compose-clean.yml) file. To run the example locally, run:
152+
153+
```
154+
docker-compose -f docker-compose-clean.yml up pact-broker
155+
156+
# in another console
157+
docker-compose -f docker-compose-clean.yml up clean
158+
```
159+
160+
### Known issues with the data clean up task
161+
162+
* When the pact-broker docker container gets restarted because of an internal error, another supercronic (the application that runs the cron task in the background) process seems to get started each time, leading to multiple clean tasks running at once. This issue has been noticed in local testing, but we do not know if it is likely to be an issue under normal production use. Please raise an issue if you are observing it. The mitigation for this is to run the clean from an external source as documented above.
163+
132164
## Running with Docker Compose
133165

134166
For a quick start with the Pact Broker and Postgres, we have an example

docker-compose-clean-cron.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
version: "3"
2+
3+
services:
4+
postgres:
5+
image: postgres:12
6+
healthcheck:
7+
test: psql postgres --command "select 1" -U postgres
8+
environment:
9+
POSTGRES_USER: postgres
10+
POSTGRES_PASSWORD: password
11+
POSTGRES_DB: postgres
12+
volumes:
13+
- ${PWD}/docker/postgres-entrypoint.sh:/docker-entrypoint-initdb.d/init-db.sh
14+
- ${PWD}/docker/pg-dump.sql:/tmp/pg-dump.sql
15+
16+
pact-broker:
17+
build: .
18+
ports:
19+
- "9393:9393"
20+
depends_on:
21+
- postgres
22+
environment:
23+
PACT_BROKER_DATABASE_URL: "postgres://postgres:password@postgres/postgres"
24+
PACT_BROKER_LOG_LEVEL: "INFO"
25+
PACT_BROKER_SQL_LOG_LEVEL: "DEBUG"
26+
PACT_BROKER_DATABASE_CLEAN_ENABLED: "true"
27+
PACT_BROKER_DATABASE_CONNECT_MAX_RETRIES: "5"
28+
PACT_BROKER_DATABASE_CLEAN_CRON_SCHEDULE: "* * * * *"
29+
PACT_BROKER_DATABASE_CLEAN_DELETION_LIMIT: "500"
30+
PACT_BROKER_SQL_LOG_WARN_DURATION: "60"
31+
# Keep all prod versions, AND the latest version for every pacticipant/tag, and all versions less than 30 days old
32+
PACT_BROKER_DATABASE_CLEAN_KEEP_VERSION_SELECTORS: '[{"latest": true, "tag": true}, {"max_age": 30}]'

docker-compose-clean.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
version: "3"
2+
3+
services:
4+
postgres:
5+
image: postgres:12
6+
healthcheck:
7+
test: psql postgres --command "select 1" -U postgres
8+
environment:
9+
POSTGRES_USER: postgres
10+
POSTGRES_PASSWORD: password
11+
POSTGRES_DB: postgres
12+
volumes:
13+
- ${PWD}/docker/postgres-entrypoint.sh:/docker-entrypoint-initdb.d/init-db.sh
14+
- ${PWD}/docker/pg-dump.sql:/tmp/pg-dump.sql
15+
16+
pact-broker:
17+
build: .
18+
ports:
19+
- "9393:9393"
20+
depends_on:
21+
- postgres
22+
environment:
23+
PACT_BROKER_DATABASE_URL: "postgres://postgres:password@postgres/postgres"
24+
PACT_BROKER_DATABASE_CONNECT_MAX_RETRIES: "5"
25+
26+
clean:
27+
build: .
28+
depends_on:
29+
- postgres
30+
environment:
31+
PACT_BROKER_DATABASE_URL: "postgres://postgres:password@postgres/postgres"
32+
PACT_BROKER_DATABASE_CONNECT_MAX_RETRIES: "5"
33+
PACT_BROKER_DATABASE_CLEAN_DELETION_LIMIT: "500"
34+
PACT_BROKER_SQL_LOG_WARN_DURATION: "60"
35+
# Keep all prod versions, AND the latest version for every pacticipant/tag, and all versions less than 30 days old
36+
PACT_BROKER_DATABASE_CLEAN_KEEP_VERSION_SELECTORS: '[{"latest": true, "tag": true}, {"max_age": 30}]'
37+
PACT_BROKER_LOG_LEVEL: INFO
38+
PACT_BROKER_SQL_LOG_LEVEL: DEBUG
39+
entrypoint: clean

docker-compose-dev-migrate.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
version: "3"
2+
3+
services:
4+
postgres:
5+
image: postgres:12
6+
healthcheck:
7+
test: psql postgres --command "select 1" -U postgres
8+
# ports:
9+
# - "5432:5432"
10+
environment:
11+
POSTGRES_USER: postgres
12+
POSTGRES_PASSWORD: password
13+
POSTGRES_DB: postgres
14+
volumes:
15+
- ${PWD}/docker/postgres-entrypoint.sh:/docker-entrypoint-initdb.d/init-db.sh
16+
- ${PWD}/docker/pg-dump.sql:/tmp/pg-dump.sql
17+
18+
pact-broker:
19+
build: .
20+
ports:
21+
- "9393:9393"
22+
depends_on:
23+
- postgres
24+
environment:
25+
PACT_BROKER_PORT_ENVIRONMENT_VARIABLE_NAME: PORT
26+
PACT_BROKER_DATABASE_URL_ENVIRONMENT_VARIABLE_NAME: DATABASE_URL
27+
DATABASE_URL: "postgres://postgres:password@postgres/postgres"
28+
PACT_BROKER_DATABASE_BETA_CLEAN_ENABLED: "true"
29+
PACT_BROKER_DATABASE_CLEAN_CRON_SCHEDULE: "* * * * *"
30+
PACT_BROKER_DATABASE_CLEAN_DRY_RUN: "false"
31+
PACT_BROKER_DATABASE_CLEAN_DELETION_LIMIT: "500"
32+
# Keep all prod versions, AND the latest version for every pacticipant/tag, and all versions less than 30 days old
33+
PACT_BROKER_DATABASE_CLEAN_KEEP_VERSION_SELECTORS: '[{"latest": true, "tag": true}, {"max_age": 30}]'
34+
# PACT_BROKER_DATABASE_USERNAME: postgres
35+
# PACT_BROKER_DATABASE_PASSWORD: password
36+
# PACT_BROKER_DATABASE_HOST: postgres
37+
# PACT_BROKER_DATABASE_NAME: postgres
38+
# PACT_BROKER_PORT: "9292"
39+
PORT: '9393'
40+
PACT_BROKER_LOG_LEVEL: INFO
41+
PACT_BROKER_SQL_LOG_LEVEL: DEBUG
42+
volumes:
43+
- ./script/docker/docker-compose-entrypoint.sh:/custom-entrypoint.sh
44+
45+
db-migrate:
46+
build: .
47+
depends_on:
48+
- postgres
49+
environment:
50+
PACT_BROKER_DATABASE_URL: "postgres://postgres:password@postgres/postgres"
51+
PACT_BROKER_LOG_LEVEL: INFO
52+
PACT_BROKER_SQL_LOG_LEVEL: DEBUG
53+
PACT_BROKER_MIGRATION_TARGET: "20200922"
54+
entrypoint: db-migrate
55+
56+
db-version:
57+
build: .
58+
depends_on:
59+
- postgres
60+
environment:
61+
PACT_BROKER_DATABASE_URL: "postgres://postgres:password@postgres/postgres"
62+
PACT_BROKER_LOG_LEVEL: INFO
63+
PACT_BROKER_SQL_LOG_LEVEL: DEBUG
64+
entrypoint: db-version

docker-compose-dev.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ services:
1414
volumes:
1515
- ${PWD}/docker/postgres-entrypoint.sh:/docker-entrypoint-initdb.d/init-db.sh
1616
- ${PWD}/docker/pg-dump.sql:/tmp/pg-dump.sql
17+
logging:
18+
driver: none
1719

1820
pact-broker:
1921
build: .
@@ -25,10 +27,11 @@ services:
2527
PACT_BROKER_PORT_ENVIRONMENT_VARIABLE_NAME: PORT
2628
PACT_BROKER_DATABASE_URL_ENVIRONMENT_VARIABLE_NAME: DATABASE_URL
2729
DATABASE_URL: "postgres://postgres:password@postgres/postgres"
28-
PACT_BROKER_DATABASE_BETA_CLEAN_ENABLED: "true"
30+
PACT_BROKER_DATABASE_CLEAN_ENABLED: "false"
2931
PACT_BROKER_DATABASE_CLEAN_CRON_SCHEDULE: "* * * * *"
3032
PACT_BROKER_DATABASE_CLEAN_DRY_RUN: "false"
3133
PACT_BROKER_DATABASE_CLEAN_DELETION_LIMIT: "500"
34+
PACT_BROKER_SQL_LOG_WARN_DURATION: "60"
3235
# Keep all prod versions, AND the latest version for every pacticipant/tag, and all versions less than 30 days old
3336
PACT_BROKER_DATABASE_CLEAN_KEEP_VERSION_SELECTORS: '[{"latest": true, "tag": true}, {"max_age": 30}]'
3437
PACT_BROKER_DATABASE_CONNECT_MAX_RETRIES: "10"
@@ -40,6 +43,4 @@ services:
4043
PORT: '9393'
4144
PACT_BROKER_LOG_LEVEL: INFO
4245
PACT_BROKER_SQL_LOG_LEVEL: DEBUG
43-
entrypoint: /custom-entrypoint.sh
44-
volumes:
45-
- ./script/docker/docker-compose-entrypoint.sh:/custom-entrypoint.sh
46+
PACT_BROKER_DATABASE_CONNECT_MAX_RETRIES: "5"

pact_broker/Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'pact_broker/tasks'
55

66
def create_database_configuration
77
dc = PactBroker::DockerConfiguration.new(ENV, OpenStruct.new)
8-
create_database_connection_from_config($logger, dc.database_configuration)
8+
create_database_connection_from_config($logger, dc.database_configuration, dc.database_connect_max_retries)
99
end
1010

1111
def env(name)

pact_broker/clean.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#/bin/sh
1+
#!/bin/sh
22

33
bundle exec rake pact_broker:db:clean
44
bundle exec rake pact_broker:db:delete_overwritten_data

pact_broker/entrypoint.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/bin/sh
22

3-
if [ "${PACT_BROKER_DATABASE_BETA_CLEAN_ENABLED}" = "true" ] && [ ! -f .supercronic ]; then
3+
if [ "${PACT_BROKER_DATABASE_CLEAN_ENABLED}" = "true" ]; then
44
echo "Creating crontab with schedule ${PACT_BROKER_DATABASE_CLEAN_CRON_SCHEDULE} to clean database"
55
echo "${PACT_BROKER_DATABASE_CLEAN_CRON_SCHEDULE} /pact_broker/clean.sh" >> /pact_broker/crontab
6-
touch .supercronic
76
/usr/local/bin/supercronic -quiet -passthrough-logs /pact_broker/crontab &
87
fi
98

0 commit comments

Comments
 (0)