Leadership

class chancy.plugins.leadership.ImmediateLeadership[source]

Bases: Plugin

A plugin that simply sets the worker as the leader immediately upon startup.

This plugin is only ever intended for testing purposes, and should not be used in a production environment.

static get_identifier() str[source]

Returns a unique identifier for this plugin.

This identifier should be unique across all active plugins. If a custom plugin provides compatible functionality to a built-in plugin, it may use the same identifier as the built-in plugin.

classmethod get_scope() PluginScope[source]

Get the scope of this plugin. Scopes control when and where the plugin will be run.

By default, plugins are scoped to the worker.

async run(worker: Worker, chancy: Chancy)[source]

Runs the plugin.

This function can and should run indefinitely, as it will be cancelled when the worker is stopped.

class chancy.plugins.leadership.Leadership(poll_interval: int = 60, timeout: int = 120)[source]

Bases: Plugin

Leadership is used to ensure some plugins are run by only one worker at a time. For example, we wouldn’t want every worker running database pruning every 60 seconds, as that would be immensely wasteful. Most other plugins require a Leadership plugin to be enabled.

Note

This plugin is enabled by default, you only need to provide it in the list of plugins to customize its arguments or if no_default_plugins is set to True.

from chancy.plugins.leadership import Leadership

async with Chancy(..., plugins=[
    Leadership()
]) as chancy:
    ...

Implementation

The plugin implements a simple database-backed leader election system through a ‘leader’ table with the following characteristics:

  • Uses a single row with ID=1 to track the current leader worker.

  • Workers attempt to insert/update this row with their worker_id and an expiration timestamp.

  • Leadership is acquired when a worker successfully inserts the row (when no leader exists) or when the previous leader’s entry has expired.

  • The leader renews its leadership by updating its expiration timestamp during each poll cycle.

  • If leadership renewal fails, the worker loses its leader status.

  • When a worker gains or loses leadership, appropriate notifications are sent via leadership.gained, leadership.lost, and leadership.renewed events.

  • Worker leadership status is tracked via a worker.is_leader event flag.

param poll_interval:

The number of seconds between leadership poll intervals.

param timeout:

The number of seconds before a worker is considered to have lost leadership.

static get_identifier() str[source]

Returns a unique identifier for this plugin.

This identifier should be unique across all active plugins. If a custom plugin provides compatible functionality to a built-in plugin, it may use the same identifier as the built-in plugin.

classmethod get_scope() PluginScope[source]

Get the scope of this plugin. Scopes control when and where the plugin will be run.

By default, plugins are scoped to the worker.

get_tables() list[str][source]

Get the names of all tables this plugin is responsible for.

async run(worker: Worker, chancy: Chancy)[source]

Runs the plugin.

This function can and should run indefinitely, as it will be cancelled when the worker is stopped.