Skip to content

Commit 37a6314

Browse files
committed
watched_tasks: let spawn_task return Result instead of panicking
Signed-off-by: Leonard Göhrs <l.goehrs@pengutronix.de>
1 parent 607a22c commit 37a6314

32 files changed

+230
-187
lines changed

src/adc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl Adc {
227227

228228
time.set(Timestamp::now());
229229
}
230-
});
230+
})?;
231231

232232
Ok(adc)
233233
}

src/backlight.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Backlight {
6262
}
6363

6464
Ok(())
65-
});
65+
})?;
6666

6767
Ok(Self { brightness })
6868
}

src/broker.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// with this program; if not, write to the Free Software Foundation, Inc.,
1616
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717

18+
use anyhow::Result;
1819
use async_std::sync::Arc;
1920
use serde::{de::DeserializeOwned, Serialize};
2021

@@ -115,11 +116,13 @@ impl BrokerBuilder {
115116
/// Finish building the broker
116117
///
117118
/// This consumes the builder so that no new topics can be registered
118-
pub fn build(self, wtb: &mut WatchedTasksBuilder, server: &mut tide::Server<()>) {
119+
pub fn build(self, wtb: &mut WatchedTasksBuilder, server: &mut tide::Server<()>) -> Result<()> {
119120
let topics = Arc::new(self.topics);
120121

121-
persistence::register(wtb, topics.clone());
122+
persistence::register(wtb, topics.clone())?;
122123
rest::register(server, topics.clone());
123124
mqtt_conn::register(server, topics);
125+
126+
Ok(())
124127
}
125128
}

src/broker/persistence.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ async fn save_on_change(
148148
Ok(())
149149
}
150150

151-
pub fn register(wtb: &mut WatchedTasksBuilder, topics: Arc<Vec<Arc<dyn AnyTopic>>>) {
151+
pub fn register(wtb: &mut WatchedTasksBuilder, topics: Arc<Vec<Arc<dyn AnyTopic>>>) -> Result<()> {
152152
load(&topics).unwrap();
153153

154154
let (tx, rx) = unbounded();
@@ -157,5 +157,5 @@ pub fn register(wtb: &mut WatchedTasksBuilder, topics: Arc<Vec<Arc<dyn AnyTopic>
157157
topic.subscribe_as_bytes(tx.clone(), false);
158158
}
159159

160-
wtb.spawn_task("persistence-save", save_on_change(topics, rx));
160+
wtb.spawn_task("persistence-save", save_on_change(topics, rx))
161161
}

src/dbus.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::watched_tasks::WatchedTasksBuilder;
2323

2424
#[cfg(feature = "demo_mode")]
2525
mod zb {
26-
pub type Result<T> = std::result::Result<T, ()>;
26+
pub use anyhow::Result;
2727

2828
pub struct Connection;
2929
pub struct ConnectionBuilder;
@@ -78,20 +78,17 @@ impl DbusSession {
7878
wtb: &mut WatchedTasksBuilder,
7979
led_dut: Arc<Topic<BlinkPattern>>,
8080
led_uplink: Arc<Topic<BlinkPattern>>,
81-
) -> Self {
81+
) -> anyhow::Result<Self> {
8282
let tacd = Tacd::new();
8383

84-
let conn_builder = ConnectionBuilder::system()
85-
.unwrap()
86-
.name("de.pengutronix.tacd")
87-
.unwrap();
84+
let conn_builder = ConnectionBuilder::system()?.name("de.pengutronix.tacd")?;
8885

89-
let conn = Arc::new(tacd.serve(conn_builder).build().await.unwrap());
86+
let conn = Arc::new(tacd.serve(conn_builder).build().await?);
9087

91-
Self {
92-
network: Network::new(bb, wtb, &conn, led_dut, led_uplink),
93-
rauc: Rauc::new(bb, wtb, &conn),
94-
systemd: Systemd::new(bb, wtb, &conn).await,
95-
}
88+
Ok(Self {
89+
network: Network::new(bb, wtb, &conn, led_dut, led_uplink)?,
90+
rauc: Rauc::new(bb, wtb, &conn)?,
91+
systemd: Systemd::new(bb, wtb, &conn).await?,
92+
})
9693
}
9794
}

src/dbus/networkmanager/mod.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// with this program; if not, write to the Free Software Foundation, Inc.,
1616
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717

18+
use anyhow::Result;
1819
use async_std;
1920
use async_std::sync::Arc;
2021

@@ -31,7 +32,7 @@ mod hostname;
3132
// Put them inside a mod so we do not have to decorate each one with
3233
// a #[cfg(not(feature = "demo_mode"))].
3334
mod optional_includes {
34-
pub use anyhow::{anyhow, Result};
35+
pub use anyhow::anyhow;
3536
pub use async_std::stream::StreamExt;
3637
pub use async_std::task::sleep;
3738
pub use futures::{future::FutureExt, pin_mut, select};
@@ -261,7 +262,7 @@ impl Network {
261262
_conn: C,
262263
_led_dut: Arc<Topic<BlinkPattern>>,
263264
_led_uplink: Arc<Topic<BlinkPattern>>,
264-
) -> Self {
265+
) -> Result<Self> {
265266
let this = Self::setup_topics(bb);
266267

267268
this.hostname.set("lxatac".to_string());
@@ -275,7 +276,7 @@ impl Network {
275276
carrier: true,
276277
});
277278

278-
this
279+
Ok(this)
279280
}
280281

281282
#[cfg(not(feature = "demo_mode"))]
@@ -285,7 +286,7 @@ impl Network {
285286
conn: &Arc<Connection>,
286287
led_dut: Arc<Topic<BlinkPattern>>,
287288
led_uplink: Arc<Topic<BlinkPattern>>,
288-
) -> Self {
289+
) -> Result<Self> {
289290
let this = Self::setup_topics(bb);
290291

291292
{
@@ -307,7 +308,7 @@ impl Network {
307308
}
308309

309310
Ok(())
310-
});
311+
})?;
311312
}
312313

313314
{
@@ -337,7 +338,7 @@ impl Network {
337338
}
338339

339340
Ok(())
340-
});
341+
})?;
341342
}
342343

343344
{
@@ -364,7 +365,7 @@ impl Network {
364365
}
365366

366367
Ok(())
367-
});
368+
})?;
368369
}
369370

370371
{
@@ -386,9 +387,9 @@ impl Network {
386387
}
387388

388389
Ok(())
389-
});
390+
})?;
390391
}
391392

392-
this
393+
Ok(this)
393394
}
394395
}

src/dbus/rauc/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl Rauc {
283283
bb: &mut BrokerBuilder,
284284
wtb: &mut WatchedTasksBuilder,
285285
_conn: &Arc<Connection>,
286-
) -> Self {
286+
) -> Result<Self> {
287287
let inst = Self::setup_topics(bb);
288288

289289
inst.operation.set("idle".to_string());
@@ -300,17 +300,17 @@ impl Rauc {
300300
inst.channels.clone(),
301301
inst.slot_status.clone(),
302302
),
303-
);
303+
)?;
304304

305-
inst
305+
Ok(inst)
306306
}
307307

308308
#[cfg(not(feature = "demo_mode"))]
309309
pub fn new(
310310
bb: &mut BrokerBuilder,
311311
wtb: &mut WatchedTasksBuilder,
312312
conn: &Arc<Connection>,
313-
) -> Self {
313+
) -> Result<Self> {
314314
let inst = Self::setup_topics(bb);
315315

316316
let conn_task = conn.clone();
@@ -409,7 +409,7 @@ impl Rauc {
409409
break Ok(());
410410
}
411411
}
412-
});
412+
})?;
413413

414414
let conn_task = conn.clone();
415415
let progress = inst.progress.clone();
@@ -431,7 +431,7 @@ impl Rauc {
431431
}
432432

433433
Ok(())
434-
});
434+
})?;
435435

436436
let conn_task = conn.clone();
437437
let last_error = inst.last_error.clone();
@@ -453,7 +453,7 @@ impl Rauc {
453453
}
454454

455455
Ok(())
456-
});
456+
})?;
457457

458458
let conn_task = conn.clone();
459459
let (mut install_stream, _) = inst.install.clone().subscribe_unbounded();
@@ -473,7 +473,7 @@ impl Rauc {
473473
}
474474

475475
Ok(())
476-
});
476+
})?;
477477

478478
// Reload the channel list on request
479479
let (reload_stream, _) = inst.reload.clone().subscribe_unbounded();
@@ -485,8 +485,8 @@ impl Rauc {
485485
inst.channels.clone(),
486486
inst.slot_status.clone(),
487487
),
488-
);
488+
)?;
489489

490-
inst
490+
Ok(inst)
491491
}
492492
}

src/dbus/systemd/mod.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ impl Service {
100100
_wtb: &mut WatchedTasksBuilder,
101101
_conn: Arc<Connection>,
102102
_unit_name: &str,
103-
) {
103+
) -> anyhow::Result<()> {
104104
self.status.set(ServiceStatus::get().await.unwrap());
105+
106+
Ok(())
105107
}
106108

107109
#[cfg(not(feature = "demo_mode"))]
@@ -110,7 +112,7 @@ impl Service {
110112
wtb: &mut WatchedTasksBuilder,
111113
conn: Arc<Connection>,
112114
unit_name: &'static str,
113-
) {
115+
) -> anyhow::Result<()> {
114116
let unit_path = {
115117
let manager = manager::ManagerProxy::new(&conn).await.unwrap();
116118
manager.get_unit(unit_name).await.unwrap()
@@ -150,7 +152,7 @@ impl Service {
150152
.await
151153
.unwrap();
152154
}
153-
});
155+
})?;
154156

155157
let (mut action_reqs, _) = self.action.clone().subscribe_unbounded();
156158

@@ -171,7 +173,9 @@ impl Service {
171173
}
172174

173175
Ok(())
174-
});
176+
})?;
177+
178+
Ok(())
175179
}
176180
}
177181

@@ -181,7 +185,7 @@ impl Systemd {
181185
wtb: &mut WatchedTasksBuilder,
182186
reboot: Arc<Topic<bool>>,
183187
_conn: Arc<Connection>,
184-
) {
188+
) -> anyhow::Result<()> {
185189
let (mut reboot_reqs, _) = reboot.subscribe_unbounded();
186190

187191
wtb.spawn_task("systemd-reboot", async move {
@@ -192,15 +196,15 @@ impl Systemd {
192196
}
193197

194198
Ok(())
195-
});
199+
})
196200
}
197201

198202
#[cfg(not(feature = "demo_mode"))]
199203
pub fn handle_reboot(
200204
wtb: &mut WatchedTasksBuilder,
201205
reboot: Arc<Topic<bool>>,
202206
conn: Arc<Connection>,
203-
) {
207+
) -> anyhow::Result<()> {
204208
let (mut reboot_reqs, _) = reboot.subscribe_unbounded();
205209

206210
wtb.spawn_task("systemd-reboot", async move {
@@ -215,35 +219,37 @@ impl Systemd {
215219
}
216220

217221
Ok(())
218-
});
222+
})
219223
}
220224

221225
pub async fn new(
222226
bb: &mut BrokerBuilder,
223227
wtb: &mut WatchedTasksBuilder,
224228
conn: &Arc<Connection>,
225-
) -> Self {
229+
) -> anyhow::Result<Self> {
226230
let reboot = bb.topic_rw("/v1/tac/reboot", Some(false));
227231

228-
Self::handle_reboot(wtb, reboot.clone(), conn.clone());
232+
Self::handle_reboot(wtb, reboot.clone(), conn.clone())?;
229233

230234
let networkmanager = Service::new(bb, "network-manager");
231235
let labgrid = Service::new(bb, "labgrid-exporter");
232236
let iobus = Service::new(bb, "lxa-iobus");
233237

234238
networkmanager
235239
.connect(wtb, conn.clone(), "NetworkManager.service")
236-
.await;
240+
.await?;
237241
labgrid
238242
.connect(wtb, conn.clone(), "labgrid-exporter.service")
239-
.await;
240-
iobus.connect(wtb, conn.clone(), "lxa-iobus.service").await;
243+
.await?;
244+
iobus
245+
.connect(wtb, conn.clone(), "lxa-iobus.service")
246+
.await?;
241247

242-
Self {
248+
Ok(Self {
243249
reboot,
244250
networkmanager,
245251
labgrid,
246252
iobus,
247-
}
253+
})
248254
}
249255
}

0 commit comments

Comments
 (0)