-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix race condition on seeding database on startup. Improve startup logs.
If nginx got started before MongoDB/Mora was fully responsive, there was a possibility seeding the initial database data would fail. This improves the seeding process by waiting for MongoDB to be fully up before proceeding with the seeding. Seeding will be re-attempted until it succeeds. Some related improvements were made to the similar Elasticsearch setup process. We were already waiting there to ensure Elasticsearch was up before running setup, but if Elasticsearch timed out, then setup wasn't re-attempted for another 1 hour. This improves things so that setup is reattempted on startup until it succeeds. This also quiets the startup logs by only logging events when MongoDB or Elasticsearch aren't ready after the 60 second timeout period (rather than on each connection attempt).
- Loading branch information
Showing
3 changed files
with
64 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,37 @@ | ||
local deep_merge_overwrite_arrays = require "api-umbrella.utils.deep_merge_overwrite_arrays" | ||
local lock = require "resty.lock" | ||
local interval_lock = require "api-umbrella.utils.interval_lock" | ||
local mongo = require "api-umbrella.utils.mongo" | ||
local random_token = require "api-umbrella.utils.random_token" | ||
local uuid = require "resty.uuid" | ||
|
||
local nowMongoDate = { ["$date"] = { ["$numberLong"] = tostring(os.time() * 1000) } } | ||
|
||
local function wait_for_mongodb() | ||
local mongodb_alive = false | ||
local wait_time = 0 | ||
local sleep_time = 0.5 | ||
local max_time = 14 | ||
repeat | ||
local _, err = mongo.collections() | ||
if err then | ||
ngx.log(ngx.NOTICE, "failed to establish connection to mongodb (this is expected if mongodb is starting up at the same time): ", err) | ||
else | ||
mongodb_alive = true | ||
end | ||
|
||
if not mongodb_alive then | ||
ngx.sleep(sleep_time) | ||
wait_time = wait_time + sleep_time | ||
end | ||
until mongodb_alive or wait_time > max_time | ||
|
||
if mongodb_alive then | ||
return true, nil | ||
else | ||
return false, "elasticsearch was not ready within " .. max_time .."s" | ||
end | ||
end | ||
|
||
local function seed_api_keys() | ||
local keys = { | ||
-- [email protected] | ||
|
@@ -208,19 +234,24 @@ local function seed_admin_permissions() | |
end | ||
|
||
local function seed() | ||
local seed_lock = lock:new("locks", { ["timeout"] = 0 }) | ||
local _, lock_err = seed_lock:lock("seed_database") | ||
if lock_err then | ||
return | ||
local _, err = wait_for_mongodb() | ||
if not err then | ||
seed_api_keys() | ||
seed_initial_superusers() | ||
seed_admin_permissions() | ||
else | ||
ngx.log(ngx.ERR, "timed out waiting for mongodb before seeding, rerunning...") | ||
ngx.sleep(5) | ||
seed() | ||
end | ||
end | ||
|
||
seed_api_keys() | ||
seed_initial_superusers() | ||
seed_admin_permissions() | ||
local function seed_once() | ||
interval_lock.mutex_exec("seed_database", seed) | ||
end | ||
|
||
return function() | ||
local ok, err = ngx.timer.at(0, seed) | ||
local ok, err = ngx.timer.at(0, seed_once) | ||
if not ok then | ||
ngx.log(ngx.ERR, "failed to create timer: ", err) | ||
return | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters