commit - 06ca83c9320d3419a8af1b1fe22de90f8657e721
commit + 0cb91010c2a52e1ff3d7d6d467a71e7117778413
blob - /dev/null
blob + 6673d27f2805d5faa9fabca1f74dc089ad9b8609 (mode 644)
--- /dev/null
+++ changelogs/unreleased/config-get-configuration-from-app.md
+## feature/config
+
+* It is now possible to access configuration from the application script using
+ the `config:get()` method (gh-8862).
blob - 14455723390b554a6899b3898141ef6704d26f68
blob + 372426d9400ef2ff60b3904ce216d51f3614ae12
--- src/box/lua/config/applier/app.lua
+++ src/box/lua/config/applier/app.lua
local log = require('internal.config.utils.log')
-local function apply(config)
+local function apply(_config)
+ log.verbose('app.apply: do nothing')
+end
+
+local function post_apply(config)
local configdata = config._configdata
local file = configdata:get('app.file', {use_default = true})
local module = configdata:get('app.module', {use_default = true})
if file ~= nil then
assert(module == nil)
local fn = assert(loadfile(file))
- log.verbose('app.apply: loading '..file)
+ log.verbose('app.post_apply: loading '..file)
fn(file)
elseif module ~= nil then
- log.verbose('app.apply: loading '..module)
+ log.verbose('app.post_apply: loading '..module)
require(module)
end
end
return {
name = 'app',
apply = apply,
+ post_apply = post_apply,
}
blob - 6d49cc98131ce806035a6bee8e0de0273a98f4f9
blob + c83ca07d0068f20ac5f66164a771188109044fba
--- src/box/lua/config/init.lua
+++ src/box/lua/config/init.lua
end
self._configdata_applied = self._configdata
-
- if extras ~= nil then
- extras.post_apply(self)
- end
else
assert(false)
end
end
self._configdata_applied = self._configdata
+end
+
+function methods._post_apply(self)
+ for _, applier in ipairs(self._appliers) do
+ if applier.post_apply ~= nil then
+ applier.post_apply(self)
+ end
+ end
if extras ~= nil then
extras.post_apply(self)
end
end
+-- Set proper status depending on received alerts.
+function methods._set_status_based_on_alerts(self)
+ local status = 'ready'
+ for _, alert in pairs(self._alerts) do
+ assert(alert.type == 'error' or alert.type == 'warn')
+ if alert.type == 'error' then
+ status = 'check_errors'
+ break
+ end
+ status = 'check_warnings'
+ end
+ self._status = status
+ broadcast(self)
+end
+
function methods._startup(self, instance_name, config_file)
assert(self._status == 'uninitialized')
self._status = 'startup_in_progress'
else
self:_apply_on_startup({phase = 2})
end
+ self:_set_status_based_on_alerts()
- self._status = 'ready'
- broadcast(self)
+ self:_post_apply()
+ self:_set_status_based_on_alerts()
end
function methods.get(self, path)
self._alerts = {}
self._metadata = {}
- local ok, err = pcall(self._collect, self, opts)
- if ok then
- ok, err = pcall(self._apply, self)
- end
+
+ local ok, err = pcall(function(opts)
+ self:_collect(opts)
+ self:_apply()
+ self:_set_status_based_on_alerts()
+ self:_post_apply()
+ end, opts)
+
assert(not ok or err == nil)
if not ok then
self:_alert({type = 'error', message = err})
end
- -- Set proper status depending on received alerts.
- local status = 'ready'
- for _, alert in pairs(self._alerts) do
- assert(alert.type == 'error' or alert.type == 'warn')
- if alert.type == 'error' then
- status = 'check_errors'
- break
- end
- status = 'check_warnings'
- end
- self._status = status
- broadcast(self)
+ self:_set_status_based_on_alerts()
+
return ok, err
end
blob - 360789c74336a6830cfbb7332200a32b6ce4e505
blob + 77cc6a628cc0fcae4720ce8248ffee00efff16a8
--- test/config-luatest/app_test.lua
+++ test/config-luatest/app_test.lua
-- Start a script that is pointed by app.file or app.module.
--
--- TODO: Verify that the script has access to config:get() values.
+-- Verify that the script has access to config:get() values.
g.test_startup_success = function(g)
local script = [[
- -- TODO: Verify that the script has access to config:get()
- -- values.
- -- local config = require('config')
- -- assert(config:get('app.cfg.foo') == 42)
+ local config = require('config')
+ assert(config:get('app.cfg.foo') == 42)
+ local info = config:info()
+ assert(info.status == 'ready')
+ assert(#info.alerts == 0)
_G.foo = 42
]]
blob - 354ddf4faabc269a976ee7db9376e705897c1694
blob + 4298ee704e21cec51ea906fe55d3ebcb0987c628
--- test/config-luatest/appliers_test.lua
+++ test/config-luatest/appliers_test.lua
local fiber = require('internal.config.applier.fiber')
fiber.apply(config)
local app = require('internal.config.applier.app')
- local ok, err = pcall(app.apply, config)
+ local ok, err = pcall(app.post_apply, config)
%s
os.exit(0)
]]
blob - 41dca54679a78593d71eb621dbce37dab4445d2f
blob + 8a0c49239477eab1c13295966abf9dede97111a3
--- test/config-luatest/config_test.lua
+++ test/config-luatest/config_test.lua
local fiber = require('fiber')
local status = ''
box.watch('config.info', function(_, v) status = v.status end)
- while status ~= 'startup_in_progress' and
- status ~= 'reload_in_progress' do
+ while status ~= 'ready' do
fiber.sleep(0.1)
end
print(status)
local args = {'main.lua'}
local res = justrun.tarantool(dir, {}, args, opts)
t.assert_equals(res.exit_code, 0)
- local exp = {'startup_in_progress', 'ready', 'reload_in_progress', 'ready'}
+ local exp = {'ready', 'ready', 'ready', 'ready'}
t.assert_equals(res.stdout, table.concat(exp, "\n"))
end