Skip to content

Commit d9f1651

Browse files
committed
feat: support scheduled cleaning of old data (beta)
1 parent 2420ebd commit d9f1651

File tree

6 files changed

+51
-8
lines changed

6 files changed

+51
-8
lines changed

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
FROM ruby:2.6.6-alpine
22

3+
ENV SUPERCRONIC_URL=https://github.com/aptible/supercronic/releases/download/v0.1.11/supercronic-linux-amd64 \
4+
SUPERCRONIC=supercronic-linux-amd64 \
5+
SUPERCRONIC_SHA1SUM=a2e2d47078a8dafc5949491e5ea7267cc721d67c
6+
7+
RUN wget "$SUPERCRONIC_URL" \
8+
&& echo "${SUPERCRONIC_SHA1SUM} ${SUPERCRONIC}" | sha1sum -c - \
9+
&& chmod +x "$SUPERCRONIC" \
10+
&& mv "$SUPERCRONIC" "/usr/local/bin/${SUPERCRONIC}" \
11+
&& ln -s "/usr/local/bin/${SUPERCRONIC}" /usr/local/bin/supercronic
12+
313
# Installation path
414
ENV HOME=/pact_broker
515

@@ -30,6 +40,8 @@ COPY pact_broker $HOME/
3040
# Start Puma
3141
ENV RACK_ENV=production
3242
ENV PACT_BROKER_PORT_ENVIRONMENT_VARIABLE_NAME=PACT_BROKER_PORT
43+
ENV PACT_BROKER_DATABASE_BETA_CLEAN_ENABLED=false
44+
ENV PACT_BROKER_DATABASE_BETA_CLEAN_CRON_SCHEDULE="5 * * * *"
3345
ENV PACT_BROKER_PORT=9292
3446
USER ruby
3547
ENTRYPOINT ["./entrypoint.sh"]

docker-compose-dev.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ services:
55
image: postgres
66
healthcheck:
77
test: psql postgres --command "select 1" -U postgres
8-
ports:
9-
- "5432:5432"
8+
# ports:
9+
# - "5432:5432"
1010
environment:
1111
POSTGRES_USER: postgres
1212
POSTGRES_PASSWORD: password
@@ -22,14 +22,16 @@ services:
2222
PACT_BROKER_PORT_ENVIRONMENT_VARIABLE_NAME: PORT
2323
PACT_BROKER_DATABASE_URL_ENVIRONMENT_VARIABLE_NAME: DATABASE_URL
2424
DATABASE_URL: "postgres://postgres:password@postgres/postgres"
25+
PACT_BROKER_DATABASE_BETA_CLEAN_ENABLED: "true"
26+
PACT_BROKER_DATABASE_BETA_CLEAN_CRON_SCHEDULE: "* * * * *"
2527
# PACT_BROKER_DATABASE_USERNAME: postgres
2628
# PACT_BROKER_DATABASE_PASSWORD: password
2729
# PACT_BROKER_DATABASE_HOST: postgres
2830
# PACT_BROKER_DATABASE_NAME: postgres
2931
# PACT_BROKER_PORT: "9292"
3032
PORT: '9393'
3133
PACT_BROKER_LOG_LEVEL: INFO
32-
PACT_BROKER_SQL_LOG_LEVEL: INFO
34+
PACT_BROKER_SQL_LOG_LEVEL: DEBUG
3335
entrypoint: /custom-entrypoint.sh
3436
volumes:
3537
- ./script/docker/docker-compose-entrypoint.sh:/custom-entrypoint.sh

pact_broker/Rakefile

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
require_relative 'database_connection'
2-
require 'logger'
2+
require_relative 'docker_configuration'
3+
require_relative 'logger'
34
require 'pact_broker/tasks'
45

6+
def create_database_configuration
7+
dc = PactBroker::DockerConfiguration.new(ENV, OpenStruct.new)
8+
create_database_connection_from_config($logger, dc.database_configuration.tap { |it| puts it })
9+
end
10+
511
PactBroker::DB::MigrationTask.new do | task |
6-
task.database_connection = create_database_connection(Logger.new($stdout))
12+
task.database_connection = create_database_configuration
713
end
814

915
PactBroker::DB::VersionTask.new do | task |
10-
task.database_connection = create_database_connection(Logger.new($stdout))
16+
task.database_connection = create_database_configuration
1117
end
1218

1319
PactBroker::DB::CleanTask.new do | task |
14-
task.database_connection = create_database_connection(Logger.new($stdout))
20+
task.database_connection = create_database_configuration
21+
22+
if (ENV['PACT_BROKER_DATABASE_CLEAN_KEEP_VERSION_SELECTORS'] || '') != ''
23+
require 'json'
24+
task.keep_version_selectors = JSON.parse(ENV['PACT_BROKER_DATABASE_CLEAN_KEEP_VERSION_SELECTORS'])
25+
end
26+
27+
if (ENV['PACT_BROKER_DATABASE_CLEAN_VERSION_DELETION_LIMIT'] || '') != ''
28+
task.version_deletion_limit = ENV['PACT_BROKER_DATABASE_CLEAN_VERSION_DELETION_LIMIT'].to_i
29+
end
30+
31+
if (ENV['PACT_BROKER_DATABASE_CLEAN_DRY_RUN'] || '') != ''
32+
task.dry_run = ENV['PACT_BROKER_DATABASE_CLEAN_DRY_RUN'] == 'true'
33+
end
1534
end
1635

1736
PactBroker::DB::DeleteOverwrittenDataTask.new do | task |
18-
task.database_connection = create_database_connection(Logger.new($stdout))
37+
task.database_connection = create_database_configuration
1938
if ENV['PACT_BROKER_DELETE_OVERWRITTEN_DATA_AGE']
2039
task.age_in_days = ENV['PACT_BROKER_DELETE_OVERWRITTEN_DATA_AGE'].to_i
2140
else

pact_broker/clean.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#/bin/sh
2+
3+
bundle exec rake pact_broker:db:clean

pact_broker/entrypoint.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
#!/bin/sh
22

3+
if [ "${PACT_BROKER_DATABASE_BETA_CLEAN_ENABLED}" = "true" ]; then
4+
echo "Creating crontab with schedule ${PACT_BROKER_DATABASE_BETA_CLEAN_CRON_SCHEDULE} to clean database"
5+
echo "${PACT_BROKER_DATABASE_BETA_CLEAN_CRON_SCHEDULE} /pact_broker/clean.sh" >> /pact_broker/crontab
6+
/usr/local/bin/supercronic -quiet -passthrough-logs /pact_broker/crontab &
7+
fi
8+
39
bundle exec puma

pact_broker/logger.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'logger'
2+
require 'semantic_logger'
23

34
log_level = begin
45
Kernel.const_get('Logger').const_get(ENV['PACT_BROKER_LOG_LEVEL'] || 'WARN')

0 commit comments

Comments
 (0)