mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
feat(backend::scheduler) deadlock spotted
This commit is contained in:
15 files changed
+373
-426
No files matched your search
File renamed without changes.
+9
-11
@@ -1,12 +1,10 @@
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::{
|
|
||||||
modules::blog::cron::check_update::utils::get_md_file_basename,
|
|
||||||
modules::blog::cron::check_update::{error::PostIdxError, utils::parse_date},
|
|
||||||
};
|
|
||||||
use markdown::MdFile;
|
use markdown::MdFile;
|
||||||
|
|
||||||
|
use crate::modules::blog::components::check_update::utils::get_md_file_basename;
|
||||||
|
use crate::modules::blog::components::check_update::{error::PostIdxError, utils::parse_date};
|
||||||
use sqlx::{query, PgPool, Pool, Postgres, Row};
|
use sqlx::{query, PgPool, Pool, Postgres, Row};
|
||||||
use tokio::runtime::Runtime;
|
use tokio::runtime::Runtime;
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
@@ -18,7 +16,7 @@ async fn base(db_connection: &PgPool, post: &MdFile) -> Result<(), PostIdxError>
|
|||||||
return Err(PostIdxError::MissingField {
|
return Err(PostIdxError::MissingField {
|
||||||
field: "title".to_string(),
|
field: "title".to_string(),
|
||||||
filename: post.filename.clone(),
|
filename: post.filename.clone(),
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
Some(title) => title.as_str().unwrap_or_else(|| {
|
Some(title) => title.as_str().unwrap_or_else(|| {
|
||||||
warn!("invalid title for `{}`", &post.filename);
|
warn!("invalid title for `{}`", &post.filename);
|
||||||
@@ -41,7 +39,7 @@ async fn base(db_connection: &PgPool, post: &MdFile) -> Result<(), PostIdxError>
|
|||||||
return Err(PostIdxError::MissingField {
|
return Err(PostIdxError::MissingField {
|
||||||
field: "category".to_string(),
|
field: "category".to_string(),
|
||||||
filename: post.filename.clone(),
|
filename: post.filename.clone(),
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
Some(title) => title.as_str().unwrap_or_else(|| {
|
Some(title) => title.as_str().unwrap_or_else(|| {
|
||||||
warn!("invalid category for `{}`", &post.filename);
|
warn!("invalid category for `{}`", &post.filename);
|
||||||
@@ -58,7 +56,7 @@ async fn base(db_connection: &PgPool, post: &MdFile) -> Result<(), PostIdxError>
|
|||||||
err_field: last_update.to_string(),
|
err_field: last_update.to_string(),
|
||||||
filename: post.filename.clone(),
|
filename: post.filename.clone(),
|
||||||
msg: e.to_string(),
|
msg: e.to_string(),
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -70,7 +68,7 @@ async fn base(db_connection: &PgPool, post: &MdFile) -> Result<(), PostIdxError>
|
|||||||
return Err(PostIdxError::MissingField {
|
return Err(PostIdxError::MissingField {
|
||||||
field: "date".to_string(),
|
field: "date".to_string(),
|
||||||
filename: post.filename.clone(),
|
filename: post.filename.clone(),
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
Some(first_update) => match parse_date(first_update.as_str().unwrap()) {
|
Some(first_update) => match parse_date(first_update.as_str().unwrap()) {
|
||||||
Ok(date) => date,
|
Ok(date) => date,
|
||||||
@@ -80,7 +78,7 @@ async fn base(db_connection: &PgPool, post: &MdFile) -> Result<(), PostIdxError>
|
|||||||
err_field: first_update.to_string(),
|
err_field: first_update.to_string(),
|
||||||
filename: post.filename.clone(),
|
filename: post.filename.clone(),
|
||||||
msg: e.to_string(),
|
msg: e.to_string(),
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -137,7 +135,7 @@ async fn tag(db_connection: &PgPool, post: &MdFile) -> Result<(), PostIdxError>
|
|||||||
return Err(PostIdxError::MissingField {
|
return Err(PostIdxError::MissingField {
|
||||||
field: "tags".to_string(),
|
field: "tags".to_string(),
|
||||||
filename: post.filename.clone(),
|
filename: post.filename.clone(),
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
Some(val) => val,
|
Some(val) => val,
|
||||||
};
|
};
|
||||||
@@ -173,7 +171,7 @@ async fn tag(db_connection: &PgPool, post: &MdFile) -> Result<(), PostIdxError>
|
|||||||
db_table: "Tag".to_string(),
|
db_table: "Tag".to_string(),
|
||||||
id: post_id.to_string(),
|
id: post_id.to_string(),
|
||||||
msg: e.to_string(),
|
msg: e.to_string(),
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
use crate::constants::config::general::{default_value, var_name};
|
||||||
|
use macros::panic_with_log;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::{
|
||||||
|
env,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::modules::blog::components::check_update::{
|
||||||
|
// index::drop_index,
|
||||||
|
// utils::{expand_path, index_files, search_md},
|
||||||
|
utils::expand_path,
|
||||||
|
};
|
||||||
|
use crate::scheduler::task_func::AsyncTaskFunc;
|
||||||
|
use sqlx::PgPool;
|
||||||
|
use tracing::{info, warn, Level};
|
||||||
|
|
||||||
|
// mod error;
|
||||||
|
// mod index;
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Args {
|
||||||
|
git_work_dir: PathBuf,
|
||||||
|
git_url: String,
|
||||||
|
git_remote_name: String,
|
||||||
|
git_remote_branch: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn task(db_connection: PgPool) -> impl AsyncTaskFunc {
|
||||||
|
// init
|
||||||
|
let git_work_dir = {
|
||||||
|
let path = if let Ok(default_log_path) = env::var(var_name::WORK_DIR) {
|
||||||
|
expand_path(default_log_path)
|
||||||
|
} else {
|
||||||
|
let fallback_path = expand_path(default_value::WORK_DIR.to_string());
|
||||||
|
warn!(
|
||||||
|
"unset env var `{}`, using default path: {}/blog_git",
|
||||||
|
var_name::WORK_DIR,
|
||||||
|
fallback_path
|
||||||
|
);
|
||||||
|
fallback_path
|
||||||
|
};
|
||||||
|
Path::new(&path).join("blog_git")
|
||||||
|
};
|
||||||
|
|
||||||
|
// clone & check update
|
||||||
|
|
||||||
|
let git_url = match env::var(var_name::BLOG_GIT_URL) {
|
||||||
|
Ok(url) => url,
|
||||||
|
Err(e) => {
|
||||||
|
panic_with_log!(
|
||||||
|
Level::ERROR,
|
||||||
|
"failed to read `{}`: {}",
|
||||||
|
var_name::BLOG_GIT_URL,
|
||||||
|
e
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let git_remote_name = env::var(var_name::BLOG_GIT_REMOTE_NAME)
|
||||||
|
.unwrap_or(default_value::BLOG_GIT_REMOTE_NAME.to_string());
|
||||||
|
let git_remote_branch = env::var(var_name::BLOG_GIT_REMOTE_BRANCH)
|
||||||
|
.unwrap_or(default_value::BLOG_GIT_REMOTE_BRANCH.to_string());
|
||||||
|
|
||||||
|
let arc_args = Arc::new(Args {
|
||||||
|
git_work_dir,
|
||||||
|
git_url,
|
||||||
|
git_remote_name,
|
||||||
|
git_remote_branch,
|
||||||
|
});
|
||||||
|
|
||||||
|
move || Box::pin(test(arc_args.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn test(args: Arc<Args>) {
|
||||||
|
info!("test");
|
||||||
|
dbg!(&args);
|
||||||
|
}
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
use crate::constants::{
|
||||||
|
config::general::{default_value, var_name},
|
||||||
|
time::DEFAULT_DATE_FORMAT,
|
||||||
|
};
|
||||||
|
|
||||||
|
// use crate::modules::blog::components::check_update::index::index_file;
|
||||||
|
use chrono::{DateTime, FixedOffset, ParseResult};
|
||||||
|
use dirs::home_dir;
|
||||||
|
use tracing::warn;
|
||||||
|
|
||||||
|
// pub fn search_md(entry: PathBuf) -> Vec<PathBuf> {
|
||||||
|
// if entry.ends_with(".git") {
|
||||||
|
// return vec![];
|
||||||
|
// }
|
||||||
|
// if entry.is_dir() {
|
||||||
|
// if let Ok(files) = entry.read_dir() {
|
||||||
|
// files
|
||||||
|
// .flat_map(|file| {
|
||||||
|
// let file_path = file.unwrap().path();
|
||||||
|
// search_md(file_path)
|
||||||
|
// })
|
||||||
|
// .collect()
|
||||||
|
// } else {
|
||||||
|
// vec![]
|
||||||
|
// }
|
||||||
|
// } else if entry.extension().is_some_and(|ext| ext == "md") {
|
||||||
|
// vec![entry]
|
||||||
|
// } else {
|
||||||
|
// vec![]
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn index_files(files: &[PathBuf], db_conn: &Pool<Postgres>) {
|
||||||
|
// let rt = Runtime::new().unwrap();
|
||||||
|
// rt.block_on(async {
|
||||||
|
// let files_clone = files.to_owned();
|
||||||
|
// let handlers = files_clone
|
||||||
|
// .into_iter()
|
||||||
|
// .map(|file: PathBuf| {
|
||||||
|
// let conn_clone = db_conn.clone();
|
||||||
|
// spawn(async move {
|
||||||
|
// if let Err(e) = index_file(&file, &conn_clone).await {
|
||||||
|
// error!("failed to index markdown file `{}`: {}", file.display(), e);
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
// })
|
||||||
|
// .collect::<Vec<_>>();
|
||||||
|
//
|
||||||
|
// block_on(join_all(handlers));
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
|
||||||
|
pub fn expand_path(path: String) -> String {
|
||||||
|
if path.starts_with('~') {
|
||||||
|
home_dir()
|
||||||
|
.map(|dir| {
|
||||||
|
dir.join(path.strip_prefix("~/").unwrap())
|
||||||
|
.into_os_string()
|
||||||
|
.into_string()
|
||||||
|
.unwrap()
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
warn!(
|
||||||
|
"failed to parse env var `{}`, using default value `{}`",
|
||||||
|
var_name::WORK_DIR,
|
||||||
|
default_value::WORK_DIR
|
||||||
|
);
|
||||||
|
default_value::WORK_DIR.to_string()
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_date(date_str: &str) -> ParseResult<DateTime<FixedOffset>> {
|
||||||
|
DateTime::parse_from_str(format!("{}T120000", date_str).as_str(), DEFAULT_DATE_FORMAT)
|
||||||
|
}
|
||||||
|
|
||||||
|
// pub fn get_md_file_basename(f_name: &str) -> String {
|
||||||
|
// f_name.replace(".md", "")
|
||||||
|
// }
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod check_update;
|
||||||
|
mod rag;
|
||||||
Whitespace-only changes.
@@ -1,148 +0,0 @@
|
|||||||
use std::{
|
|
||||||
env,
|
|
||||||
fs::remove_dir_all,
|
|
||||||
path::{Path, PathBuf},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
constants::config::general::{default_value, var_name},
|
|
||||||
modules::blog::cron::check_update::{
|
|
||||||
index::drop_index,
|
|
||||||
utils::{expand_path, get_md_file_basename, index_files, search_md},
|
|
||||||
},
|
|
||||||
utils::git,
|
|
||||||
};
|
|
||||||
use macros::panic_with_log;
|
|
||||||
|
|
||||||
use git2::{Delta, Repository};
|
|
||||||
use sqlx::{PgPool, Pool, Postgres};
|
|
||||||
use tracing::{error, info, warn, Level};
|
|
||||||
|
|
||||||
mod error;
|
|
||||||
mod index;
|
|
||||||
mod utils;
|
|
||||||
|
|
||||||
pub fn task(db_connection: &PgPool) {
|
|
||||||
// init
|
|
||||||
let git_work_dir = {
|
|
||||||
let path = if let Ok(default_log_path) = env::var(var_name::WORK_DIR) {
|
|
||||||
expand_path(default_log_path)
|
|
||||||
} else {
|
|
||||||
let fallback_path = expand_path(default_value::WORK_DIR.to_string());
|
|
||||||
warn!(
|
|
||||||
"unset env var `{}`, using default path: {}/blog_git",
|
|
||||||
var_name::WORK_DIR,
|
|
||||||
fallback_path
|
|
||||||
);
|
|
||||||
fallback_path
|
|
||||||
};
|
|
||||||
Path::new(&path).join("blog_git")
|
|
||||||
};
|
|
||||||
|
|
||||||
// clone & check update
|
|
||||||
|
|
||||||
let git_url = match env::var(var_name::BLOG_GIT_URL) {
|
|
||||||
Ok(url) => url,
|
|
||||||
Err(e) => {
|
|
||||||
panic_with_log!(
|
|
||||||
Level::ERROR,
|
|
||||||
"failed to read `{}`: {}",
|
|
||||||
var_name::BLOG_GIT_URL,
|
|
||||||
e
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let git_remote_name = env::var(var_name::BLOG_GIT_REMOTE_NAME)
|
|
||||||
.unwrap_or(default_value::BLOG_GIT_REMOTE_NAME.to_string());
|
|
||||||
let git_remote_branch = env::var(var_name::BLOG_GIT_REMOTE_BRANCH)
|
|
||||||
.unwrap_or(default_value::BLOG_GIT_REMOTE_BRANCH.to_string());
|
|
||||||
|
|
||||||
match Repository::open(&git_work_dir) {
|
|
||||||
Ok(repo) => {
|
|
||||||
// prevent git remote url mismatch (clean cache)
|
|
||||||
match repo.find_remote(&git_remote_name) {
|
|
||||||
Ok(remote) => {
|
|
||||||
if remote.url().is_some_and(|url| url != git_url) {
|
|
||||||
remove_dir_all(&git_work_dir).unwrap();
|
|
||||||
warn!("blog content git({}) url mismatch, reinitialized", &git_url);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
remove_dir_all(&git_work_dir).unwrap();
|
|
||||||
error!(
|
|
||||||
"unexpected error on indexing blog content git({}), reinitialized: {}",
|
|
||||||
&git_url, e
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match git::sync(&git_remote_name, &git_remote_branch, &repo) {
|
|
||||||
Ok(updates) => {
|
|
||||||
if !updates.is_empty() {
|
|
||||||
// incremental update
|
|
||||||
info!(
|
|
||||||
"blog content git({}) update detected: {:?}",
|
|
||||||
&git_url, updates
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut update_md: Vec<PathBuf> = vec![];
|
|
||||||
|
|
||||||
for update in updates {
|
|
||||||
if [Delta::Deleted, Delta::Untracked].contains(&update.status) {
|
|
||||||
if let Some(path) = &update.old_path {
|
|
||||||
if drop_index(
|
|
||||||
&get_md_file_basename(
|
|
||||||
path.file_name().unwrap().to_str().unwrap(),
|
|
||||||
),
|
|
||||||
db_connection,
|
|
||||||
)
|
|
||||||
.is_ok()
|
|
||||||
{
|
|
||||||
info!(
|
|
||||||
"blog content git({}) file `{}` is detected as deleted/untracked, index dropped",
|
|
||||||
&git_url,
|
|
||||||
&path.display()
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
error!(
|
|
||||||
"blog content git({}) file `{}` is detected as deleted/untracked, failed to drop index",
|
|
||||||
&git_url,
|
|
||||||
&path.display()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if let Some(path) = &update.new_path {
|
|
||||||
if path.components().any(|comp| comp.as_os_str() == "posts")
|
|
||||||
&& path.extension().is_some_and(|ext| ext == "md")
|
|
||||||
{
|
|
||||||
update_md.push(path.to_path_buf());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
index_files(&update_md, db_connection);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!(
|
|
||||||
"failed to sync blog content git({}) updates: {}",
|
|
||||||
&git_url, e
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
match Repository::clone(&git_url, &git_work_dir) {
|
|
||||||
Ok(_repo) => {
|
|
||||||
// full repo index
|
|
||||||
let files = search_md(git_work_dir.join("posts"));
|
|
||||||
index_files(&files, db_connection);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
error!("failed to clone blog content git({}): {}", &git_url, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,88 +0,0 @@
|
|||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
constants::{
|
|
||||||
config::general::{default_value, var_name},
|
|
||||||
time::DEFAULT_DATE_FORMAT,
|
|
||||||
},
|
|
||||||
modules::blog::cron::check_update::index::index_file,
|
|
||||||
};
|
|
||||||
|
|
||||||
use chrono::{DateTime, FixedOffset, ParseResult};
|
|
||||||
use dirs::home_dir;
|
|
||||||
use futures::{executor::block_on, future::join_all};
|
|
||||||
use sqlx::{Pool, Postgres};
|
|
||||||
use tokio::{runtime::Runtime, spawn};
|
|
||||||
use tracing::{error, warn};
|
|
||||||
|
|
||||||
pub fn search_md(entry: PathBuf) -> Vec<PathBuf> {
|
|
||||||
if entry.ends_with(".git") {
|
|
||||||
return vec![];
|
|
||||||
}
|
|
||||||
if entry.is_dir() {
|
|
||||||
if let Ok(files) = entry.read_dir() {
|
|
||||||
files
|
|
||||||
.flat_map(|file| {
|
|
||||||
let file_path = file.unwrap().path();
|
|
||||||
search_md(file_path)
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
} else {
|
|
||||||
vec![]
|
|
||||||
}
|
|
||||||
} else if entry.extension().is_some_and(|ext| ext == "md") {
|
|
||||||
vec![entry]
|
|
||||||
} else {
|
|
||||||
vec![]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn index_files(files: &[PathBuf], db_conn: &Pool<Postgres>) {
|
|
||||||
let rt = Runtime::new().unwrap();
|
|
||||||
rt.block_on(async {
|
|
||||||
let files_clone = files.to_owned();
|
|
||||||
let handlers = files_clone
|
|
||||||
.into_iter()
|
|
||||||
.map(|file: PathBuf| {
|
|
||||||
let conn_clone = db_conn.clone();
|
|
||||||
spawn(async move {
|
|
||||||
if let Err(e) = index_file(&file, &conn_clone).await {
|
|
||||||
error!("failed to index markdown file `{}`: {}", file.display(), e);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
block_on(join_all(handlers));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn expand_path(path: String) -> String {
|
|
||||||
if path.starts_with('~') {
|
|
||||||
home_dir()
|
|
||||||
.map(|dir| {
|
|
||||||
dir.join(path.strip_prefix("~/").unwrap())
|
|
||||||
.into_os_string()
|
|
||||||
.into_string()
|
|
||||||
.unwrap()
|
|
||||||
})
|
|
||||||
.unwrap_or_else(|| {
|
|
||||||
warn!(
|
|
||||||
"failed to parse env var `{}`, using default value `{}`",
|
|
||||||
var_name::WORK_DIR,
|
|
||||||
default_value::WORK_DIR
|
|
||||||
);
|
|
||||||
default_value::WORK_DIR.to_string()
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
path
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_date(date_str: &str) -> ParseResult<DateTime<FixedOffset>> {
|
|
||||||
DateTime::parse_from_str(format!("{}T120000", date_str).as_str(), DEFAULT_DATE_FORMAT)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_md_file_basename(f_name: &str) -> String {
|
|
||||||
f_name.replace(".md", "")
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
// pub mod check_update;
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
mod api;
|
mod api;
|
||||||
mod cron;
|
mod components;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
modules::{
|
modules::{
|
||||||
@@ -13,9 +13,12 @@ use crate::{
|
|||||||
},
|
},
|
||||||
Module,
|
Module,
|
||||||
},
|
},
|
||||||
scheduler::tasks::{async_basic::AsyncBasic, CronTask},
|
scheduler::tasks::CronTask,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::modules::blog::components::check_update;
|
||||||
|
use crate::scheduler::tasks::async_basic::AsyncBasic;
|
||||||
|
use crate::scheduler::tasks::basic::BasicTask;
|
||||||
use axum::{routing::get, Router};
|
use axum::{routing::get, Router};
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
|
|
||||||
@@ -42,12 +45,12 @@ impl Module for Blog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_cron_tasks(&self) -> Vec<Box<dyn CronTask>> {
|
fn get_cron_tasks(&self) -> Vec<Box<dyn CronTask>> {
|
||||||
// let db_handler = self.db_handler.clone();
|
let db_handler = self.db_handler.clone();
|
||||||
vec![
|
vec![
|
||||||
// BasicTask::new(move || check_update::task(&db_handler), "0/15 * * * * *")
|
AsyncBasic::new(check_update::task(db_handler.clone()), "* * * * * *")
|
||||||
// .unwrap()
|
.unwrap()
|
||||||
// .to_task(),
|
.to_task(),
|
||||||
AsyncBasic::new(|| Box::pin(async { println!("test") }), "* * * * * *")
|
BasicTask::new(|| println!("test lol"), "* * * * * *")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_task(),
|
.to_task(),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
pub mod blog;
|
pub mod blog;
|
||||||
|
|
||||||
use std::env;
|
use std::{env, num::ParseIntError, thread};
|
||||||
use std::num::ParseIntError;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
constants::config::server::{default_value, var_name},
|
constants::config::server::{default_value, var_name},
|
||||||
@@ -10,6 +9,7 @@ use crate::{
|
|||||||
use macros::error_panic;
|
use macros::error_panic;
|
||||||
|
|
||||||
use axum::Router;
|
use axum::Router;
|
||||||
|
use futures::executor::block_on;
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
|
|
||||||
@@ -68,11 +68,15 @@ impl ModuleTree {
|
|||||||
};
|
};
|
||||||
|
|
||||||
for module in &self.modules {
|
for module in &self.modules {
|
||||||
let _ = scheduler.insert_list(module.get_cron_tasks());
|
let _ = scheduler.insert_list(module.get_cron_tasks()).await;
|
||||||
app = app.nest(module.get_mount_point(), module.get_server_router());
|
app = app.nest(module.get_mount_point(), module.get_server_router());
|
||||||
}
|
}
|
||||||
|
|
||||||
scheduler.start().unwrap();
|
let _ = thread::spawn(move || {
|
||||||
|
let fut = scheduler.start();
|
||||||
|
block_on(fut).unwrap()
|
||||||
|
})
|
||||||
|
.join();
|
||||||
|
|
||||||
axum::serve(listener, app).await.unwrap_or_else(|e| {
|
axum::serve(listener, app).await.unwrap_or_else(|e| {
|
||||||
error_panic!("failed to start axum: {}", e.to_string());
|
error_panic!("failed to start axum: {}", e.to_string());
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, HashSet},
|
collections::{HashMap, HashSet},
|
||||||
sync::{Arc, RwLock},
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::scheduler::{
|
use crate::scheduler::{
|
||||||
@@ -9,12 +9,13 @@ use crate::scheduler::{
|
|||||||
tasks::CronTask,
|
tasks::CronTask,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use tokio::sync::RwLock;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
mod runner;
|
mod runner;
|
||||||
mod sleeper;
|
mod sleeper;
|
||||||
mod task_func;
|
pub mod task_func;
|
||||||
pub mod tasks;
|
pub mod tasks;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
@@ -26,43 +27,42 @@ pub struct Scheduler {
|
|||||||
|
|
||||||
impl Scheduler {
|
impl Scheduler {
|
||||||
/// Start all tasks in the scheduler
|
/// Start all tasks in the scheduler
|
||||||
pub fn start(&self) -> Result<(), SchedulerError> {
|
pub async fn start(&self) -> Result<(), SchedulerError> {
|
||||||
self.runner.run_all()
|
self.runner.run_all().await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Terminate all tasks in the scheduler
|
/// Terminate all tasks in the scheduler
|
||||||
pub fn halt(&self) -> Result<(), SchedulerError> {
|
pub async fn halt(&self) -> Result<(), SchedulerError> {
|
||||||
for id in self.running.read()?.iter() {
|
for id in self.running.read().await.iter() {
|
||||||
let func = &self.runner.terminate_remove(*id)?;
|
let func = &self.runner.terminate_remove(*id).await?;
|
||||||
self.running.write()?.remove(id);
|
self.running.write().await.remove(id);
|
||||||
self.parking_pool.write()?.insert(*id, func.clone());
|
self.parking_pool.write().await.insert(*id, func.clone());
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Run a task with `id`
|
/// Run a task with `id`
|
||||||
pub fn run(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
pub async fn run(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
||||||
if self.running.read()?.contains(id) {
|
if self.running.read().await.contains(id) {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else if self.parking_pool.read()?.contains_key(id) {
|
} else if self.parking_pool.read().await.contains_key(id) {
|
||||||
let func = self.parking_pool.write()?.remove(id).unwrap();
|
let func = self.parking_pool.write().await.remove(id).unwrap();
|
||||||
self.running.write()?.insert(*id);
|
self.running.write().await.insert(*id);
|
||||||
self.runner
|
self.runner.add_with_id(func, id).await?;
|
||||||
.add_with_id(func, id)
|
self.runner.run_id(id).await
|
||||||
.and_then(|_| self.runner.run_id(id))
|
|
||||||
} else {
|
} else {
|
||||||
Err(IdNotFound)
|
Err(IdNotFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Stop a task with `id`
|
/// Stop a task with `id`
|
||||||
pub fn stop(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
pub async fn stop(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
||||||
if self.running.read()?.contains(id) {
|
if self.running.read().await.contains(id) {
|
||||||
let func = self.runner.terminate_remove(*id)?;
|
let func = self.runner.terminate_remove(*id).await?;
|
||||||
self.running.write()?.remove(id);
|
self.running.write().await.remove(id);
|
||||||
self.parking_pool.write()?.insert(*id, func.clone());
|
self.parking_pool.write().await.insert(*id, func.clone());
|
||||||
Ok(())
|
Ok(())
|
||||||
} else if self.parking_pool.read()?.get(id).is_some() {
|
} else if self.parking_pool.read().await.get(id).is_some() {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(IdNotFound)
|
Err(IdNotFound)
|
||||||
@@ -72,17 +72,21 @@ impl Scheduler {
|
|||||||
// pub fn now(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
// pub fn now(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
pub fn insert(&self, task: Box<dyn CronTask>) -> Result<Uuid, SchedulerError> {
|
pub async fn insert(&self, task: Box<dyn CronTask>) -> Result<Uuid, SchedulerError> {
|
||||||
let id = self.runner.add(task.into())?;
|
let id = self.runner.add(task.into()).await?;
|
||||||
self.running.write()?.insert(id);
|
self.running.write().await.insert(id);
|
||||||
Ok(id)
|
Ok(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_list(&self, tasks: Vec<Box<dyn CronTask>>) -> Result<Vec<Uuid>, SchedulerError> {
|
pub async fn insert_list(
|
||||||
|
&self,
|
||||||
|
tasks: Vec<Box<dyn CronTask>>,
|
||||||
|
) -> Result<Vec<Uuid>, SchedulerError> {
|
||||||
let ids = self
|
let ids = self
|
||||||
.runner
|
.runner
|
||||||
.add_list(tasks.into_iter().map(|t| t.into()).collect())?;
|
.add_list(tasks.into_iter().map(|t| t.into()).collect())
|
||||||
self.running.write()?.extend(&ids);
|
.await?;
|
||||||
|
self.running.write().await.extend(&ids);
|
||||||
Ok(ids)
|
Ok(ids)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,51 +95,51 @@ impl Scheduler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
// #[cfg(test)]
|
||||||
mod tests {
|
// mod tests {
|
||||||
use std::{error::Error, thread, time::Duration};
|
// use std::{error::Error, thread, time::Duration};
|
||||||
|
//
|
||||||
use crate::scheduler::{
|
// use crate::scheduler::{
|
||||||
tasks::{async_basic::AsyncBasic, basic::BasicTask},
|
// tasks::{async_basic::AsyncBasic, basic::BasicTask},
|
||||||
Scheduler,
|
// Scheduler,
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
use uuid::Uuid;
|
// use uuid::Uuid;
|
||||||
|
//
|
||||||
fn execution(scheduler: Scheduler, id: Uuid) -> Result<(), Box<dyn Error>> {
|
// async fn execution(scheduler: Scheduler, id: Uuid) -> Result<(), Box<dyn Error>> {
|
||||||
for i in 1..=2 {
|
// for i in 1..=2 {
|
||||||
println!("Loop {} start", i);
|
// println!("Loop {} start", i);
|
||||||
println!("Run {}", i);
|
// println!("Run {}", i);
|
||||||
scheduler.run(&id)?;
|
// scheduler.run(&id).await?;
|
||||||
println!("Sleep {}", i);
|
// println!("Sleep {}", i);
|
||||||
thread::sleep(Duration::from_secs(4));
|
// thread::sleep(Duration::from_secs(4));
|
||||||
println!("Sleep {} finished", i);
|
// println!("Sleep {} finished", i);
|
||||||
scheduler.stop(&id)?;
|
// scheduler.stop(&id)?;
|
||||||
println!("Stop {}", i);
|
// println!("Stop {}", i);
|
||||||
thread::sleep(Duration::from_secs(2));
|
// thread::sleep(Duration::from_secs(2));
|
||||||
println!("Loop {} finished", i);
|
// println!("Loop {} finished", i);
|
||||||
}
|
// }
|
||||||
Ok(())
|
// Ok(())
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
#[test]
|
// #[tokio::test]
|
||||||
fn test_run() -> Result<(), Box<dyn Error>> {
|
// async fn test_run() -> Result<(), Box<dyn Error>> {
|
||||||
let scheduler = Scheduler::default();
|
// let scheduler = Scheduler::default();
|
||||||
let task = BasicTask::new(|| println!("test print"), "* * * * * *")?;
|
// let task = BasicTask::new(|| println!("test print"), "* * * * * *")?;
|
||||||
let id = scheduler.insert(Box::new(task))?;
|
// let id = scheduler.insert(Box::new(task)).await?;
|
||||||
scheduler.start()?;
|
// scheduler.start().await?;
|
||||||
execution(scheduler, id)
|
// execution(scheduler, id)
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
#[test]
|
// #[tokio::test]
|
||||||
fn test_run_async() -> Result<(), Box<dyn Error>> {
|
// async fn test_run_async() -> Result<(), Box<dyn Error>> {
|
||||||
let scheduler = Scheduler::default();
|
// let scheduler = Scheduler::default();
|
||||||
let task = AsyncBasic::new(
|
// let task = AsyncBasic::new(
|
||||||
|| Box::pin(async { println!("test async print") }),
|
// || Box::pin(async { println!("test async print") }),
|
||||||
"* * * * * *",
|
// "* * * * * *",
|
||||||
)?;
|
// )?;
|
||||||
let id = scheduler.insert(Box::new(task))?;
|
// let id = scheduler.insert(Box::new(task)).await?;
|
||||||
scheduler.start()?;
|
// scheduler.start().await?;
|
||||||
execution(scheduler, id)
|
// execution(scheduler, id)
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
@@ -1,20 +1,22 @@
|
|||||||
use std::{
|
|
||||||
collections::HashMap,
|
|
||||||
sync::{
|
|
||||||
atomic::{AtomicBool, Ordering}, Arc,
|
|
||||||
RwLock,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::scheduler::{
|
use crate::scheduler::{
|
||||||
error::{SchedulerError, SchedulerError::IdNotFound},
|
error::{SchedulerError, SchedulerError::IdNotFound},
|
||||||
sleeper::SkippableSleeper,
|
sleeper::SkippableSleeper,
|
||||||
tasks::CronTask,
|
tasks::CronTask,
|
||||||
};
|
};
|
||||||
|
use futures::future;
|
||||||
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
sync::{
|
||||||
|
atomic::{AtomicBool, Ordering},
|
||||||
|
Arc,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
use tokio::runtime::Runtime;
|
use tokio::runtime::Runtime;
|
||||||
|
use tokio::sync::RwLock;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct TaskStatus {
|
struct TaskStatus {
|
||||||
sleeper: RwLock<SkippableSleeper>,
|
sleeper: RwLock<SkippableSleeper>,
|
||||||
is_running: AtomicBool,
|
is_running: AtomicBool,
|
||||||
@@ -40,13 +42,22 @@ impl TaskEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run_blocking(&self) {
|
pub async fn run_blocking(&self) {
|
||||||
|
dbg!("entered");
|
||||||
|
dbg!("chkpt 1");
|
||||||
loop {
|
loop {
|
||||||
self.status.sleeper.read().unwrap().start();
|
dbg!("chkpt 2");
|
||||||
|
// deadlock spotted
|
||||||
|
self.status.sleeper.read().await.start().await;
|
||||||
|
dbg!("chkpt 3");
|
||||||
if self.status.expected_running.load(Ordering::SeqCst) {
|
if self.status.expected_running.load(Ordering::SeqCst) {
|
||||||
|
dbg!("chkpt 4");
|
||||||
self.func.call().await;
|
self.func.call().await;
|
||||||
|
dbg!("chkpt 5");
|
||||||
if let Some(next_execution) = self.func.get_next_execution() {
|
if let Some(next_execution) = self.func.get_next_execution() {
|
||||||
*(self.status.sleeper.write().unwrap()) = next_execution.into();
|
dbg!("chkpt 6a");
|
||||||
|
*(self.status.sleeper.write().await) = next_execution.into();
|
||||||
} else {
|
} else {
|
||||||
|
dbg!("chkpt 6b");
|
||||||
self.status.is_running.store(false, Ordering::SeqCst);
|
self.status.is_running.store(false, Ordering::SeqCst);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -56,10 +67,10 @@ impl TaskEntity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn terminate(&self) {
|
pub async fn terminate(&self) {
|
||||||
self.status.expected_running.store(false, Ordering::SeqCst);
|
self.status.expected_running.store(false, Ordering::SeqCst);
|
||||||
self.status.is_running.store(false, Ordering::SeqCst);
|
self.status.is_running.store(false, Ordering::SeqCst);
|
||||||
self.status.sleeper.read().unwrap().skip();
|
self.status.sleeper.read().await.skip();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,8 +87,8 @@ impl Runner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_all(&self) -> Result<(), SchedulerError> {
|
pub async fn run_all(&self) -> Result<(), SchedulerError> {
|
||||||
let list = &self.task_list.read()?;
|
let list = &self.task_list.read().await;
|
||||||
self.rt.block_on(async {
|
self.rt.block_on(async {
|
||||||
for (_, entity) in list.iter() {
|
for (_, entity) in list.iter() {
|
||||||
self.run_entity(entity.clone()).await
|
self.run_entity(entity.clone()).await
|
||||||
@@ -86,8 +97,8 @@ impl Runner {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_id(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
pub async fn run_id(&self, id: &Uuid) -> Result<(), SchedulerError> {
|
||||||
if let Some(entity) = self.task_list.read()?.get(id).cloned() {
|
if let Some(entity) = self.task_list.read().await.get(id).cloned() {
|
||||||
self.rt.block_on(async { self.run_entity(entity).await });
|
self.rt.block_on(async { self.run_entity(entity).await });
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
@@ -100,60 +111,67 @@ impl Runner {
|
|||||||
&& entity.status.expected_running.load(Ordering::SeqCst)
|
&& entity.status.expected_running.load(Ordering::SeqCst)
|
||||||
{
|
{
|
||||||
entity.status.is_running.store(true, Ordering::SeqCst);
|
entity.status.is_running.store(true, Ordering::SeqCst);
|
||||||
tokio::spawn({
|
let fut = {
|
||||||
let entity_clone = entity.clone();
|
let entity_clone = entity.clone();
|
||||||
async move { entity_clone.run_blocking().await }
|
async move { entity_clone.run_blocking().await }
|
||||||
})
|
};
|
||||||
.await
|
dbg!("spawn");
|
||||||
.expect("tokio runtime error");
|
tokio::spawn(fut);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_with_id(
|
pub async fn add_with_id(
|
||||||
&self,
|
&self,
|
||||||
task: Arc<Box<dyn CronTask>>,
|
task: Arc<Box<dyn CronTask>>,
|
||||||
id: &Uuid,
|
id: &Uuid,
|
||||||
) -> Result<(), SchedulerError> {
|
) -> Result<(), SchedulerError> {
|
||||||
self.task_list
|
self.task_list
|
||||||
.write()?
|
.write()
|
||||||
|
.await
|
||||||
.insert(*id, TaskEntity::new(task).into());
|
.insert(*id, TaskEntity::new(task).into());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(&self, task: Arc<Box<dyn CronTask>>) -> Result<Uuid, SchedulerError> {
|
pub async fn add(&self, task: Arc<Box<dyn CronTask>>) -> Result<Uuid, SchedulerError> {
|
||||||
let id = Uuid::new_v4();
|
let id = Uuid::new_v4();
|
||||||
self.add_with_id(task, &id)?;
|
self.add_with_id(task, &id).await?;
|
||||||
Ok(id)
|
Ok(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_list_with_id(
|
pub async fn add_list_with_id(
|
||||||
&self,
|
&self,
|
||||||
tasks: Vec<(&Uuid, Arc<Box<dyn CronTask>>)>,
|
tasks: Vec<(&Uuid, Arc<Box<dyn CronTask>>)>,
|
||||||
) -> Result<(), SchedulerError> {
|
) -> Result<(), SchedulerError> {
|
||||||
tasks
|
let fut = tasks
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.try_for_each(|(id, task)| self.add_with_id(task, id))
|
.map(async |(id, entity)| self.add_with_id(entity, id).await)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
future::join_all(fut).await.into_iter().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_list(
|
pub async fn add_list(
|
||||||
&self,
|
&self,
|
||||||
tasks: Vec<Arc<Box<dyn CronTask>>>,
|
tasks: Vec<Arc<Box<dyn CronTask>>>,
|
||||||
) -> Result<Vec<Uuid>, SchedulerError> {
|
) -> Result<Vec<Uuid>, SchedulerError> {
|
||||||
tasks
|
let fut = tasks
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|task| {
|
.map(async |task| {
|
||||||
let id = Uuid::new_v4();
|
let id = Uuid::new_v4();
|
||||||
self.add_with_id(task, &id).map(move |_| id)
|
self.add_with_id(task, &id).await.map(move |_| id)
|
||||||
})
|
})
|
||||||
.collect::<Result<Vec<_>, _>>()
|
.collect::<Vec<_>>();
|
||||||
|
future::join_all(fut).await.into_iter().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn terminate_remove(&self, id: Uuid) -> Result<Arc<Box<dyn CronTask>>, SchedulerError> {
|
pub async fn terminate_remove(
|
||||||
let mut list = self.task_list.write()?;
|
&self,
|
||||||
|
id: Uuid,
|
||||||
|
) -> Result<Arc<Box<dyn CronTask>>, SchedulerError> {
|
||||||
|
let mut list = self.task_list.write().await;
|
||||||
match list.get(&id) {
|
match list.get(&id) {
|
||||||
None => Err(IdNotFound),
|
None => Err(IdNotFound),
|
||||||
Some(task) => {
|
Some(task) => {
|
||||||
task.terminate();
|
task.terminate().await;
|
||||||
let func = list.remove(&id).unwrap().func.clone();
|
let func = list.remove(&id).unwrap().func.clone();
|
||||||
Ok(func.clone())
|
Ok(func.clone())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,80 +1,81 @@
|
|||||||
use std::{
|
|
||||||
sync::{Arc, Condvar, Mutex},
|
|
||||||
thread,
|
|
||||||
time::Duration,
|
|
||||||
};
|
|
||||||
|
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
use std::{sync::Arc, time::Duration};
|
||||||
|
use tokio::sync::Notify;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct SkippableSleeper {
|
pub struct SkippableSleeper {
|
||||||
pub duration: Duration,
|
duration: Duration,
|
||||||
state: Arc<(Mutex<bool>, Condvar)>,
|
skipped: Arc<AtomicBool>,
|
||||||
|
notify: Arc<Notify>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SkippableSleeper {
|
impl SkippableSleeper {
|
||||||
pub fn new(duration: Duration) -> Self {
|
pub fn new(duration: Duration) -> Self {
|
||||||
SkippableSleeper {
|
Self {
|
||||||
duration,
|
duration,
|
||||||
state: Arc::new((Mutex::new(false), Condvar::new())),
|
skipped: Arc::new(AtomicBool::new(false)),
|
||||||
|
notify: Arc::new(Notify::new()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blocks until the duration has passed or skip() is called
|
pub async fn start(&self) {
|
||||||
pub fn start(&self) {
|
if self.skipped.load(Ordering::Acquire) {
|
||||||
let (lock, cvar) = &*self.state;
|
return;
|
||||||
let mut received = lock.lock().unwrap();
|
}
|
||||||
let state_clone = Arc::clone(&self.state);
|
|
||||||
let duration_clone = self.duration;
|
let sleep = tokio::time::sleep(self.duration);
|
||||||
thread::spawn(move || {
|
let notify = self.notify.notified();
|
||||||
thread::sleep(duration_clone);
|
|
||||||
let (lock, cvar) = &*state_clone;
|
tokio::select! {
|
||||||
let mut received = lock.lock().unwrap();
|
_ = sleep => {}
|
||||||
*received = true;
|
_ = notify => {
|
||||||
cvar.notify_one();
|
self.skipped.store(true, Ordering::Release);
|
||||||
});
|
}
|
||||||
while !*received {
|
|
||||||
received = cvar.wait(received).unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip the blocking delay
|
|
||||||
pub fn skip(&self) {
|
pub fn skip(&self) {
|
||||||
let (lock, cvar) = &*self.state;
|
self.notify.notify_waiters();
|
||||||
let mut received = lock.lock().unwrap();
|
|
||||||
*received = true;
|
|
||||||
cvar.notify_one();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<DateTime<Utc>> for SkippableSleeper {
|
impl From<DateTime<Utc>> for SkippableSleeper {
|
||||||
fn from(value: DateTime<Utc>) -> Self {
|
fn from(value: DateTime<Utc>) -> Self {
|
||||||
let from_utc_timestamp = value.timestamp() as u64;
|
let now = Utc::now();
|
||||||
let curr_utc_timestamp = Utc::now().timestamp() as u64;
|
let duration = value.signed_duration_since(now);
|
||||||
SkippableSleeper::new(Duration::from_secs(from_utc_timestamp - curr_utc_timestamp))
|
let duration = duration.to_std().unwrap_or(Duration::ZERO);
|
||||||
|
Self::new(duration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod tests {
|
||||||
use crate::scheduler::sleeper::SkippableSleeper;
|
use super::*;
|
||||||
use std::thread;
|
use tokio::time::{sleep, Duration};
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn usage() {
|
async fn skip_sleeper() {
|
||||||
let sleeper = SkippableSleeper::new(Duration::new(10, 0));
|
let sleeper = SkippableSleeper::new(Duration::from_secs(10));
|
||||||
thread::spawn({
|
let sleeper_clone = sleeper.clone();
|
||||||
let sleeper = sleeper.clone();
|
|
||||||
move || {
|
let handle = tokio::spawn(async move {
|
||||||
println!("Sleeping...");
|
println!("Sleeping...");
|
||||||
sleeper.start();
|
sleeper_clone.start().await;
|
||||||
println!("Woke up!");
|
println!("Woke up!");
|
||||||
}
|
|
||||||
});
|
});
|
||||||
println!("Start Sleeping...");
|
|
||||||
thread::sleep(Duration::from_secs(4));
|
println!("Start waiting...");
|
||||||
|
sleep(Duration::from_secs(4)).await;
|
||||||
println!("Skipping sleep...");
|
println!("Skipping sleep...");
|
||||||
sleeper.skip();
|
sleeper.skip();
|
||||||
|
|
||||||
|
handle.await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn normal_sleeper() {
|
||||||
|
let sleeper = SkippableSleeper::new(Duration::from_secs(4));
|
||||||
|
sleeper.start().await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,11 +3,12 @@
|
|||||||
/// https://github.com/rust-lang/git2-rs/commit/f3b87baed1e33d6c2d94fe1fa6aa6503a071d837
|
/// https://github.com/rust-lang/git2-rs/commit/f3b87baed1e33d6c2d94fe1fa6aa6503a071d837
|
||||||
///
|
///
|
||||||
use git2::{
|
use git2::{
|
||||||
AnnotatedCommit, AutotagOption, Delta, FetchOptions, Reference, Remote, Repository, build,
|
build, AnnotatedCommit, AutotagOption, Delta, FetchOptions, Reference, Remote, Repository,
|
||||||
};
|
};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use tracing::{error, warn};
|
use tracing::{error, warn};
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub struct FileDelta {
|
pub struct FileDelta {
|
||||||
pub old_path: Option<PathBuf>,
|
pub old_path: Option<PathBuf>,
|
||||||
@@ -15,7 +16,6 @@ pub struct FileDelta {
|
|||||||
pub status: Delta,
|
pub status: Delta,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn do_fetch<'a>(
|
fn do_fetch<'a>(
|
||||||
repo: &'a Repository,
|
repo: &'a Repository,
|
||||||
refs: &[&str],
|
refs: &[&str],
|
||||||
@@ -30,7 +30,6 @@ fn do_fetch<'a>(
|
|||||||
repo.reference_to_annotated_commit(&fetch_head)
|
repo.reference_to_annotated_commit(&fetch_head)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn fast_forward(
|
fn fast_forward(
|
||||||
repo: &Repository,
|
repo: &Repository,
|
||||||
lb: &mut Reference,
|
lb: &mut Reference,
|
||||||
@@ -48,7 +47,6 @@ fn fast_forward(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn normal_merge(
|
fn normal_merge(
|
||||||
repo: &Repository,
|
repo: &Repository,
|
||||||
local: &AnnotatedCommit,
|
local: &AnnotatedCommit,
|
||||||
@@ -85,7 +83,6 @@ fn normal_merge(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn do_merge<'a>(
|
fn do_merge<'a>(
|
||||||
repo: &'a Repository,
|
repo: &'a Repository,
|
||||||
remote_branch: &str,
|
remote_branch: &str,
|
||||||
@@ -122,7 +119,6 @@ fn do_merge<'a>(
|
|||||||
calculate_diff(repo, &head_commit, &fetch_commit)
|
calculate_diff(repo, &head_commit, &fetch_commit)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn calculate_diff(
|
fn calculate_diff(
|
||||||
repo: &Repository,
|
repo: &Repository,
|
||||||
local: &AnnotatedCommit,
|
local: &AnnotatedCommit,
|
||||||
@@ -153,7 +149,6 @@ fn calculate_diff(
|
|||||||
Ok(updated_files.into_iter().collect())
|
Ok(updated_files.into_iter().collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn sync(
|
pub fn sync(
|
||||||
remote_name: &str,
|
remote_name: &str,
|
||||||
remote_branch: &str,
|
remote_branch: &str,
|
||||||
|
|||||||
Reference in new issue
Block a user