Simplify the code

This commit is contained in:
sxyazi 2023-12-06 00:58:05 +08:00
parent 04316e6242
commit dba0c643b7
No known key found for this signature in database
5 changed files with 43 additions and 56 deletions

View file

@ -66,12 +66,12 @@ rules = [
]
[tasks]
micro_workers = 5
macro_workers = 10
bizarre_retry = 5
image_alloc = 536870912 # 512MB
image_bound = [ 0, 0 ]
ignore_precaching_tasks = true
micro_workers = 5
macro_workers = 10
bizarre_retry = 5
image_alloc = 536870912 # 512MB
image_bound = [ 0, 0 ]
suppress_preload = false
[plugins]
preload = []

View file

@ -15,7 +15,7 @@ pub struct Tasks {
pub image_alloc: u32,
pub image_bound: [u16; 2],
pub ignore_precaching_tasks: bool,
pub suppress_preload: bool,
}
impl Default for Tasks {

View file

@ -16,9 +16,9 @@ pub struct Running {
}
impl Running {
pub(super) fn add(&mut self, name: String) -> usize {
pub(super) fn add(&mut self, kind: TaskKind, name: String) -> usize {
self.incr += 1;
self.all.insert(self.incr, Task::new(self.incr, name));
self.all.insert(self.incr, Task::new(self.incr, kind, name));
self.incr
}
@ -32,23 +32,28 @@ impl Running {
pub fn get_id(&self, idx: usize) -> Option<usize> { self.values().nth(idx).map(|t| t.id) }
#[inline]
pub fn len(&self) -> usize { self.all.len() }
pub fn len(&self) -> usize {
if TASKS.suppress_preload {
self.all.values().filter(|t| t.kind != TaskKind::Preload).count()
} else {
self.all.len()
}
}
#[inline]
pub(super) fn exists(&self, id: usize) -> bool { self.all.contains_key(&id) }
#[inline]
pub fn values(&self) -> impl Iterator<Item = &Task> {
let map = self.all.values();
if TASKS.ignore_precaching_tasks {
map.into_iter().filter(|t| t.kind == TaskKind::User).collect::<Vec<_>>().into_iter()
pub fn values(&self) -> Box<dyn Iterator<Item = &Task> + '_> {
if TASKS.suppress_preload {
Box::new(self.all.values().filter(|t| t.kind != TaskKind::Preload))
} else {
map.into_iter().collect::<Vec<_>>().into_iter()
Box::new(self.all.values())
}
}
#[inline]
pub fn is_empty(&self) -> bool { self.all.is_empty() }
pub fn is_empty(&self) -> bool { self.len() == 0 }
pub(super) fn try_remove(
&mut self,

View file

@ -174,7 +174,7 @@ impl Scheduler {
pub fn file_cut(&self, from: Url, mut to: Url, force: bool) {
let mut running = self.running.write();
let id = running.add(format!("Cut {:?} to {:?}", from, to));
let id = running.add(TaskKind::User, format!("Cut {:?} to {:?}", from, to));
running.hooks.insert(id, {
let from = from.clone();
@ -205,7 +205,7 @@ impl Scheduler {
pub fn file_copy(&self, from: Url, mut to: Url, force: bool) {
let name = format!("Copy {:?} to {:?}", from, to);
let id = self.running.write().add(name);
let id = self.running.write().add(TaskKind::User, name);
_ = self.todo.send_blocking({
let file = self.file.clone();
@ -221,7 +221,7 @@ impl Scheduler {
pub fn file_link(&self, from: Url, mut to: Url, relative: bool, force: bool) {
let name = format!("Link {from:?} to {to:?}");
let id = self.running.write().add(name);
let id = self.running.write().add(TaskKind::User, name);
_ = self.todo.send_blocking({
let file = self.file.clone();
@ -240,7 +240,7 @@ impl Scheduler {
pub fn file_delete(&self, target: Url) {
let mut running = self.running.write();
let id = running.add(format!("Delete {:?}", target));
let id = running.add(TaskKind::User, format!("Delete {:?}", target));
running.hooks.insert(id, {
let target = target.clone();
@ -268,7 +268,7 @@ impl Scheduler {
pub fn file_trash(&self, target: Url) {
let name = format!("Trash {:?}", target);
let id = self.running.write().add(name);
let id = self.running.write().add(TaskKind::User, name);
_ = self.todo.send_blocking({
let file = self.file.clone();
@ -287,7 +287,7 @@ impl Scheduler {
};
let mut running = self.running.write();
let id = running.add(name);
let id = running.add(TaskKind::User, name);
let (cancel_tx, mut cancel_rx) = oneshot::channel();
running.hooks.insert(id, {
@ -335,11 +335,7 @@ impl Scheduler {
continue;
}
let id = running.add(format!("Calculate the size of {:?}", target));
if let Some(task) = self.running.clone().write().get_mut(id) {
task.kind = TaskKind::PreCache;
}
let id = running.add(TaskKind::Preload, format!("Calculate the size of {:?}", target));
_ = self.todo.send_blocking({
let precache = self.precache.clone();
let target = target.clone();
@ -354,11 +350,7 @@ impl Scheduler {
pub fn precache_mime(&self, targets: Vec<Url>) {
let name = format!("Preload mimetype for {} files", targets.len());
let id = self.running.write().add(name);
if let Some(task) = self.running.clone().write().get_mut(id) {
task.kind = TaskKind::PreCache;
}
let id = self.running.write().add(TaskKind::Preload, name);
_ = self.todo.send_blocking({
let precache = self.precache.clone();
@ -371,33 +363,21 @@ impl Scheduler {
pub fn precache_image(&self, targets: Vec<Url>) {
let name = format!("Precache of {} image files", targets.len());
let id = self.running.write().add(name);
if let Some(task) = self.running.clone().write().get_mut(id) {
task.kind = TaskKind::PreCache;
}
let id = self.running.write().add(TaskKind::Preload, name);
self.precache.image(id, targets).ok();
}
pub fn precache_video(&self, targets: Vec<Url>) {
let name = format!("Precache of {} video files", targets.len());
let id = self.running.write().add(name);
if let Some(task) = self.running.clone().write().get_mut(id) {
task.kind = TaskKind::PreCache;
}
let id = self.running.write().add(TaskKind::Preload, name);
self.precache.video(id, targets).ok();
}
pub fn precache_pdf(&self, targets: Vec<Url>) {
let name = format!("Precache of {} PDF files", targets.len());
let id = self.running.write().add(name);
if let Some(task) = self.running.clone().write().get_mut(id) {
task.kind = TaskKind::PreCache;
}
let id = self.running.write().add(TaskKind::Preload, name);
self.precache.pdf(id, targets).ok();
}

View file

@ -3,9 +3,9 @@ use tokio::sync::mpsc;
#[derive(Debug, Default)]
pub struct Task {
pub id: usize,
pub kind: TaskKind,
pub name: String,
pub stage: TaskStage,
pub kind: TaskKind,
pub total: u32,
pub succ: u32,
@ -19,7 +19,16 @@ pub struct Task {
}
impl Task {
pub fn new(id: usize, name: String) -> Self { Self { id, name, ..Default::default() } }
pub fn new(id: usize, kind: TaskKind, name: String) -> Self {
Self { id, kind, name, ..Default::default() }
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum TaskKind {
#[default]
User,
Preload,
}
#[derive(Debug)]
@ -70,10 +79,3 @@ pub enum TaskStage {
Dispatched,
Hooked,
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
pub enum TaskKind {
#[default]
User,
PreCache,
}