event_system.rst 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. Working with the CloudBridge Event System
  2. =========================================
  3. In order to provide more comprehensive logging and standardize CloudBridge
  4. functions, we have adopted a middleware layer to handle event calls. In short,
  5. each event has a corresponding list of dispatchers called in priority order.
  6. For the time being, only a listening subscription model is implemented, thus
  7. each event has a series of subscribed methods accepting the same parameters,
  8. that get run in priority order along with the main function call.
  9. This Event System allows both developers and users to easily add
  10. intermediary functions by event name, without having to modify the
  11. pre-existing code, thus improving the library's flexibility.
  12. Event Handler
  13. -------------
  14. Each function attached to an event has a corresponding handler. This handler
  15. has a type, a callback function, and a link to the next handler. When
  16. invoked, the handler will call its callback function and, when available,
  17. invoke the next handler in the linked list of handlers.
  18. Handler Types
  19. -------------
  20. Each Event Handler has a type, which determines how it's invoked. There are
  21. currently two supported types: `SUBSCRIPTION`, and `RESULT_SUBSCRIPTION`.
  22. Handlers of `SUBSCRIPTION` type are simple listeners, who intercept the main
  23. function arguments but do not modify them. They are independent of any
  24. previous or future handler, and have no return value. Their associated
  25. callback function expects the exact same parameters as the main function.
  26. Handlers of `RESULT_SUBSCRIPTION` type are similar to `SUBSCRIPTION` handlers,
  27. but have access to the last non-null return value from any previous handler.
  28. They are similarly listeners, intercepting arguments without modifying them
  29. and do not return any value. Their associated callback will however be
  30. called with an additional keyword parameter named `callback_result` holding
  31. the last non-null return value from any previous handler. The callback
  32. function thus needs to accept such a parameter.
  33. Event Dispatcher
  34. ----------------
  35. A single event dispatcher is initialized with the provider, and will hold
  36. the entirety of the handlers for all events. This dispatcher handles new
  37. subscriptions and event calls. When an event is called, the dispatcher will
  38. link each handler to the next one in line, then invoke the first handler,
  39. thus triggering the chain of handlers.
  40. Priorities
  41. ----------
  42. As previously mentioned, dispatchers will be invoked in order of priority.
  43. These priorities are assigned at subscription time, and must be unique.
  44. Below are the default priorities used across events:
  45. +------------------------+----------+
  46. | Handler | Priority |
  47. +------------------------+----------+
  48. | Pre-Logger | 20000 |
  49. +------------------------+----------+
  50. | Main Function Call | 25000 |
  51. +------------------------+----------+
  52. | Post-Logger | 30000 |
  53. +------------------------+----------+
  54. The Pre- and Post- loggers represent universal loggers respectively keeping
  55. track of the event called and its parameters before the call, and the returned
  56. value after the call. The main function call represents the core function,
  57. which is not subscribed permanently, but rather called directly with the event.
  58. Example
  59. -------
  60. Below is an example of the way in which the Event System works for a simple
  61. getter.
  62. .. code-block:: python
  63. def _get(object_id):
  64. # get the object
  65. return obj
  66. def _pre_log(object_id):
  67. print("I am calling 'get' with object_id: {}".format(object_id))
  68. def _post_log(callback_result, object_id):
  69. print("Returned object {} for a 'get' request with object_id: {}"
  70. .format(callback_result, object_id))
  71. event_name = "service.get"
  72. provider.events.subscribe(event_name, priority=20000, callback=_pre_log)
  73. provider.events.subscribe(event_name, priority=30000, callback=_post_log,
  74. result_callback=True)
  75. # Public get function
  76. def get(object_id):
  77. return provider.events.interceptable_call(event_name, priority=25000,
  78. callback=_get,
  79. object_id=object_id)
  80. In the above example, calling the public `get` function will be the
  81. equivalent of calling the below function:
  82. .. code-block:: python
  83. def get(object_id):
  84. _pre_log(object_id)
  85. result = _get(object_id)
  86. _post_log(callback_result=result, object_id)
  87. return result