Update push

Save all changed cookies file every 1 min
This commit is contained in:
2023-11-05 13:46:07 +00:00
committed by GitHub
parent 2fb310d897
commit ece32cf9e2
5 changed files with 57 additions and 0 deletions

View File

@@ -309,16 +309,27 @@ impl CookieJarLine {
}
}
fn cal_md5(cookies: &Vec<CookieJarLine>) -> String {
let mut s = String::new();
for c in cookies.iter() {
s += c.to_netscape_str().as_str();
}
let re = md5::compute(s.as_bytes());
format!("{:x}", re)
}
#[derive(Clone, Debug)]
/// Cookies Jar
pub struct CookieJar {
cookies: Vec<CookieJarLine>,
md5: String,
}
impl CookieJar {
pub fn new() -> Self {
Self {
cookies: Vec::new(),
md5: String::from("d41d8cd98f00b204e9800998ecf8427e"),
}
}
@@ -414,9 +425,14 @@ impl CookieJar {
self.add(c);
}
self.check_expired();
self.md5 = cal_md5(&self.cookies);
true
}
pub fn is_changed(&self) -> bool {
self.md5 != cal_md5(&self.cookies)
}
pub fn save<P: AsRef<Path> + ?Sized>(&mut self, file_name: &P) -> bool {
let p = file_name.as_ref();
self.check_expired();
@@ -440,6 +456,7 @@ impl CookieJar {
return false;
}
}
self.md5 = cal_md5(&self.cookies);
true
}
@@ -485,6 +502,24 @@ impl CookieJarManager {
}
}
pub fn save(&self) {
let jars = self.jars.get_ref();
for (path, (jar, _)) in jars.iter() {
if jar.get_ref().is_changed() {
log::debug!("Cookies file changed: {}", path.display());
if !jar.get_mut().save(path) {
log::warn!(
"{} {}",
gettext("Warning: Failed to save cookies file:"),
path.display()
);
} else {
log::debug!("Cookies file saved: {}", path.display());
}
}
}
}
pub fn get_cookie_jar<P: AsRef<Path> + ?Sized>(
&self,
path: &P,
@@ -535,6 +570,10 @@ lazy_static! {
static ref MANAGER: CookieJarManager = CookieJarManager::new();
}
pub fn save_all_cookies() {
MANAGER.save();
}
#[derive(Clone, Debug)]
/// A cookie jar that make sure there are one cookie interface for a cookies file
pub struct ManagedCookieJar {

View File

@@ -54,6 +54,10 @@ pub enum PushTaskPixivAction {
},
}
fn default_pixiv_max_len() -> usize {
100
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PushTaskPixivConfig {
@@ -66,6 +70,9 @@ pub struct PushTaskPixivConfig {
pub use_app_api: Option<bool>,
/// Use data from webpage first.
pub use_webpage: Option<bool>,
#[serde(default = "default_pixiv_max_len")]
/// Max length of cached pushed artworks list
pub max_len: usize,
}
#[derive(Debug, Serialize, Deserialize)]

View File

@@ -102,6 +102,10 @@ impl<'a> RunContext<'a> {
self.web_run().await?;
}
if self.send_mode.is_none() {
let len = self.pushed.get_ref().len();
if len > self.config.max_len {
self.pushed.get_mut().drain(0..(len - self.config.max_len));
}
let data = serde_json::to_string(self.pushed.get_ref().as_slice())?;
self.ctx.db.set_push_task_data(self.task.id, &data).await?;
self.ctx

View File

@@ -548,6 +548,7 @@ impl<'a> RunContext<'a> {
if cfg.add_link_to_image {
text.push_str(&format!("]({})", url));
}
text.push_str(" \n");
}
}
if cfg.author_locations.contains(&AuthorLocation::Top) {

View File

@@ -12,6 +12,12 @@ pub async fn start_timer(ctx: Arc<ServerContext>) {
loop {
interval.tick().await;
tasks.add_task(revoke_expired_tokens(ctx.clone())).await;
tasks
.add_task(async {
crate::cookies::save_all_cookies();
Ok(())
})
.await;
tasks.join().await;
for task in tasks.take_finished_tasks() {
let re = task.await;