|
| 1 | +mod task; |
| 2 | + |
| 3 | +use std::{ |
| 4 | + collections::{HashSet, VecDeque}, |
| 5 | + path::PathBuf, |
| 6 | + sync::{Arc, Mutex}, |
| 7 | + time::Duration, |
| 8 | +}; |
| 9 | + |
| 10 | +use database::YTLocalDatabase; |
| 11 | +use flume::Receiver; |
| 12 | +use tokio::{select, task::JoinHandle, time::sleep}; |
| 13 | +use ytpapi2::YoutubeMusicVideoRef; |
| 14 | + |
| 15 | +use common_structs::MusicDownloadStatus; |
| 16 | + |
| 17 | +pub type MessageHandler = Arc<dyn Fn(DownloadManagerMessage) + Send + Sync + 'static>; |
| 18 | + |
| 19 | +pub enum DownloadManagerMessage { |
| 20 | + VideoStatusUpdate(String, MusicDownloadStatus), |
| 21 | +} |
| 22 | + |
| 23 | +pub struct DownloadManager { |
| 24 | + database: &'static YTLocalDatabase, |
| 25 | + cache_dir: PathBuf, |
| 26 | + handles: Mutex<Vec<JoinHandle<()>>>, |
| 27 | + download_list: Mutex<VecDeque<YoutubeMusicVideoRef>>, |
| 28 | + in_download: Mutex<HashSet<String>>, |
| 29 | +} |
| 30 | + |
| 31 | +impl DownloadManager { |
| 32 | + pub fn new(cache_dir: PathBuf, database: &'static YTLocalDatabase) -> Self { |
| 33 | + Self { |
| 34 | + database, |
| 35 | + cache_dir, |
| 36 | + handles: Mutex::new(Vec::new()), |
| 37 | + download_list: Mutex::new(VecDeque::new()), |
| 38 | + in_download: Mutex::new(HashSet::new()), |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + pub fn remove_from_in_downloads(&self, video: &String) { |
| 43 | + self.in_download.lock().unwrap().remove(video); |
| 44 | + } |
| 45 | + |
| 46 | + fn take(&self) -> Option<YoutubeMusicVideoRef> { |
| 47 | + self.download_list.lock().unwrap().pop_front() |
| 48 | + } |
| 49 | + |
| 50 | + /// This has to be called as a service stream |
| 51 | + /// HANDLES.lock().unwrap().push(run_service(async move { |
| 52 | + /// run_service_stream(sender); |
| 53 | + /// })); |
| 54 | + pub fn run_service_stream(&'static self, cancelation: Receiver<()>, sender: MessageHandler) { |
| 55 | + let fut = async move { |
| 56 | + loop { |
| 57 | + if let Some(id) = self.take() { |
| 58 | + self.start_download(id, sender.clone()).await; |
| 59 | + } else { |
| 60 | + sleep(Duration::from_millis(200)).await; |
| 61 | + } |
| 62 | + } |
| 63 | + }; |
| 64 | + let service = tokio::task::spawn(async move { |
| 65 | + select! { |
| 66 | + _ = fut => {}, |
| 67 | + _ = cancelation.recv_async() => {}, |
| 68 | + } |
| 69 | + }); |
| 70 | + self.handles.lock().unwrap().push(service); |
| 71 | + } |
| 72 | + |
| 73 | + pub fn spawn_system(&'static self, cancelation: Receiver<()>, sender: MessageHandler) { |
| 74 | + for _ in 0..DOWNLOADER_COUNT { |
| 75 | + self.run_service_stream(cancelation.clone(), sender.clone()); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + pub fn clean(&'static self, cancelation: Receiver<()>, sender: MessageHandler) { |
| 80 | + self.download_list.lock().unwrap().clear(); |
| 81 | + self.in_download.lock().unwrap().clear(); |
| 82 | + { |
| 83 | + let mut handle = self.handles.lock().unwrap(); |
| 84 | + for i in handle.iter() { |
| 85 | + i.abort() |
| 86 | + } |
| 87 | + handle.clear(); |
| 88 | + } |
| 89 | + self.spawn_system(cancelation, sender); |
| 90 | + } |
| 91 | + |
| 92 | + pub fn set_download_list(&self, to_add: impl IntoIterator<Item = YoutubeMusicVideoRef>) { |
| 93 | + let mut list = self.download_list.lock().unwrap(); |
| 94 | + list.clear(); |
| 95 | + list.extend(to_add); |
| 96 | + } |
| 97 | + |
| 98 | + pub fn add_to_download_list(&self, to_add: impl IntoIterator<Item = YoutubeMusicVideoRef>) { |
| 99 | + let mut list = self.download_list.lock().unwrap(); |
| 100 | + list.extend(to_add); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +const DOWNLOADER_COUNT: usize = 4; |
0 commit comments