Setup a Syncer which will do background database syncrhonzation (when messaged to do so)

This commit is contained in:
Mike Dilger 2022-12-31 07:07:35 +13:00
parent 9994072485
commit 59abfdd15b
4 changed files with 70 additions and 7 deletions

View File

@ -28,7 +28,15 @@ pub struct Globals {
/// This is ephemeral. It is filled during lazy_static initialization,
/// 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
pub events: RwLock<HashMap<Id, Event>>,
@ -80,13 +88,18 @@ lazy_static! {
let (to_minions, _) = broadcast::channel(256);
// 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 {
db: Mutex::new(None),
to_minions,
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()),
incoming_events: RwLock::new(Vec::new()),
relationships: RwLock::new(HashMap::new()),

View File

@ -15,6 +15,7 @@ mod process;
mod relationship;
mod settings;
mod signer;
mod syncer;
mod ui;
use crate::comms::BusMessage;
@ -67,15 +68,29 @@ fn main() -> Result<(), Error> {
}
async fn tokio_main() {
// Steal `from_minions` from the GLOBALS, and give it to a new Overlord
let from_minions = {
let mut mutex_option = GLOBALS.from_minions.lock().await;
// note: we don't get a spawn handle here, we don't signal this thread when we are exiting,
// we just let it die when tokio_main() exits. I think that is ok.
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)
}
.unwrap();
// Run the overlord
let mut overlord = crate::overlord::Overlord::new(from_minions);
let mut overlord = crate::overlord::Overlord::new(overlord_receiver);
overlord.run().await;
}

View File

@ -114,6 +114,9 @@ impl Overlord {
.insert(Url::new(&relay.url), relay.clone());
}
// Test the syncer
let _ = GLOBALS.to_syncer.send("test".to_owned());
// Load people from the database
{
let mut dbpeople = DbPerson::fetch(None).await?;

32
src/syncer.rs Normal file
View 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);
}
}
}
}
}