1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/*
This file is part of jellything (https://codeberg.org/metamuffin/jellything)
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2026 metamuffin <metamuffin.org>
*/
use anyhow::Result;
use log::debug;
use rayon::{current_num_threads, current_thread_index};
use serde::Serialize;
use tokio::sync::RwLock;
pub static IMPORT_ERRORS: RwLock<Vec<String>> = RwLock::const_new(Vec::new());
pub static IMPORT_PROGRESS: RwLock<Option<ImportProgress>> = RwLock::const_new(None);
#[derive(Debug, Serialize, Clone)]
pub struct ImportProgress {
pub stage: String,
pub total_items: usize,
pub finished_items: usize,
pub tasks: Vec<String>,
}
pub(crate) fn start_import() {
*IMPORT_ERRORS.blocking_write() = Vec::new();
*IMPORT_PROGRESS.blocking_write() = Some(ImportProgress {
stage: "Initializing".to_string(),
total_items: 0,
finished_items: 0,
tasks: vec!["idle".to_string(); current_num_threads()],
});
}
pub(crate) fn end_import() {
*IMPORT_PROGRESS.blocking_write() = None;
}
pub(crate) fn set_stage(name: String, total: usize) {
if let Some(p) = IMPORT_PROGRESS.blocking_write().as_mut() {
p.stage = name;
p.total_items = total;
p.finished_items = 0;
}
}
pub(crate) fn inc_finished() {
if let Some(p) = IMPORT_PROGRESS.blocking_write().as_mut() {
p.finished_items += 1;
}
}
pub(crate) fn inc_total() {
if let Some(p) = IMPORT_PROGRESS.blocking_write().as_mut() {
p.total_items += 1;
}
}
pub(crate) fn set_task(task: String) {
let thread = current_thread_index().unwrap_or(0);
debug!("T#{thread:<3}| {task}");
if let Some(p) = IMPORT_PROGRESS.blocking_write().as_mut() {
p.tasks[thread] = task
}
}
pub(crate) fn catch<T>(res: Result<T>) -> Option<T> {
match res {
Ok(res) => Some(res),
Err(e) => {
IMPORT_ERRORS.blocking_write().push(format!("{e:#}"));
None
}
}
}
|