|
1 | 1 | require 'httpclient' |
| 2 | +require 'retryable' |
2 | 3 |
|
3 | 4 | namespace :db do |
4 | 5 | desc 'Create a Sequel migration in ./db/migrate' |
@@ -38,6 +39,13 @@ namespace :db do |
38 | 39 | migrate |
39 | 40 | end |
40 | 41 |
|
| 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 | + |
41 | 49 | desc 'Rollback migrations to the database (one migration by default)' |
42 | 50 | task :rollback, [:number_to_rollback] do |_, args| |
43 | 51 | RakeConfig.context = :migrate |
@@ -171,24 +179,6 @@ namespace :db do |
171 | 179 | end |
172 | 180 | end |
173 | 181 |
|
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 | | - |
192 | 182 | namespace :dev do |
193 | 183 | desc 'Migrate the database set in spec/support/bootstrap/db_config' |
194 | 184 | task :migrate do |
@@ -237,6 +227,20 @@ namespace :db do |
237 | 227 | task recreate: %w[parallel:drop parallel:create] |
238 | 228 | end |
239 | 229 |
|
| 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 | + |
240 | 244 | def migrate |
241 | 245 | Steno.init(Steno::Config.new(sinks: [Steno::Sink::IO.new($stdout)])) |
242 | 246 | db_logger = Steno.logger('cc.db.migrations') |
@@ -298,18 +302,4 @@ namespace :db do |
298 | 302 | end |
299 | 303 | end |
300 | 304 | 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 |
315 | 305 | end |
0 commit comments