Pruner

class chancy.plugins.pruner.Pruner(rule: ~chancy.rule.SQLAble = <chancy.rule.Condition object>, *, maximum_to_prune: int = 10000, poll_interval: int = 60)[source]

Bases: Plugin

A plugin that prunes stale data from the database.

from chancy.plugins.leadership import Leadership
from chancy.plugins.pruner import Pruner

async with Chancy(..., plugins=[
    Leadership(),
    Pruner(
        Pruner.Rules.Queue() == "default" & (Pruner.Rules.Age() > 60)
    )
]) as chancy:
    ...

The pruner will never prune jobs that haven’t been run yet or are currently running. When the pruner runs, it will also call the chancy.plugin.Plugin.cleanup() method on any plugins that implement it, allowing them to clean up any data that is no longer needed such as completed workflows.

Rules

You can use simple rules, or combine them using the | and & operators to create complex rules.

For example, to prune jobs that are older than 60 seconds:

Pruner(Pruner.Rules.Age() > 60)

Or to prune jobs that are older than 60 seconds and are in the “default” queue:

Pruner(Pruner.Rules.Queue() == "default" & (Pruner.Rules.Age() > 60))

Or to prune jobs that are older than 60 seconds and are in the “default” queue, or instantly deleted if the job is update_cache:

Pruner(
    (Pruner.Rules.Queue() == "default" & (Pruner.Rules.Age() > 60)) |
    Pruner.Rules.Job() == "update_cache"
)

By default, the pruner will run every 60 seconds and will remove up to 10,000 jobs in a single run that have been completed for more than 60 seconds.

Tip

By default, only an Age rule will be covered by an index. If you use multiple rules, you may need to create additional indexes to improve performance on busy queues.

param rule:

The rule that the pruner will use to match jobs.

param maximum_to_prune:

The maximum number of jobs to prune in a single run of the pruner.

param poll_interval:

The interval in seconds between each run of the pruner.

Rules[source]

alias of JobRules

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 prune(chancy: Chancy, cursor: AsyncCursor) int[source]

Prune stale records from the database.

Parameters:
  • chancy – The Chancy application.

  • cursor – The database cursor to use for the operation.

Returns:

The number of rows removed from the database

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.