Skip to content

Commit e71123f

Browse files
Added custom event handler section (#3853)
* Added custom event handler section * Update docs/multi_database.rst Co-authored-by: andy-stark-redis <[email protected]> * Update docs/multi_database.rst Co-authored-by: andy-stark-redis <[email protected]> --------- Co-authored-by: andy-stark-redis <[email protected]>
1 parent 8739042 commit e71123f

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

docs/multi_database.rst

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ reverse proxy behind an actual REST API endpoint.
315315
health_check_url="https://cluster.example.com",
316316
),
317317
],
318-
# Add custom checks (in addition to default PingHealthCheck)
318+
# Add custom health check to replace the default
319319
health_checks=[
320320
# Redis Enterprise REST-based lag-aware check
321321
LagAwareHealthCheck(
@@ -412,6 +412,51 @@ To enable periodic fallback to a higher-priority healthy database, set `auto_fal
412412
)
413413
client = MultiDBClient(cfg)
414414
415+
416+
Custom failover callbacks
417+
-------------------------
418+
419+
You may want to activate custom actions when failover happens. For example, you may want to collect some metrics,
420+
logs or externally persist a connection state.
421+
422+
You can register your own event listener for the `ActiveDatabaseChanged` event (which is emitted when a failover happens) using
423+
the `EventDispatcher`.
424+
425+
.. code-block:: python
426+
427+
class LogFailoverEventListener(EventListenerInterface):
428+
def __init__(self, logger: Logger):
429+
self.logger = logger
430+
431+
def listen(self, event: ActiveDatabaseChanged):
432+
self.logger.warning(
433+
f"Failover happened. Active database switched from {event.old_database} to {event.new_database}"
434+
)
435+
436+
event_dispatcher = EventDispatcher()
437+
listener = LogFailoverEventListener(logging.getLogger(__name__))
438+
439+
# Register custom listener
440+
event_dispatcher.register_listeners(
441+
{
442+
ActiveDatabaseChanged: [listener],
443+
}
444+
)
445+
446+
config = MultiDbConfig(
447+
client_class=client_class,
448+
databases_config=db_configs,
449+
command_retry=command_retry,
450+
min_num_failures=min_num_failures,
451+
health_check_probes=3,
452+
health_check_interval=health_check_interval,
453+
event_dispatcher=event_dispatcher,
454+
health_check_probes_delay=health_check_delay,
455+
)
456+
457+
client = MultiDBClient(config)
458+
459+
415460
Managing databases at runtime
416461
-----------------------------
417462

0 commit comments

Comments
 (0)