Skip to content

Commit 0cb9101

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`) Part of #8862 NO_DOC=reflected in tarantool/doc#3544
1 parent 06ca83c commit 0cb9101

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+
* It is now possible to access configuration from the application script using
4+
the `config:get()` method (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
@@ -279,10 +279,6 @@ function methods._apply_on_startup(self, opts)
279279
end
280280

281281
self._configdata_applied = self._configdata
282-
283-
if extras ~= nil then
284-
extras.post_apply(self)
285-
end
286282
else
287283
assert(false)
288284
end
@@ -294,12 +290,35 @@ function methods._apply(self)
294290
end
295291

296292
self._configdata_applied = self._configdata
293+
end
294+
295+
function methods._post_apply(self)
296+
for _, applier in ipairs(self._appliers) do
297+
if applier.post_apply ~= nil then
298+
applier.post_apply(self)
299+
end
300+
end
297301

298302
if extras ~= nil then
299303
extras.post_apply(self)
300304
end
301305
end
302306

307+
-- Set proper status depending on received alerts.
308+
function methods._set_status_based_on_alerts(self)
309+
local status = 'ready'
310+
for _, alert in pairs(self._alerts) do
311+
assert(alert.type == 'error' or alert.type == 'warn')
312+
if alert.type == 'error' then
313+
status = 'check_errors'
314+
break
315+
end
316+
status = 'check_warnings'
317+
end
318+
self._status = status
319+
broadcast(self)
320+
end
321+
303322
function methods._startup(self, instance_name, config_file)
304323
assert(self._status == 'uninitialized')
305324
self._status = 'startup_in_progress'
@@ -331,9 +350,10 @@ function methods._startup(self, instance_name, config_file)
331350
else
332351
self:_apply_on_startup({phase = 2})
333352
end
353+
self:_set_status_based_on_alerts()
334354

335-
self._status = 'ready'
336-
broadcast(self)
355+
self:_post_apply()
356+
self:_set_status_based_on_alerts()
337357
end
338358

339359
function methods.get(self, path)
@@ -360,27 +380,21 @@ function methods._reload_noexc(self, opts)
360380

361381
self._alerts = {}
362382
self._metadata = {}
363-
local ok, err = pcall(self._collect, self, opts)
364-
if ok then
365-
ok, err = pcall(self._apply, self)
366-
end
383+
384+
local ok, err = pcall(function(opts)
385+
self:_collect(opts)
386+
self:_apply()
387+
self:_set_status_based_on_alerts()
388+
self:_post_apply()
389+
end, opts)
390+
367391
assert(not ok or err == nil)
368392
if not ok then
369393
self:_alert({type = 'error', message = err})
370394
end
371395

372-
-- Set proper status depending on received alerts.
373-
local status = 'ready'
374-
for _, alert in pairs(self._alerts) do
375-
assert(alert.type == 'error' or alert.type == 'warn')
376-
if alert.type == 'error' then
377-
status = 'check_errors'
378-
break
379-
end
380-
status = 'check_warnings'
381-
end
382-
self._status = status
383-
broadcast(self)
396+
self:_set_status_based_on_alerts()
397+
384398
return ok, err
385399
end
386400

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)