Skip to content

Commit 8117972

Browse files
authored
Moves description of the on_event callback to a separate subsection (#5245)
* Moves description of the on_event callback to a separate subsection * Adds links from role creation steps to all subsections mentioned in the steps (configuration schema, validation function, apply function, etc.); * Fixes #5244
1 parent 5afceb5 commit 8117972

File tree

1 file changed

+60
-30
lines changed

1 file changed

+60
-30
lines changed

doc/platform/app/app_roles.rst

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,39 +67,16 @@ Overview
6767
A custom application role is an object which implements custom functions or logic adding to Tarantool's built-in roles and roles provided by third-party Lua modules.
6868
For example, a logging role can be created to add logging functionality on top of the built-in one.
6969

70-
Since version :doc:`3.4.0 </release/3.4.0>`, you can define an ``on_event`` callback for custom roles. The ``on_event`` callback is called
71-
every time a ``box.status`` system event is broadcasted.
72-
If multiple custom roles have the ``on_event`` callback defined, these callbacks are called one after another in the order
73-
defined by roles dependencies.
74-
75-
The ``on_event`` callback returns 3 arguments, when it is called:
76-
77-
- ``config``, which contains the configuration of the role;
7870

79-
- ``key``, which reflects the trigger event and is set to:
80-
81-
- ``config.apply`` if the callback was triggered by a configuration update;
82-
83-
- ``box.status`` if it was triggered by the ``box.status`` system event.
84-
- ``value``, which shows and logs the information about the instance status as in the trigger ``box.status`` system event.
85-
If the callback is triggered by a configuration update, the ``value`` shows the information of the most recent ``box.status`` system event.
86-
87-
.. NOTE::
88-
89-
- All ``on_event`` callbacks with the ``config.apply`` key are executed as a part of the configuration process.
90-
Process statuses ``ready`` or ``check_warnings`` are reached only after all such ``on_event`` callbacks are done.
91-
92-
- All ``on_event`` callbacks are executed inside of a ``pcall``. If an error is raised for a callback, it is logged
93-
with the ``error`` level and the series execution continues.
9471

9572
Creating a custom role includes the following steps:
9673

97-
#. (Optional) Define the role configuration schema.
98-
#. Define a function that validates a role configuration.
99-
#. Define a function that applies a validated configuration.
100-
#. Define a function that stops a role.
101-
#. (Optional) Define roles from which this custom role depends on.
102-
#. (Optional) Define the ``on_event`` callback function.
74+
#. (Optional) Define the :ref:`role configuration schema <roles_create_custom_role_schema>`.
75+
#. Define a function that :ref:`validates a role configuration <roles_create_custom_role_validate>`.
76+
#. Define a function that :ref:`applies a validated configuration <roles_create_custom_role_apply>`.
77+
#. Define a function that :ref:`stops a role <roles_create_custom_role_stop>`.
78+
#. (Optional) Define roles from which this custom role :ref:`depends on <roles_create_custom_role_dependencies>`.
79+
#. (Optional) Define the ``on_event`` :ref:`callback function <roles_create_custom_role_on_event_callback>`.
10380

10481
As a result, a role module should return an object that has corresponding functions and fields specified:
10582

@@ -130,7 +107,7 @@ You can omit the optional steps and get a simple role as in the example below.
130107
stop = function() -- ... -- end,
131108
}
132109
133-
You can modify a role, for example, by adding dependencies or specifying the on_event callback.
110+
You can modify a role, for example, by adding dependencies or specifying the ``on_event`` callback.
134111
If you modify a role, you need to restart the Tarantool instance with the role in order to apply the changes.
135112

136113
.. NOTE::
@@ -238,7 +215,60 @@ This means that all the dependencies of a role should be defined in the ``roles`
238215
239216
You can find the full example here: `application_role_cfg <https://github.com/tarantool/doc/tree/latest/doc/code_snippets/snippets/config/instances.enabled/application_role_cfg>`_.
240217

218+
.. _roles_create_custom_role_on_event_callback:
219+
220+
On_event callback
221+
~~~~~~~~~~~~~~~~~
222+
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
224+
every time a ``box.status`` system event is broadcasted.
225+
If multiple custom roles have the ``on_event`` callback defined, these callbacks are called one after another in the order
226+
defined by roles dependencies.
227+
228+
The ``on_event`` callback returns 3 arguments, when it is called:
229+
230+
- ``config``, which contains the configuration of the role;
231+
232+
- ``key``, which reflects the trigger event and is set to:
233+
234+
- ``config.apply`` if the callback was triggered by a configuration update;
241235

236+
- ``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.
238+
If the callback is triggered by a configuration update, the ``value`` shows the information of the most recent ``box.status`` system event.
239+
240+
.. NOTE::
241+
242+
- All ``on_event`` callbacks with the ``config.apply`` key are executed as a part of the configuration process.
243+
Process statuses ``ready`` or ``check_warnings`` are reached only after all such ``on_event`` callbacks are done.
244+
245+
- All ``on_event`` callbacks are executed inside of a ``pcall``. If an error is raised for a callback, it is logged
246+
with the ``error`` level and the series execution continues.
247+
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+
}
242272
243273
.. _roles_create_custom_role_init:
244274

0 commit comments

Comments
 (0)