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
impl Context
Sourcepub fn config<T: DeserializeOwned>(&self) -> Result<T>
pub fn config<T: DeserializeOwned>(&self) -> Result<T>
The configuration, deserialized (an empty document is null).
pub fn config_json(&self) -> Json
Sourcepub fn live(&self) -> Live
pub fn live(&self) -> Live
Examples found in repository?
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 }Sourcepub fn events(&self) -> Events
pub fn events(&self) -> Events
Examples found in repository?
More examples
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 }Sourcepub fn claims(&self) -> Claims
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.
pub fn calls(&self) -> Calls
Sourcepub fn next_event(&self) -> NextEvent ⓘ
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?
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 }Sourcepub fn interest(&self) -> Interest
pub fn interest(&self) -> Interest
Which of the plugin’s UI surfaces some viewer shows now
(Interest). Changes arrive as
Event::Interest.
Sourcepub fn next_event_until(&self, at_ms: Option<u64>) -> NextEventUntil ⓘ
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.
Sourcepub fn sleep(&self, ms: u64) -> Sleep ⓘ
pub fn sleep(&self, ms: u64) -> Sleep ⓘ
Completes ms milliseconds from now.
Examples found in repository?
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 }Sourcepub fn sleep_until(&self, at_ms: u64) -> Sleep ⓘ
pub fn sleep_until(&self, at_ms: u64) -> Sleep ⓘ
Completes at at_ms on the daemon’s clock.
Sourcepub fn spawn(&self, task: impl Future<Output = ()> + 'static)
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?
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
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 }Sourcepub fn caller(&self) -> Option<Caller>
pub fn caller(&self) -> Option<Caller>
Who made the call being answered, inside DaemonPlugin::call;
None elsewhere.
Sourcepub fn machine_id(&self) -> String
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.
Sourcepub fn account_id(&self) -> Option<String>
pub fn account_id(&self) -> Option<String>
The account this daemon is enrolled in, once the daemon has read it.
Sourcepub fn lease_epoch(&self) -> Option<u64>
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.