mirror of
https://github.com/romanz/electrs.git
synced 2026-08-20 13:27:31 +02:00
Refactor thread spawning into a separate module
This commit is contained in:
parent
7409142edf
commit
64230332cc
3 changed files with 21 additions and 3 deletions
|
|
@ -23,6 +23,7 @@ mod p2p;
|
|||
pub mod server;
|
||||
mod signals;
|
||||
mod status;
|
||||
mod thread;
|
||||
mod tracker;
|
||||
mod types;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
use anyhow::Context;
|
||||
use crossbeam_channel::{unbounded, Receiver};
|
||||
use signal_hook::consts::signal::*;
|
||||
use signal_hook::iterator::Signals;
|
||||
|
||||
use std::thread;
|
||||
use crate::thread::spawn;
|
||||
|
||||
pub(crate) enum Signal {
|
||||
Exit,
|
||||
|
|
@ -16,15 +17,16 @@ pub(crate) fn register() -> Receiver<Signal> {
|
|||
];
|
||||
let (tx, rx) = unbounded();
|
||||
let mut signals = Signals::new(&ids).expect("failed to register signal hook");
|
||||
thread::spawn(move || {
|
||||
spawn("signal", move || {
|
||||
for id in &mut signals {
|
||||
info!("notified via SIG{}", id);
|
||||
let signal = match id {
|
||||
SIGUSR1 => Signal::Trigger,
|
||||
_ => Signal::Exit,
|
||||
};
|
||||
tx.send(signal).expect("failed to send signal");
|
||||
tx.send(signal).context("failed to send signal")?;
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
rx
|
||||
}
|
||||
|
|
|
|||
15
src/thread.rs
Normal file
15
src/thread.rs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
use anyhow::Result;
|
||||
|
||||
pub(crate) fn spawn<F>(name: &'static str, f: F) -> std::thread::JoinHandle<()>
|
||||
where
|
||||
F: 'static + Send + FnOnce() -> Result<()>,
|
||||
{
|
||||
std::thread::Builder::new()
|
||||
.name(name.to_owned())
|
||||
.spawn(move || {
|
||||
if let Err(e) = f() {
|
||||
warn!("{} thread failed: {}", name, e);
|
||||
}
|
||||
})
|
||||
.expect("failed to spawn a thread")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue