Function box.once
-
box.once(key, function[, ...])¶ Execute a function, provided it has not been executed before. A passed value is checked to see whether the function has already been executed. If it has been executed before, nothing happens. If it has not been executed before, the function is invoked.
See an example of using
box.once()in Adding storage code.Warning: If an error occurs inside
box.once()when initializing a database, you can re-execute the failedbox.once()block without stopping the database. The solution is to delete theonceobject from the system space _schema. Saybox.space._schema:select{}, find youronceobject there and delete it.When
box.once()is used for initialization, it may be useful to wait until the database is in an appropriate state (read-only or read-write). In that case, see the functions in the Submodule box.ctl.Parameters: - key (
string) – a value that will be checked - function (
function) – a function - ... – arguments that must be passed to the function
Note
The parameter
keywill be stored in the _schema system space afterbox.once()is called in order to prevent a double run. These keys are global per replica set. So a simultaneous call ofbox.once()with the same key on two instances of the same replica set may succeed on both of them, but it’ll lead to a transaction conflict.Example
The example shows how to re-execute the
box.once()block that contains thehellokey.First, check the
_schemasystem space. The_schemaspace in the example contains twobox.onceobjects –oncebyeandoncehello:app:instance001> box.space._schema:select{} --- - - ['oncebye'] - ['oncehello'] - ['replicaset_name', 'replicaset001'] - ['replicaset_uuid', '72d2d9bf-5d9f-48c4-ba80-9d657e128fee'] - ['version', 3, 1, 0]
Delete the
oncehelloobject:app:instance001> box.space._schema:delete('oncehello') --- - ['oncehello'] ...
After that, check the
_schemaspace again:app:instance001> box.space._schema:select{} --- - - ['oncebye'] - ['replicaset_name', 'replicaset001'] - ['replicaset_uuid', '72d2d9bf-5d9f-48c4-ba80-9d657e128fee'] - ['version', 3, 1, 0] ...
To re-execute the function, call the
box.once()method again:app:instance001> box.once('hello', function() end) --- ... app:instance001> box.space._schema:select{} --- - - ['oncebye'] - ['oncehello'] - ['replicaset_name', 'replicaset001'] - ['replicaset_uuid', '72d2d9bf-5d9f-48c4-ba80-9d657e128fee'] - ['version', 3, 1, 0] ...
- key (