@@ -220,12 +220,12 @@ You can find the full example here: `application_role_cfg <https://github.com/ta
220
220
On_event callback
221
221
~~~~~~~~~~~~~~~~~
222
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
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
224
224
every time a ``box.status `` system event is broadcasted.
225
225
If multiple custom roles have the ``on_event `` callback defined, these callbacks are called one after another in the order
226
226
defined by roles dependencies.
227
227
228
- The ``on_event `` callback returns 3 arguments, when it is called:
228
+ The ``on_event `` callback accepts 3 arguments, when it is called:
229
229
230
230
- ``config ``, which contains the configuration of the role;
231
231
@@ -234,7 +234,7 @@ The ``on_event`` callback returns 3 arguments, when it is called:
234
234
- ``config.apply `` if the callback was triggered by a configuration update;
235
235
236
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.
237
+ - ``value ``, which shows the information about the instance status as in the trigger ``box.status `` system event.
238
238
If the callback is triggered by a configuration update, the ``value `` shows the information of the most recent ``box.status `` system event.
239
239
240
240
.. NOTE ::
@@ -245,30 +245,7 @@ The ``on_event`` callback returns 3 arguments, when it is called:
245
245
- All ``on_event `` callbacks are executed inside of a ``pcall ``. If an error is raised for a callback, it is logged
246
246
with the ``error `` level and the series execution continues.
247
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
- }
248
+ The example of the ``on_event `` callback is provided in the :ref: `spaces creation <roles_create_space >` article below.
272
249
273
250
.. _roles_create_custom_role_init :
274
251
@@ -306,14 +283,38 @@ You can check an instance state by subscribing to the ``box.status`` event using
306
283
-- ...
307
284
end)
308
285
309
-
310
286
.. NOTE ::
311
287
312
288
Given that a role may be enabled when an instance is already in read-write mode,
313
289
you also need to execute schema initialization code from :ref: `apply() <roles_create_custom_role_apply >`.
314
290
To make sure a space is created only once, use the :ref: `if_not_exists <space_opts_if_not_exists >` option.
315
291
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 >`.
316
294
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
+ }
317
318
318
319
.. _roles_life_cycle :
319
320
0 commit comments