pub async fn exec_async_runner<AppEv: Send + Sync + 'static, AppMetric: Debug + Send + Sync + 'static>(
exec: impl AsyncExecutor<AppEv, AppMetric>,
app: impl App<AppEv, AppMetric> + 'static,
fs: impl WFS,
o11y: O11yProcessorOptions<AppMetric>,
) -> Result<(), MainEarlyReturn>Expand description
Run apps via an async based executor
Examples found in repository?
examples/basic.rs (line 122)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
async fn main() -> Result<(), MainEarlyReturn> {
let (tx, mut rx) = tokio::sync::mpsc::channel::<O11yEvent<()>>(10);
let _o11y_consumer_task = tokio::spawn(async move {
while let Some(res) = rx.recv().await {
match res.kind {
O11yEventKind::Status(cap, sz) => {
println!("{}: status cap:{} max:{}", res.timestamp, cap, sz);
}
O11yEventKind::App(_O11y) => {}
O11yEventKind::HostInfo(_hi) => {}
O11yEventKind::HostStats(_hs) => {}
O11yEventKind::Flush => {
println!("{}: flush", res.timestamp);
}
O11yEventKind::Finish => {
println!("{}: finish", res.timestamp);
}
O11yEventKind::Init(log_dir) => {
println!("{}: init log_dir:{:?}", res.timestamp, log_dir);
}
O11yEventKind::Log(level, target, name) => {
println!("{}: {} target:{} name:{}", res.timestamp, level, target, name);
}
O11yEventKind::Reconnect => {}
O11yEventKind::Clear => {}
O11yEventKind::Span(_, _) => {}
}
}
});
let wob = Observability {
tx: tx.clone(),
level: Level::INFO,
};
tracing_subscriber::registry().with(wob).init();
let app_name = "wora_basic";
let args = BasicAppOpts::parse();
let app = BasicApp { args: args, counter: 1 };
let fs = PhysicalVFS::new();
let interval = std::time::Duration::from_secs(5);
let o11y = O11yProcessorOptionsBuilder::default()
.sender(tx)
.flush_interval(interval.clone())
.status_interval(interval.clone())
.host_stats_interval(interval.clone())
.build()
.unwrap();
match UnixLikeUser::new(app_name, fs.clone()).await {
Ok(exec) => exec_async_runner(exec, app, fs.clone(), o11y).await?,
Err(exec_err) => {
error!("exec error:{}", exec_err);
return Err(MainEarlyReturn::Vfs(exec_err));
}
}
Ok(())
}More examples
examples/async_daemon.rs (line 229)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
async fn main() -> Result<(), MainEarlyReturn> {
let args = DaemonArgs::parse();
let app_state = DaemonState {};
let app = DaemonApp {
args: args.clone(),
state: Arc::new(RwLock::new(app_state)),
config: DaemonConfig::default(),
};
let (tx, mut rx) = tokio::sync::mpsc::channel::<O11yEvent<()>>(10);
let _o11y_consumer_task = tokio::spawn(async move {
while let Some(res) = rx.recv().await {
match res.kind {
O11yEventKind::Status(cap, sz) => {
println!("{}: status cap:{} max:{}", res.timestamp, cap, sz);
}
O11yEventKind::App(_O11y) => {}
O11yEventKind::HostInfo(_hi) => {}
O11yEventKind::HostStats(_hs) => {}
O11yEventKind::Flush => {
println!("{}: flush", res.timestamp);
}
O11yEventKind::Finish => {
println!("{}: finish", res.timestamp);
}
O11yEventKind::Init(log_dir) => {
println!("{}: init log_dir:{:?}", res.timestamp, log_dir);
}
O11yEventKind::Log(level, target, name) => {
println!("{}: {} target:{} name:{}", res.timestamp, level, target, name);
}
O11yEventKind::Reconnect => {}
O11yEventKind::Clear => {}
O11yEventKind::Span(_, _) => {}
}
}
});
let wob = Observability {
tx: tx.clone(),
level: Level::INFO,
};
tracing_subscriber::registry().with(wob).init();
let fs = PhysicalVFS::new();
let interval = std::time::Duration::from_secs(5);
let O11y = O11yProcessorOptionsBuilder::default()
.sender(tx)
.flush_interval(interval.clone())
.status_interval(interval.clone())
.host_stats_interval(interval.clone())
.build()
.unwrap();
match &args.run_mode {
RunMode::Sys => {
let exec = UnixLikeSystem::new(app.name()).await;
exec_async_runner(exec, app, fs, O11y).await?
}
RunMode::User => match UnixLikeUser::new(app.name(), fs.clone()).await {
Ok(exec) => exec_async_runner(exec, app, fs.clone(), O11y).await?,
Err(exec_err) => {
error!("exec error:{}", exec_err);
return Err(MainEarlyReturn::Vfs(exec_err));
}
},
}
Ok(())
}