mirror of
https://github.com/mikedilger/gossip.git
synced 2024-09-19 19:46:50 +00:00
Setup a Syncer which will do background database syncrhonzation (when messaged to do so)
This commit is contained in:
parent
9994072485
commit
59abfdd15b
@ -28,7 +28,15 @@ pub struct Globals {
|
|||||||
|
|
||||||
/// This is ephemeral. It is filled during lazy_static initialization,
|
/// This is ephemeral. It is filled during lazy_static initialization,
|
||||||
/// and stolen away when the Overlord is created.
|
/// and stolen away when the Overlord is created.
|
||||||
pub from_minions: Mutex<Option<mpsc::UnboundedReceiver<BusMessage>>>,
|
pub tmp_overlord_receiver: Mutex<Option<mpsc::UnboundedReceiver<BusMessage>>>,
|
||||||
|
|
||||||
|
/// This is an mpsc channel. The Syncer listens on it.
|
||||||
|
/// To create a sender, just clone() it.
|
||||||
|
pub to_syncer: mpsc::UnboundedSender<String>,
|
||||||
|
|
||||||
|
/// This is ephemeral. It is filled during lazy_static initializtion,
|
||||||
|
/// and stolen away when the Syncer is created.
|
||||||
|
pub tmp_syncer_receiver: Mutex<Option<mpsc::UnboundedReceiver<String>>>,
|
||||||
|
|
||||||
/// All nostr events, keyed by the event Id
|
/// All nostr events, keyed by the event Id
|
||||||
pub events: RwLock<HashMap<Id, Event>>,
|
pub events: RwLock<HashMap<Id, Event>>,
|
||||||
@ -80,13 +88,18 @@ lazy_static! {
|
|||||||
let (to_minions, _) = broadcast::channel(256);
|
let (to_minions, _) = broadcast::channel(256);
|
||||||
|
|
||||||
// Setup a communications channel from the Minions to the Overlord.
|
// Setup a communications channel from the Minions to the Overlord.
|
||||||
let (to_overlord, from_minions) = mpsc::unbounded_channel();
|
let (to_overlord, tmp_overlord_receiver) = mpsc::unbounded_channel();
|
||||||
|
|
||||||
|
// Setup a communications channel from the UI (or anybody else) to the Syncer.
|
||||||
|
let (to_syncer, tmp_syncer_receiver) = mpsc::unbounded_channel();
|
||||||
|
|
||||||
Globals {
|
Globals {
|
||||||
db: Mutex::new(None),
|
db: Mutex::new(None),
|
||||||
to_minions,
|
to_minions,
|
||||||
to_overlord,
|
to_overlord,
|
||||||
from_minions: Mutex::new(Some(from_minions)),
|
tmp_overlord_receiver: Mutex::new(Some(tmp_overlord_receiver)),
|
||||||
|
to_syncer,
|
||||||
|
tmp_syncer_receiver: Mutex::new(Some(tmp_syncer_receiver)),
|
||||||
events: RwLock::new(HashMap::new()),
|
events: RwLock::new(HashMap::new()),
|
||||||
incoming_events: RwLock::new(Vec::new()),
|
incoming_events: RwLock::new(Vec::new()),
|
||||||
relationships: RwLock::new(HashMap::new()),
|
relationships: RwLock::new(HashMap::new()),
|
||||||
|
23
src/main.rs
23
src/main.rs
@ -15,6 +15,7 @@ mod process;
|
|||||||
mod relationship;
|
mod relationship;
|
||||||
mod settings;
|
mod settings;
|
||||||
mod signer;
|
mod signer;
|
||||||
|
mod syncer;
|
||||||
mod ui;
|
mod ui;
|
||||||
|
|
||||||
use crate::comms::BusMessage;
|
use crate::comms::BusMessage;
|
||||||
@ -67,15 +68,29 @@ fn main() -> Result<(), Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn tokio_main() {
|
async fn tokio_main() {
|
||||||
// Steal `from_minions` from the GLOBALS, and give it to a new Overlord
|
// note: we don't get a spawn handle here, we don't signal this thread when we are exiting,
|
||||||
let from_minions = {
|
// we just let it die when tokio_main() exits. I think that is ok.
|
||||||
let mut mutex_option = GLOBALS.from_minions.lock().await;
|
tokio::task::spawn(async move {
|
||||||
|
// Steal `tmp_syncer_receiver` from the GLOBALS, and give it to a new Syncer
|
||||||
|
let syncer_receiver = {
|
||||||
|
let mut mutex_option = GLOBALS.tmp_syncer_receiver.lock().await;
|
||||||
|
mem::replace(mutex_option.deref_mut(), None)
|
||||||
|
}
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut syncer = crate::syncer::Syncer::new(syncer_receiver);
|
||||||
|
syncer.run().await;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Steal `tmp_overlord_receiver` from the GLOBALS, and give it to a new Overlord
|
||||||
|
let overlord_receiver = {
|
||||||
|
let mut mutex_option = GLOBALS.tmp_overlord_receiver.lock().await;
|
||||||
mem::replace(mutex_option.deref_mut(), None)
|
mem::replace(mutex_option.deref_mut(), None)
|
||||||
}
|
}
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Run the overlord
|
// Run the overlord
|
||||||
let mut overlord = crate::overlord::Overlord::new(from_minions);
|
let mut overlord = crate::overlord::Overlord::new(overlord_receiver);
|
||||||
overlord.run().await;
|
overlord.run().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,6 +114,9 @@ impl Overlord {
|
|||||||
.insert(Url::new(&relay.url), relay.clone());
|
.insert(Url::new(&relay.url), relay.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test the syncer
|
||||||
|
let _ = GLOBALS.to_syncer.send("test".to_owned());
|
||||||
|
|
||||||
// Load people from the database
|
// Load people from the database
|
||||||
{
|
{
|
||||||
let mut dbpeople = DbPerson::fetch(None).await?;
|
let mut dbpeople = DbPerson::fetch(None).await?;
|
||||||
|
32
src/syncer.rs
Normal file
32
src/syncer.rs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
pub struct Syncer {
|
||||||
|
incoming: mpsc::UnboundedReceiver<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Syncer {
|
||||||
|
pub fn new(incoming: mpsc::UnboundedReceiver<String>) -> Syncer {
|
||||||
|
Syncer { incoming }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run(&mut self) {
|
||||||
|
loop {
|
||||||
|
let message = self.incoming.recv().await;
|
||||||
|
|
||||||
|
if message.is_none() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let message = message.unwrap();
|
||||||
|
|
||||||
|
match &*message {
|
||||||
|
"test" => {
|
||||||
|
tracing::debug!("Syncer received test message.");
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
tracing::debug!("Syncer received unknown message: {}", message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user