Skip to main content
Cloacina Documentation
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Trigger

cloacina::python::trigger Rust

Python trigger bindings via PyO3.

Provides:

  • @cloaca.trigger decorator for defining custom Python triggers
  • TriggerResult Python class for returning poll results
  • PythonTriggerWrapper implementing the Rust Trigger trait

Structs

cloacina::python::trigger::PythonTriggerDef

pub

A collected Python trigger definition.

Fields

Name Type Description
name String
poll_interval Duration
allow_concurrent bool
python_function PyObject

cloacina::python::trigger::TriggerResult

Binding

Python API: cloaca.python.trigger.TriggerResult

Python-side trigger result returned from poll functions.

Usage from Python:

from cloaca import TriggerResult
@cloaca.trigger(name="my_trigger", poll_interval="10s")
def my_trigger():
if some_condition():
return TriggerResult(should_fire=True, context={"key": "value"})
return TriggerResult(should_fire=False)

Fields

Name Type Description
should_fire bool
context Option < PyObject >

Methods

new
fn new (should_fire : bool , context : Option < PyObject >) -> Self

Python API: cloaca.python.trigger.TriggerResult.new

Source
    fn new(should_fire: bool, context: Option<PyObject>) -> Self {
        Self {
            should_fire,
            context,
        }
    }
__repr__
fn __repr__ (& self) -> String

Python API: cloaca.python.trigger.TriggerResult.repr

Source
    fn __repr__(&self) -> String {
        if self.should_fire {
            "TriggerResult(should_fire=True)".to_string()
        } else {
            "TriggerResult(should_fire=False)".to_string()
        }
    }

cloacina::python::trigger::TriggerDecorator

Binding

Python API: cloaca.python.trigger.TriggerDecorator

Decorator for defining Python triggers.

@cloaca.trigger(name="check_inbox", poll_interval="30s")
def check_inbox():

#### Fields

| Name | Type | Description |
|------|------|-------------|
| `name` | `Option < String >` |  |
| `poll_interval` | `Duration` |  |
| `allow_concurrent` | `bool` |  |

#### Methods

##### `__call__`

```rust
fn __call__ (& self , py : Python , func : PyObject) -> PyResult < PyObject >

Python API: cloaca.python.trigger.TriggerDecorator.call

Source
    pub fn __call__(&self, py: Python, func: PyObject) -> PyResult<PyObject> {
        let trigger_name = if let Some(name) = &self.name {
            name.clone()
        } else {
            func.getattr(py, "__name__")?.extract::<String>(py)?
        };

        let def = PythonTriggerDef {
            name: trigger_name.clone(),
            poll_interval: self.poll_interval,
            allow_concurrent: self.allow_concurrent,
            python_function: func.clone_ref(py),
        };

        PYTHON_TRIGGER_REGISTRY.lock().push(def);

        tracing::debug!("Registered Python trigger: {}", trigger_name);

        // Return the original function (decorator is transparent)
        Ok(func)
    }

cloacina::python::trigger::PythonTriggerWrapper

pub

Rust wrapper that implements the Trigger trait by calling a Python function.

Fields

Name Type Description
name String
poll_interval Duration
allow_concurrent bool
python_function PyObject

Methods

new pub
fn new (def : & PythonTriggerDef) -> Self
Source
    pub fn new(def: &PythonTriggerDef) -> Self {
        let function = Python::with_gil(|py| def.python_function.clone_ref(py));
        Self {
            name: def.name.clone(),
            poll_interval: def.poll_interval,
            allow_concurrent: def.allow_concurrent,
            python_function: function,
        }
    }

Functions

cloacina::python::trigger::drain_python_triggers

pub

fn drain_python_triggers () -> Vec < PythonTriggerDef >

Collect all registered Python triggers and clear the registry.

Source
pub fn drain_python_triggers() -> Vec<PythonTriggerDef> {
    let mut registry = PYTHON_TRIGGER_REGISTRY.lock();
    std::mem::take(&mut *registry)
}

cloacina::python::trigger::trigger

Binding

Python API: cloaca.python.trigger.trigger

fn trigger (name : Option < String > , poll_interval : String , allow_concurrent : bool ,) -> PyResult < TriggerDecorator >

@cloaca.trigger(...) decorator factory.

Source
pub fn trigger(
    name: Option<String>,
    poll_interval: String,
    allow_concurrent: bool,
) -> PyResult<TriggerDecorator> {
    let interval = parse_duration_str(&poll_interval).map_err(|e| {
        PyValueError::new_err(format!("Invalid poll_interval '{}': {}", poll_interval, e))
    })?;

    Ok(TriggerDecorator {
        name,
        poll_interval: interval,
        allow_concurrent,
    })
}