Skip to content

Commit e67e9d1

Browse files
committed
config: allow to call config:get() from app script
It is convenient to access configuration using `config:get()` from the application script (`app.file` or `app.module`). However, before this commit, it was not possible, because the configuration was not considered as applied before the application script is loaded. Now, the script loading is moved into the post-apply phase. The resulting sequence of steps on startup/reload is the following. * collect configuration information (from all the sources) * <if the previous step is failed, set `check_errors` status and break> * apply the configuration (call all the appliers) * <if the previous step is failed, set `check_errors` status and break> * <set the new successful status: `ready` or `check_warnings`> * call post-apply hooks (including application script loading) * <if the previous step is failed, set `check_errors` status and break> * <set the new successful status: `ready` or `check_warnings`> I would like to briefly comment the changes in the tests. * `app_test.lua`: added the check for the new behavior (call config:get() from the app script) * `appliers_test.lua`: fixed the applier call (`.apply` -> `.post_apply`) * `config_test.lua`: fixed status observed in the app script (`*_in_progress` -> `ready`) NO_DOC=reflected in tarantool/doc#3544
1 parent f5ac0e1 commit e67e9d1

File tree

6 files changed

+57
-34
lines changed

6 files changed

+57
-34
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## feature/config
2+
3+
* Allow to access a configuration from an application script using
4+
`config:get()` (gh-8862).

src/box/lua/config/applier/app.lua

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
local log = require('internal.config.utils.log')
22

3-
local function apply(config)
3+
local function apply(_config)
4+
log.verbose('app.apply: do nothing')
5+
end
6+
7+
local function post_apply(config)
48
local configdata = config._configdata
59
local file = configdata:get('app.file', {use_default = true})
610
local module = configdata:get('app.module', {use_default = true})
711
if file ~= nil then
812
assert(module == nil)
913
local fn = assert(loadfile(file))
10-
log.verbose('app.apply: loading '..file)
14+
log.verbose('app.post_apply: loading '..file)
1115
fn(file)
1216
elseif module ~= nil then
13-
log.verbose('app.apply: loading '..module)
17+
log.verbose('app.post_apply: loading '..module)
1418
require(module)
1519
end
1620
end
1721

1822
return {
1923
name = 'app',
2024
apply = apply,
25+
post_apply = post_apply,
2126
}

src/box/lua/config/init.lua

+36-22
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,6 @@ function methods._apply_on_startup(self, opts)
273273
end
274274

275275
self._configdata_applied = self._configdata
276-
277-
if extras ~= nil then
278-
extras.post_apply(self)
279-
end
280276
else
281277
assert(false)
282278
end
@@ -288,12 +284,35 @@ function methods._apply(self)
288284
end
289285

290286
self._configdata_applied = self._configdata
287+
end
288+
289+
function methods._post_apply(self)
290+
for _, applier in ipairs(self._appliers) do
291+
if applier.post_apply ~= nil then
292+
applier.post_apply(self)
293+
end
294+
end
291295

292296
if extras ~= nil then
293297
extras.post_apply(self)
294298
end
295299
end
296300

301+
-- Set proper status depending on received alerts.
302+
function methods._set_status_based_on_alerts(self)
303+
local status = 'ready'
304+
for _, alert in pairs(self._alerts) do
305+
assert(alert.type == 'error' or alert.type == 'warn')
306+
if alert.type == 'error' then
307+
status = 'check_errors'
308+
break
309+
end
310+
status = 'check_warnings'
311+
end
312+
self._status = status
313+
broadcast(self)
314+
end
315+
297316
function methods._startup(self, instance_name, config_file)
298317
assert(self._status == 'uninitialized')
299318
self._status = 'startup_in_progress'
@@ -325,9 +344,10 @@ function methods._startup(self, instance_name, config_file)
325344
else
326345
self:_apply_on_startup({phase = 2})
327346
end
347+
self:_set_status_based_on_alerts()
328348

329-
self._status = 'ready'
330-
broadcast(self)
349+
self:_post_apply()
350+
self:_set_status_based_on_alerts()
331351
end
332352

333353
function methods.get(self, path)
@@ -354,27 +374,21 @@ function methods._reload_noexc(self, opts)
354374

355375
self._alerts = {}
356376
self._metadata = {}
357-
local ok, err = pcall(self._collect, self, opts)
358-
if ok then
359-
ok, err = pcall(self._apply, self)
360-
end
377+
378+
local ok, err = pcall(function(opts)
379+
self:_collect(opts)
380+
self:_apply()
381+
self:_set_status_based_on_alerts()
382+
self:_post_apply()
383+
end, opts)
384+
361385
assert(not ok or err == nil)
362386
if not ok then
363387
self:_alert({type = 'error', message = err})
364388
end
365389

366-
-- Set proper status depending on received alerts.
367-
local status = 'ready'
368-
for _, alert in pairs(self._alerts) do
369-
assert(alert.type == 'error' or alert.type == 'warn')
370-
if alert.type == 'error' then
371-
status = 'check_errors'
372-
break
373-
end
374-
status = 'check_warnings'
375-
end
376-
self._status = status
377-
broadcast(self)
390+
self:_set_status_based_on_alerts()
391+
378392
return ok, err
379393
end
380394

test/config-luatest/app_test.lua

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ local g = helpers.group()
55

66
-- Start a script that is pointed by app.file or app.module.
77
--
8-
-- TODO: Verify that the script has access to config:get() values.
8+
-- Verify that the script has access to config:get() values.
99
g.test_startup_success = function(g)
1010
local script = [[
11-
-- TODO: Verify that the script has access to config:get()
12-
-- values.
13-
-- local config = require('config')
14-
-- assert(config:get('app.cfg.foo') == 42)
11+
local config = require('config')
12+
assert(config:get('app.cfg.foo') == 42)
13+
local info = config:info()
14+
assert(info.status == 'ready')
15+
assert(#info.alerts == 0)
1516
1617
_G.foo = 42
1718
]]

test/config-luatest/appliers_test.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ local appliers_script = [[
5252
local fiber = require('internal.config.applier.fiber')
5353
fiber.apply(config)
5454
local app = require('internal.config.applier.app')
55-
local ok, err = pcall(app.apply, config)
55+
local ok, err = pcall(app.post_apply, config)
5656
%s
5757
os.exit(0)
5858
]]

test/config-luatest/config_test.lua

+2-3
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@ g.test_config_broadcast = function()
237237
local fiber = require('fiber')
238238
local status = ''
239239
box.watch('config.info', function(_, v) status = v.status end)
240-
while status ~= 'startup_in_progress' and
241-
status ~= 'reload_in_progress' do
240+
while status ~= 'ready' do
242241
fiber.sleep(0.1)
243242
end
244243
print(status)
@@ -249,7 +248,7 @@ g.test_config_broadcast = function()
249248
local args = {'main.lua'}
250249
local res = justrun.tarantool(dir, {}, args, opts)
251250
t.assert_equals(res.exit_code, 0)
252-
local exp = {'startup_in_progress', 'ready', 'reload_in_progress', 'ready'}
251+
local exp = {'ready', 'ready', 'ready', 'ready'}
253252
t.assert_equals(res.stdout, table.concat(exp, "\n"))
254253
end
255254

0 commit comments

Comments
 (0)