Skip to content

Commit 7e0943d

Browse files
committed
feat: server cool startup would load old version files to caching
1 parent 55bf666 commit 7e0943d

File tree

2 files changed

+57
-25
lines changed

2 files changed

+57
-25
lines changed

server/src/domain_storage.rs

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use serde_repr::{Deserialize_repr, Serialize_repr};
99
use std::fs;
1010
use std::fs::{File, OpenOptions};
1111
use std::io::{Read, Write};
12+
use std::ops::RangeInclusive;
1213
use std::path::{Path, PathBuf};
1314
use std::sync::Arc;
1415
use tracing::{debug, info};
@@ -75,25 +76,36 @@ impl DomainStorage {
7576
uploading_status
7677
.insert(domain_with_sub_path.clone(), uploading_version);
7778
}
78-
if let Some(max_version) = max_version {
79-
let path_buf = sub_dir.join(max_version.to_string());
80-
let data = cache.cache_dir(
81-
domain_dir_name,
82-
Some(sub_path.as_str()),
83-
max_version,
84-
&path_buf,
85-
)?;
86-
cache.update(
87-
domain_dir_name.to_string(),
88-
Some(sub_path.as_str()),
89-
max_version,
90-
data,
91-
);
79+
80+
if let Some(version) = max_version {
81+
for version in Self::get_init_version(version) {
82+
let path_buf = sub_dir.join(version.to_string());
83+
if sub_dir
84+
.join(version.to_string())
85+
.join(UPLOADING_FILE_NAME)
86+
.exists()
87+
{
88+
continue;
89+
}
90+
let data = cache.cache_dir(
91+
domain_dir_name,
92+
Some(sub_path.as_str()),
93+
version,
94+
&path_buf,
95+
)?;
96+
cache.update(
97+
domain_dir_name.to_string(),
98+
Some(sub_path.as_str()),
99+
version,
100+
data,
101+
);
102+
}
103+
let path_buf = sub_dir.join(version.to_string());
92104
match domain_version.get_mut(domain_dir_name) {
93105
Some(mut domain_meta) => {
94106
match domain_meta.value_mut() {
95107
DomainMeta::MultipleWeb(ref mut map) => {
96-
map.insert(sub_path, (path_buf, max_version));
108+
map.insert(sub_path.clone(), (path_buf, version));
97109
}
98110
DomainMeta::OneWeb(..) => {
99111
panic!("init failure, {sub_dir:?} should be multiple web");
@@ -102,7 +114,7 @@ impl DomainStorage {
102114
}
103115
None => {
104116
let map = DashMap::new();
105-
map.insert(sub_path, (path_buf, max_version));
117+
map.insert(sub_path.clone(), (path_buf, version));
106118
domain_version.insert(
107119
domain_dir_name.to_string(),
108120
DomainMeta::MultipleWeb(map),
@@ -117,16 +129,21 @@ impl DomainStorage {
117129
if let Some(uploading_version) = uploading_version {
118130
uploading_status.insert(domain_dir_name.to_string(), uploading_version);
119131
}
120-
if let Some(max_version) = max_version {
132+
if let Some(version) = max_version {
133+
for version in Self::get_init_version(version) {
134+
let path_buf = path_prefix_buf
135+
.join(domain_dir_name)
136+
.join(version.to_string());
137+
let data =
138+
cache.cache_dir(domain_dir_name, None, version, &path_buf)?;
139+
cache.update(domain_dir_name.to_string(), None, version, data);
140+
}
121141
let path_buf = path_prefix_buf
122142
.join(domain_dir_name)
123-
.join(max_version.to_string());
124-
let data =
125-
cache.cache_dir(domain_dir_name, None, max_version, &path_buf)?;
126-
cache.update(domain_dir_name.to_string(), None, max_version, data);
143+
.join(version.to_string());
127144
domain_version.insert(
128145
domain_dir_name.to_string(),
129-
DomainMeta::OneWeb(path_buf, max_version),
146+
DomainMeta::OneWeb(path_buf, version),
130147
);
131148
}
132149
}
@@ -143,6 +160,14 @@ impl DomainStorage {
143160
}
144161
}
145162

163+
fn get_init_version(version: u32) -> RangeInclusive<u32> {
164+
if version > 2 {
165+
version - 2..=version
166+
} else {
167+
1..=version
168+
}
169+
}
170+
146171
fn get_multiple_path_data<'a>(
147172
domain_dir: &PathBuf,
148173
domain_dir_name: &'a str,
@@ -697,10 +722,10 @@ pub struct UploadDomainPosition {
697722

698723
#[cfg(test)]
699724
mod test {
700-
use crate::domain_storage::URI_REGEX_STR;
725+
use crate::domain_storage::{DomainStorage, URI_REGEX_STR};
701726
use hyper::Uri;
702727
use regex::Regex;
703-
use std::fs::File;
728+
use std::ops::RangeInclusive;
704729
use std::path::PathBuf;
705730
use std::str::FromStr;
706731

@@ -750,6 +775,14 @@ mod test {
750775
let z: Vec<&str> = z.collect();
751776
println!("{}", z.len());
752777
}
778+
779+
#[test]
780+
fn get_init_version_test() {
781+
assert_eq!(DomainStorage::get_init_version(3), 1..=3);
782+
assert_eq!(DomainStorage::get_init_version(2), 1..=2);
783+
assert_eq!(DomainStorage::get_init_version(4), 2..=4);
784+
assert!(RangeInclusive::new(1, 0).is_empty());
785+
}
753786
// #[test]
754787
// fn test_path() {
755788
// let path = PathBuf::from("/");

tests/tests/starter.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ async fn evoke_cache_when_serving_new_index() {
6565
assert_files_no_exists(request_prefix, vec!["1.html"]).await;
6666
}
6767

68-
// TODO: to fix it
6968
// This must run after evoke_cache_when_serving_new_index
7069
#[tokio::test(flavor = "multi_thread", worker_threads = 3)]
7170
async fn cool_start_server_and_serving_files() {

0 commit comments

Comments
 (0)