Skip to content

Commit

Permalink
update: smol cleanup (#11)
Browse files Browse the repository at this point in the history
* moved signal handling to its own Rust module
* removed some comments in gobot

Signed-off-by: Milos Gajdos <[email protected]>
  • Loading branch information
milosgajdos authored Apr 24, 2024
1 parent 469d559 commit 5348ef9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
3 changes: 1 addition & 2 deletions gobot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ func main() {
case <-ctx.Done():
}

// NOTE: this must run on the main thread otherwise bad things happen:
// beep uses portaudio which requires to be running on the main thread
// TODO: this must run on the main thread otherwise bad things happen
streamer, format, err := mp3.Decode(pipeReader)
if err != nil {
log.Printf("failed to initialize MP3 decoder: %v\n", err)
Expand Down
2 changes: 1 addition & 1 deletion rustbot/src/jet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl Writer {
},
Some(chunk) = chunks.recv() => {
if chunk.is_empty() {
let msg = String::from_utf8(b.to_vec()).unwrap();
let msg = String::from_utf8(b.to_vec())?;
println!("\n[A]: {}", msg);
loop {
tokio::select! {
Expand Down
16 changes: 5 additions & 11 deletions rustbot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use clap::Parser;
use prelude::*;
use rodio::{OutputStream, Sink};
use tokio::{
self, io, signal,
self, io,
sync::{mpsc, watch},
task::JoinHandle,
};

mod audio;
Expand All @@ -15,6 +14,7 @@ mod history;
mod jet;
mod llm;
mod prelude;
mod signal;
mod tts;

#[tokio::main]
Expand Down Expand Up @@ -75,16 +75,10 @@ async fn main() -> Result<()> {
let jet_write = tokio::spawn(s.writer.write(jet_chunks_rx, aud_done_rx, jet_wr_watch_rx));
let jet_read = tokio::spawn(s.reader.read(prompts_tx, jet_rd_watch_rx));
let audio_task = tokio::spawn(audio::play(audio_rd, sink, aud_done_tx, aud_watch_rx));
let sig_handler: JoinHandle<Result<()>> = tokio::spawn(async move {
tokio::select! {
_ = signal::ctrl_c() => {
println!("shutting down, received SIGINT signal...");
watch_tx.send(true)?;
}
}
Ok(())
});
let sig_handler = tokio::spawn(signal::trap(watch_tx));

// NOTE: we're not waiting for the signal handler here:
// we abort it once any of the spawn worker tasks exits.
match tokio::try_join!(tts_stream, llm_stream, jet_write, jet_read, audio_task) {
Ok(_) => {}
Err(e) => {
Expand Down
12 changes: 12 additions & 0 deletions rustbot/src/signal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::prelude::*;
use tokio::{self, signal, sync::watch};

pub async fn trap(done: watch::Sender<bool>) -> Result<()> {
tokio::select! {
_ = signal::ctrl_c() => {
println!("shutting down, received SIGINT signal...");
done.send(true)?;
}
}
Ok(())
}

0 comments on commit 5348ef9

Please sign in to comment.