Skip to main content

Context

Struct Context 

Source
pub struct Context { /* private fields */ }
Expand description

A daemon plugin’s handle to the host, its configuration and its executor. Cheap to clone.

Implementations§

Source§

impl Context

Source

pub fn config<T: DeserializeOwned>(&self) -> Result<T>

The configuration, deserialized (an empty document is null).

Source

pub fn config_json(&self) -> Json

Source

pub fn values(&self) -> Values

Examples found in repository?
examples/together_companion.rs (line 36)
34fn publish(cx: &Context, state: &str, builds: u32) {
35    let _ = cx
36        .values()
37        .set("together.status", &Status { state, builds });
38    let _ = cx.events().emit("together.changed", &(), Target::Viewers);
39}
Source

pub fn live(&self) -> Live

Examples found in repository?
examples/heartbeat_daemon.rs (line 48)
33    async fn run(&self, cx: Context) {
34        let _ = cx.events().on("heartbeat.ping");
35        // Answer pings beside the heartbeat loop.
36        let events = cx.clone();
37        cx.spawn(async move {
38            loop {
39                if let Event::Plugin { name, .. } = events.next_event().await
40                    && name == "heartbeat.ping"
41                {
42                    let _ = events.events().emit("heartbeat.pong", &(), Target::All);
43                }
44            }
45        });
46        loop {
47            self.beats.set(self.beats.get() + 1);
48            let _ = cx.live().publish("heartbeat.beat", &self.beats.get());
49            cx.sleep(30_000).await;
50        }
51    }
Source

pub fn events(&self) -> Events

Examples found in repository?
examples/together_companion.rs (line 38)
34fn publish(cx: &Context, state: &str, builds: u32) {
35    let _ = cx
36        .values()
37        .set("together.status", &Status { state, builds });
38    let _ = cx.events().emit("together.changed", &(), Target::Viewers);
39}
More examples
Hide additional examples
examples/heartbeat_daemon.rs (line 34)
33    async fn run(&self, cx: Context) {
34        let _ = cx.events().on("heartbeat.ping");
35        // Answer pings beside the heartbeat loop.
36        let events = cx.clone();
37        cx.spawn(async move {
38            loop {
39                if let Event::Plugin { name, .. } = events.next_event().await
40                    && name == "heartbeat.ping"
41                {
42                    let _ = events.events().emit("heartbeat.pong", &(), Target::All);
43                }
44            }
45        });
46        loop {
47            self.beats.set(self.beats.get() + 1);
48            let _ = cx.live().publish("heartbeat.beat", &self.beats.get());
49            cx.sleep(30_000).await;
50        }
51    }
Source

pub fn claims(&self) -> Claims

Exactly-once effects across the daemons that run a fleet plugin (a singleton’s claims carry its lease epoch). No grant.

Source

pub fn calls(&self) -> Calls

Source

pub fn now_ms(&self) -> u64

The daemon’s monotonic clock as of the current drive call.

Source

pub fn next_event(&self) -> NextEvent ⓘ

The next event the host delivers (values, live messages, plugin events, account changes). Events queue while nothing awaits them, up to EVENT_QUEUE; past that the oldest are dropped.

Examples found in repository?
examples/heartbeat_daemon.rs (line 39)
33    async fn run(&self, cx: Context) {
34        let _ = cx.events().on("heartbeat.ping");
35        // Answer pings beside the heartbeat loop.
36        let events = cx.clone();
37        cx.spawn(async move {
38            loop {
39                if let Event::Plugin { name, .. } = events.next_event().await
40                    && name == "heartbeat.ping"
41                {
42                    let _ = events.events().emit("heartbeat.pong", &(), Target::All);
43                }
44            }
45        });
46        loop {
47            self.beats.set(self.beats.get() + 1);
48            let _ = cx.live().publish("heartbeat.beat", &self.beats.get());
49            cx.sleep(30_000).await;
50        }
51    }
Source

pub fn interest(&self) -> Interest

Which of the plugin’s UI surfaces some viewer shows now (Interest). Changes arrive as Event::Interest.

Source

pub fn next_event_until(&self, at_ms: Option<u64>) -> NextEventUntil ⓘ

The next event, or None once the daemon’s clock reaches at_ms (never, with None): the one wait a loop that reads on a schedule and reacts to events needs. The timer goes with it, so an event that arrives first leaves no wake behind.

Source

pub fn sleep(&self, ms: u64) -> Sleep ⓘ

Completes ms milliseconds from now.

Examples found in repository?
examples/heartbeat_daemon.rs (line 49)
33    async fn run(&self, cx: Context) {
34        let _ = cx.events().on("heartbeat.ping");
35        // Answer pings beside the heartbeat loop.
36        let events = cx.clone();
37        cx.spawn(async move {
38            loop {
39                if let Event::Plugin { name, .. } = events.next_event().await
40                    && name == "heartbeat.ping"
41                {
42                    let _ = events.events().emit("heartbeat.pong", &(), Target::All);
43                }
44            }
45        });
46        loop {
47            self.beats.set(self.beats.get() + 1);
48            let _ = cx.live().publish("heartbeat.beat", &self.beats.get());
49            cx.sleep(30_000).await;
50        }
51    }
Source

pub fn sleep_until(&self, at_ms: u64) -> Sleep ⓘ

Completes at at_ms on the daemon’s clock.

Source

pub fn spawn(&self, task: impl Future<Output = ()> + 'static)

Runs task beside the run loop until it completes or the plugin is deactivated.

Examples found in repository?
examples/together_companion.rs (lines 78-82)
73    fn call(&self, method: &str, _request: Json, cx: &Context) -> Result<Json, Error> {
74        match method {
75            "rebuild" => {
76                let started = !self.running.replace(true);
77                if started {
78                    cx.spawn(rebuild(
79                        cx.clone(),
80                        self.builds.clone(),
81                        self.running.clone(),
82                    ));
83                }
84                Json::from_value(&started)
85            }
86            other => Err(Error::Invalid(format!("unknown method {other}"))),
87        }
88    }
More examples
Hide additional examples
examples/heartbeat_daemon.rs (lines 37-45)
33    async fn run(&self, cx: Context) {
34        let _ = cx.events().on("heartbeat.ping");
35        // Answer pings beside the heartbeat loop.
36        let events = cx.clone();
37        cx.spawn(async move {
38            loop {
39                if let Event::Plugin { name, .. } = events.next_event().await
40                    && name == "heartbeat.ping"
41                {
42                    let _ = events.events().emit("heartbeat.pong", &(), Target::All);
43                }
44            }
45        });
46        loop {
47            self.beats.set(self.beats.get() + 1);
48            let _ = cx.live().publish("heartbeat.beat", &self.beats.get());
49            cx.sleep(30_000).await;
50        }
51    }
Source

pub fn caller(&self) -> Option<Caller>

Who made the call being answered, inside DaemonPlugin::call; None elsewhere.

Source

pub fn machine_id(&self) -> String

The machine this daemon runs on, by the account’s machine id: the id a UI plugin targets with Target::Machine and finds in account::state.

Source

pub fn account_id(&self) -> Option<String>

The account this daemon is enrolled in, once the daemon has read it.

Source

pub fn lease_epoch(&self) -> Option<u64>

The lease epoch a singleton runs under (every write of its account session carries it); None for a fleet plugin. A plugin that records who did something (a claim, a value) can tag it with its epoch.

Source

pub fn release(&self) -> Option<Release>

The Standard Code build this daemon runs: its channel (branch:<name>, team, …), version and commit. None where it is not known.

Trait Implementations§

Source§

impl Clone for Context

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Context

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Resource for T
where T: 'static,

Source§

type Rep = Option<T>

The type which is actually stored in-memory for this resource. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.