pub struct Events { /* private fields */ }Expand description
The event notifier of one request, job batch or cron run: Ctx::events.
Cheap to clone; clones share their context and pending events.
Events::default() builds one for unit tests, whose events are read back
with take.
§Examples
let events = ocre::events::Events::default();
events.notify("cache.miss", "posts/12");
assert_eq!(events.take()[0].payload["value"], "posts/12");Implementations§
Source§impl Events
impl Events
Sourcepub fn notify(&self, name: &str, payload: impl Serialize)
pub fn notify(&self, name: &str, payload: impl Serialize)
Records an event (Rails’ Rails.event.notify): logs it and queues it for the subscribers.
§Examples
let events = ocre::events::Events::default();
events.notify("post.published", serde_json::json!({ "post_id": 3 }));
assert_eq!(events.take()[0].payload["post_id"], 3);Sourcepub fn tagged(&self, key: &str, value: impl Serialize) -> Self
pub fn tagged(&self, key: &str, value: impl Serialize) -> Self
This notifier with a tag added to its events (Rails’ Rails.event.tagged).
§Examples
let events = ocre::events::Events::default();
events.tagged("importer", "csv").notify("row.skipped", serde_json::json!({ "line": 12 }));
assert_eq!(events.take()[0].tags["importer"], "csv");Sourcepub fn set_context(&self, key: &str, value: impl Serialize)
pub fn set_context(&self, key: &str, value: impl Serialize)
Adds context to every later event of this request or job (Rails’ Rails.event.set_context).
§Examples
let events = ocre::events::Events::default();
events.set_context("user_id", 7);
events.notify("search", serde_json::json!({ "query": "rust" }));
assert_eq!(events.take()[0].context["user_id"], 7);Trait Implementations§
Auto Trait Implementations§
impl Freeze for Events
impl RefUnwindSafe for Events
impl Send for Events
impl Sync for Events
impl Unpin for Events
impl UnsafeUnpin for Events
impl UnwindSafe for Events
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more