如何避免init函数失败时注册box.once('init', function ...)?
How to avoid the registration of box.once('init', function ...) when the init function fails?
我想在 box.once() 下实现一个 init 函数,在 Tarantool 中只执行一次,但是,仅当 init 已成功执行时,这对我才有用。
问题:如何让“onceinit”记录只有在init运行成功时才有效??
重现案例:
init 函数的以下代码失败,因为没有 create_space_BAD 函数,但是,扫描架构时它被注册为已执行。
关于如何解决这个问题有什么建议吗?
代码:
local function start()
box.cfg{}
box.once('init', function()
if not pcall(box.schema.create_space_BAD, 'myspace') then
print('ERROR: create_space_BAD NOT EXIST')
return false
end
...
end)
end
探索架构:
tarantool> box.space._schema:select{}
---
- - ['cluster', '1cb21086-51a3-46fb-900e-1983609fc396']
- ['max_id', 511]
- ['onceinit']
- ['version', 1, 10, 2]
...
可以使用 box.space._schema:delete('onceinit') 显式取消注册您的 init 函数来解决此问题。
喜欢:
local function start()
box.cfg{}
box.once('init', function()
if not pcall(box.schema.create_space_BAD, 'myspace') then
print('ERROR: create_space_BAD NOT EXIST')
box.space._schema:delete('onceinit')
return false
end
end)
end
然后你会看到:
tarantool> box.space._schema:select{}
---
- - ['cluster', 'd6a9d97b-3a3f-4f69-8d1a-65ae5a073c16']
- ['max_id', 511]
- ['version', 2, 3, 1]
...
有关详细信息,请参阅 https://www.tarantool.io/en/doc/1.10/reference/reference_lua/box_once/
请注意,如果您创建多个 spaces/indexes,则使用 box.space._schema:delete
的方法将不起作用。推荐的方法是使用 if_not_exists
选项而不是 box.once
.
见https://www.tarantool.io/en/doc/2.3/reference/reference_lua/box_schema/#box-schema-space-create
和
https://www.tarantool.io/en/doc/2.3/reference/reference_lua/box_space/#box-space-create-index
我想在 box.once() 下实现一个 init 函数,在 Tarantool 中只执行一次,但是,仅当 init 已成功执行时,这对我才有用。
问题:如何让“onceinit”记录只有在init运行成功时才有效??
重现案例:
init 函数的以下代码失败,因为没有 create_space_BAD 函数,但是,扫描架构时它被注册为已执行。
关于如何解决这个问题有什么建议吗?
代码:
local function start()
box.cfg{}
box.once('init', function()
if not pcall(box.schema.create_space_BAD, 'myspace') then
print('ERROR: create_space_BAD NOT EXIST')
return false
end
...
end)
end
探索架构:
tarantool> box.space._schema:select{}
---
- - ['cluster', '1cb21086-51a3-46fb-900e-1983609fc396']
- ['max_id', 511]
- ['onceinit']
- ['version', 1, 10, 2]
...
可以使用 box.space._schema:delete('onceinit') 显式取消注册您的 init 函数来解决此问题。
喜欢:
local function start()
box.cfg{}
box.once('init', function()
if not pcall(box.schema.create_space_BAD, 'myspace') then
print('ERROR: create_space_BAD NOT EXIST')
box.space._schema:delete('onceinit')
return false
end
end)
end
然后你会看到:
tarantool> box.space._schema:select{}
---
- - ['cluster', 'd6a9d97b-3a3f-4f69-8d1a-65ae5a073c16']
- ['max_id', 511]
- ['version', 2, 3, 1]
...
有关详细信息,请参阅 https://www.tarantool.io/en/doc/1.10/reference/reference_lua/box_once/
请注意,如果您创建多个 spaces/indexes,则使用 box.space._schema:delete
的方法将不起作用。推荐的方法是使用 if_not_exists
选项而不是 box.once
.
见https://www.tarantool.io/en/doc/2.3/reference/reference_lua/box_schema/#box-schema-space-create
和
https://www.tarantool.io/en/doc/2.3/reference/reference_lua/box_space/#box-space-create-index