Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit d7a5fe3

Browse files
committed
Remove the istio-specific logic in the migrate process
Wait for the database to be available instead, so that the migrations can run in deployments without istio (e.g. with contour). [#175367511] Authored-by: Matt Royal <mroyal@pivotal.io>
1 parent 6d81c00 commit d7a5fe3

2 files changed

Lines changed: 23 additions & 33 deletions

File tree

Procfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ local-worker: bundle exec rake jobs:local
33
api-worker: bundle exec rake jobs:generic
44
clock: bundle exec rake clock:start
55
deployment-updater: bundle exec rake deployment_updater:start
6-
migrate: /bin/bash -c 'bundle exec rake db:wait_for_istio && bundle exec rake db:setup_database && bundle exec rake db:terminate_istio'
6+
migrate: /bin/bash -c 'bundle exec rake db:connect && bundle exec rake db:setup_database'
77

lib/tasks/db.rake

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'httpclient'
2+
require 'retryable'
23

34
namespace :db do
45
desc 'Create a Sequel migration in ./db/migrate'
@@ -38,6 +39,13 @@ namespace :db do
3839
migrate
3940
end
4041

42+
desc 'Make up to 5 attempts to connect to the database. Succeed it one is successful, and fail otherwise.'
43+
task :connect do
44+
RakeConfig.context = :migrate
45+
46+
connect
47+
end
48+
4149
desc 'Rollback migrations to the database (one migration by default)'
4250
task :rollback, [:number_to_rollback] do |_, args|
4351
RakeConfig.context = :migrate
@@ -171,24 +179,6 @@ namespace :db do
171179
end
172180
end
173181

174-
desc 'Wait for Istio sidecar to startup'
175-
task :wait_for_istio do
176-
puts 'Waiting indefinitely for Istio sidecar to be healthy'
177-
178-
sleep(5) until istio_sidecar_is_healthy?
179-
180-
puts 'Istio sidecar is now healthy'
181-
end
182-
183-
desc 'Terminate Istio sidecar for migration job'
184-
task :terminate_istio do
185-
puts 'Terminating Istio sidecar'
186-
187-
terminate_istio_sidecar
188-
189-
puts 'Istio sidecar is now terminated'
190-
end
191-
192182
namespace :dev do
193183
desc 'Migrate the database set in spec/support/bootstrap/db_config'
194184
task :migrate do
@@ -237,6 +227,20 @@ namespace :db do
237227
task recreate: %w[parallel:drop parallel:create]
238228
end
239229

230+
def connect
231+
Steno.init(Steno::Config.new(sinks: [Steno::Sink::IO.new($stdout)]))
232+
logger = Steno.logger('cc.db.connect')
233+
log_method = lambda do |retries, exception|
234+
logger.info("[Attempt ##{retries}] Retrying because [#{exception.class} - #{exception.message}]: #{exception.backtrace.first(5).join(' | ')}")
235+
end
236+
237+
Retryable.retryable(sleep: 1, tries: 5, log_method: log_method) do
238+
VCAP::CloudController::DB.connect(RakeConfig.config.get(:db), logger)
239+
end
240+
241+
logger.info("Successfully connected to database")
242+
end
243+
240244
def migrate
241245
Steno.init(Steno::Config.new(sinks: [Steno::Sink::IO.new($stdout)]))
242246
db_logger = Steno.logger('cc.db.migrations')
@@ -298,18 +302,4 @@ namespace :db do
298302
end
299303
end
300304
end
301-
302-
def istio_sidecar_is_healthy?
303-
client = HTTPClient.new
304-
response = client.request(:get, 'http://localhost:15020/healthz/ready')
305-
306-
response.code == 200
307-
rescue StandardError
308-
end
309-
310-
def terminate_istio_sidecar
311-
client = HTTPClient.new
312-
response = client.request(:post, 'http://localhost:15000/quitquitquit')
313-
raise StandardError.new("Failed to terminate Istio sidecar. Received response code: #{response.code}") unless response.code == 200
314-
end
315305
end

0 commit comments

Comments
 (0)