-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpublisher2.rs
More file actions
48 lines (40 loc) · 1.36 KB
/
publisher2.rs
File metadata and controls
48 lines (40 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! Sample Caraytid module - publisher side
use anyhow::Result;
use caryatid_sdk::{module, Context};
use config::Config;
use serde_json::json;
use std::sync::Arc;
use tracing::info;
/// Standard message type
type MType = serde_json::Value;
/// Sample publisher module
// Define it as a module, with a name and description
#[module(
message_type(MType),
name = "publisher2",
description = "Sample publisher module (#2)"
)]
pub struct Publisher;
impl Publisher {
// Implement the single initialisation function, with application
// Context and this module's Config
async fn init(&self, context: Arc<Context<MType>>, config: Arc<Config>) -> Result<()> {
let message_bus = context.message_bus.clone();
// Get configuration
let topic = config.get_string("topic").unwrap_or("test".to_string());
info!("Creating publisher on '{}'", topic);
// Send a test JSON message to the message bus on 'sample_topic'
// Let this run async
context.run(async move {
let test_message = Arc::new(json!({
"message": "Hello, world! from publisher #2",
}));
info!("Sending {:?}", test_message);
message_bus
.publish(&topic, test_message)
.await
.expect("Failed to publish message");
});
Ok(())
}
}