Skip to content

Commit c2a2d30

Browse files
authored
Fixes description and example of the on_event callback function (#5249)
* Updates and moves the example of the function to the creating spaces section * Fixes #5246
1 parent 8117972 commit c2a2d30

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

doc/platform/app/app_roles.rst

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,12 @@ You can find the full example here: `application_role_cfg <https://github.com/ta
220220
On_event callback
221221
~~~~~~~~~~~~~~~~~
222222

223-
Since version :doc:`3.4.0 </release/3.4.0>`, you can define the ``on_event`` callback for custom roles. The ``on_event`` callback is called
223+
Since version :doc:`3.3.1 </release/3.3.0>`, you can define the ``on_event`` callback for custom roles. The ``on_event`` callback is called
224224
every time a ``box.status`` system event is broadcasted.
225225
If multiple custom roles have the ``on_event`` callback defined, these callbacks are called one after another in the order
226226
defined by roles dependencies.
227227

228-
The ``on_event`` callback returns 3 arguments, when it is called:
228+
The ``on_event`` callback accepts 3 arguments, when it is called:
229229

230230
- ``config``, which contains the configuration of the role;
231231

@@ -234,7 +234,7 @@ The ``on_event`` callback returns 3 arguments, when it is called:
234234
- ``config.apply`` if the callback was triggered by a configuration update;
235235

236236
- ``box.status`` if it was triggered by the ``box.status`` system event.
237-
- ``value``, which shows and logs the information about the instance status as in the trigger ``box.status`` system event.
237+
- ``value``, which shows the information about the instance status as in the trigger ``box.status`` system event.
238238
If the callback is triggered by a configuration update, the ``value`` shows the information of the most recent ``box.status`` system event.
239239

240240
.. NOTE::
@@ -245,30 +245,7 @@ The ``on_event`` callback returns 3 arguments, when it is called:
245245
- All ``on_event`` callbacks are executed inside of a ``pcall``. If an error is raised for a callback, it is logged
246246
with the ``error`` level and the series execution continues.
247247

248-
.. code-block:: lua
249-
250-
return {
251-
validate = function() end,
252-
apply = function()
253-
_G.foo = 0
254-
end,
255-
stop = function()
256-
_G.foo = -1
257-
_G.bar = nil
258-
end,
259-
on_event = function(config, key, value)
260-
assert(_G.foo >= 0)
261-
assert(config == 12345)
262-
if(_G.foo == 0) then
263-
assert(key == 'config.apply')
264-
else
265-
assert(key == 'box.status')
266-
end
267-
_G.is_ro = value.is_ro
268-
_G.foo = _G.foo + 1
269-
_G.bar = config
270-
end,
271-
}
248+
The example of the ``on_event`` callback is provided in the :ref:`spaces creation <roles_create_space>` article below.
272249

273250
.. _roles_create_custom_role_init:
274251

@@ -306,14 +283,38 @@ You can check an instance state by subscribing to the ``box.status`` event using
306283
-- ...
307284
end)
308285
309-
310286
.. NOTE::
311287

312288
Given that a role may be enabled when an instance is already in read-write mode,
313289
you also need to execute schema initialization code from :ref:`apply() <roles_create_custom_role_apply>`.
314290
To make sure a space is created only once, use the :ref:`if_not_exists <space_opts_if_not_exists>` option.
315291

292+
Since version :doc:`3.3.1 </release/3.3.0>`, you can define space creation in a role via
293+
the ``on_event`` :ref:`callback function <roles_create_custom_role_on_event_callback>`.
316294

295+
See the example of such definition below:
296+
297+
.. code-block:: lua
298+
299+
return {
300+
validate = function() end,
301+
apply = function() end,
302+
stop = function() end,
303+
on_event = function(config, key, value)
304+
-- Can only create spaces on RW.
305+
if value.is_ro then
306+
return
307+
end
308+
-- Assume the role config is a table.
309+
if type(config) ~= 'table' then
310+
error('Config must be a table')
311+
end
312+
local space_name = config.space_name or 'default'
313+
box.schema.space.create(space_name, {
314+
if_not_exists = true,
315+
})
316+
end
317+
}
317318
318319
.. _roles_life_cycle:
319320

0 commit comments

Comments
 (0)