Trigger
Python trigger bindings via PyO3.
Provides:
@cloaca.triggerdecorator for defining custom Python triggersTriggerResultPython class for returning poll resultsPythonTriggerWrapperimplementing the RustTriggertrait
pub
A collected Python trigger definition.
| Name | Type | Description |
|---|---|---|
name |
String |
|
poll_interval |
Duration |
|
allow_concurrent |
bool |
|
python_function |
PyObject |
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)
| Name | Type | Description |
|---|---|---|
should_fire |
bool |
|
context |
Option < PyObject > |
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,
}
}
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()
}
}
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)
}
pub
Rust wrapper that implements the Trigger trait by calling a Python function.
| Name | Type | Description |
|---|---|---|
name |
String |
|
poll_interval |
Duration |
|
allow_concurrent |
bool |
|
python_function |
PyObject |
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,
}
}
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)
}
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,
})
}