Files
corro-dj/apps/worker/src/main.rs
T
2026-03-02 23:34:51 +01:00

60 lines
1.6 KiB
Rust

mod workers;
use futures::StreamExt;
use postcard::{from_bytes, to_stdvec};
use types::jobs::JobResponse;
use which::which;
use yt_dlp::{Downloader, client::Libraries};
#[tokio::main]
async fn main() {
let nats_client = async_nats::connect("nats://localhost:4222")
.await
.expect("Error creating nats client");
let mut subscriber = nats_client
.queue_subscribe("corro-dj.*", "group1".to_string())
.await
.unwrap();
let libraries = Libraries::new(which("yt-dlp").unwrap(), which("ffmpeg").unwrap());
let downloader = Downloader::builder(libraries, "output")
.build()
.await
.unwrap();
// Receive and process messages
while let Some(message) = subscriber.next().await {
println!("Received message {:?}", message);
let subject = message.subject.split(".").collect::<Vec<&str>>()[1];
let result = match subject {
"download" => {
workers::download::download(&downloader, from_bytes(&message.payload).unwrap())
.await
}
_ => {
println!("{subject}");
continue;
}
};
let response = match result {
Ok(response) => response,
Err(err) => JobResponse {
content: None,
error: Some(format!("{err}")),
},
};
if let Some(reply) = message.reply {
nats_client
.publish(reply, to_stdvec(&response).unwrap().into())
.await
.unwrap();
}
}
}