Skip to content

Commit d1bbc39

Browse files
committed
chore: Improve shutdown.
1 parent 65a9a04 commit d1bbc39

File tree

6 files changed

+45
-16
lines changed

6 files changed

+45
-16
lines changed

crates/download-manager/src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ impl DownloadManager {
5050
/// HANDLES.lock().unwrap().push(run_service(async move {
5151
/// run_service_stream(sender);
5252
/// }));
53-
pub fn run_service_stream(&'static self, cancelation: impl Future<Output = ()> + Clone + Send + 'static, sender: MessageHandler) {
53+
pub fn run_service_stream(
54+
&'static self,
55+
cancelation: impl Future<Output = ()> + Clone + Send + 'static,
56+
sender: MessageHandler,
57+
) {
5458
let fut = async move {
5559
loop {
5660
if let Some(id) = self.take() {
@@ -69,13 +73,21 @@ impl DownloadManager {
6973
self.handles.lock().unwrap().push(service);
7074
}
7175

72-
pub fn spawn_system(&'static self, cancelation: impl Future<Output = ()> + Clone + Send + 'static, sender: MessageHandler) {
76+
pub fn spawn_system(
77+
&'static self,
78+
cancelation: impl Future<Output = ()> + Clone + Send + 'static,
79+
sender: MessageHandler,
80+
) {
7381
for _ in 0..DOWNLOADER_COUNT {
7482
self.run_service_stream(cancelation.clone(), sender.clone());
7583
}
7684
}
7785

78-
pub fn clean(&'static self, cancelation: impl Future<Output = ()> + Clone + Send + 'static, sender: MessageHandler) {
86+
pub fn clean(
87+
&'static self,
88+
cancelation: impl Future<Output = ()> + Clone + Send + 'static,
89+
sender: MessageHandler,
90+
) {
7991
self.download_list.lock().unwrap().clear();
8092
self.in_download.lock().unwrap().clear();
8193
{

crates/ytermusic/src/main.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ use structures::performance::STARTUP_TIME;
66
use term::{Manager, ManagerMessage};
77
use tokio::select;
88

9-
use std::{future::Future, panic, path::{Path, PathBuf}, str::FromStr, sync::RwLock};
9+
use std::{
10+
future::Future,
11+
panic,
12+
path::{Path, PathBuf},
13+
str::FromStr,
14+
sync::RwLock,
15+
};
1016
use systems::{logger::init, player::player_system};
1117

1218
use crate::{
@@ -20,12 +26,12 @@ mod config;
2026
mod consts;
2127
mod database;
2228
mod errors;
29+
mod shutdown;
2330
mod structures;
2431
mod systems;
2532
mod term;
2633
mod utils;
27-
mod shutdown;
28-
pub use shutdown::{ShutdownSignal, is_shutdown_sent, shutdown};
34+
pub use shutdown::{is_shutdown_sent, shutdown, ShutdownSignal};
2935
mod tasks;
3036

3137
pub use database::DATABASE;
@@ -231,10 +237,7 @@ async fn app_start_main(updater_r: Receiver<ManagerMessage>, updater_s: Sender<M
231237
// Spawn the player task
232238
let (sa, player) = player_system(updater_s.clone());
233239
// Spawn the downloader system
234-
DOWNLOAD_MANAGER.spawn_system(
235-
ShutdownSignal,
236-
download_manager_handler(sa.clone()),
237-
);
240+
DOWNLOAD_MANAGER.spawn_system(ShutdownSignal, download_manager_handler(sa.clone()));
238241
STARTUP_TIME.log("Spawned system task");
239242
tasks::last_playlist::spawn_last_playlist_task(updater_s.clone());
240243
STARTUP_TIME.log("Spawned last playlist task");
@@ -248,6 +251,7 @@ async fn app_start_main(updater_r: Receiver<ManagerMessage>, updater_s: Sender<M
248251
let mut manager = Manager::new(sa, player).await;
249252
manager.run(&updater_r).unwrap();
250253
}
254+
251255
fn app_start() {
252256
let (updater_s, updater_r) = flume::unbounded::<ManagerMessage>();
253257
let updater_s_c = updater_s.clone();

crates/ytermusic/src/shutdown.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
use std::{future::Future, pin::Pin, sync::atomic::{AtomicBool, Ordering}, task::{Context, Poll}};
1+
use std::{
2+
future::Future,
3+
pin::Pin,
4+
sync::atomic::{AtomicBool, Ordering},
5+
task::{Context, Poll},
6+
};
27

38
use log::info;
49

5-
610
static SHUTDOWN_SENT: AtomicBool = AtomicBool::new(false);
711

812
pub fn is_shutdown_sent() -> bool {
@@ -27,4 +31,4 @@ impl Future for ShutdownSignal {
2731
pub fn shutdown() {
2832
SHUTDOWN_SENT.store(true, Ordering::Relaxed);
2933
info!("Shutdown signal sent, waiting for shutdown");
30-
}
34+
}

crates/ytermusic/src/structures/sound_action.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use std::{fs, sync::Arc, time::Duration};
66
use ytpapi2::YoutubeMusicVideoRef;
77

88
use crate::{
9-
DATABASE, ShutdownSignal, consts::CACHE_DIR, errors::handle_error_option, systems::{DOWNLOAD_MANAGER, player::PlayerState}
9+
consts::CACHE_DIR,
10+
errors::handle_error_option,
11+
systems::{player::PlayerState, DOWNLOAD_MANAGER},
12+
ShutdownSignal, DATABASE,
1013
};
1114

1215
/// Actions that can be sent to the player from other services

crates/ytermusic/src/term/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use ratatui::{backend::CrosstermBackend, layout::Rect, Frame, Terminal};
2525
use ytpapi2::YoutubeMusicVideoRef;
2626

2727
use crate::{
28-
is_shutdown_sent, shutdown, structures::sound_action::SoundAction, systems::player::PlayerState
28+
is_shutdown_sent, shutdown, structures::sound_action::SoundAction, systems::player::PlayerState,
2929
};
3030

3131
use self::{device_lost::DeviceLost, item_list::ListItem, playlist::Chooser, search::Search};

crates/ytermusic/src/term/search.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ use ytpapi2::{
1616
};
1717

1818
use crate::{
19-
DATABASE, ShutdownSignal, consts::CONFIG, get_header_file, run_service, structures::sound_action::{SoundAction, download_manager_handler}, systems::DOWNLOAD_MANAGER, try_get_cookies, utils::invert
19+
consts::CONFIG,
20+
get_header_file, run_service,
21+
structures::sound_action::{download_manager_handler, SoundAction},
22+
systems::DOWNLOAD_MANAGER,
23+
try_get_cookies,
24+
utils::invert,
25+
ShutdownSignal, DATABASE,
2026
};
2127

2228
use super::{

0 commit comments

Comments
 (0)