Skip to content

Commit 1dfc81c

Browse files
Encode upload loop into S3 migration command
This loop will halt when all files are uploaded to S3, avoiding needing to write something in bash / externally.
1 parent 16e6017 commit 1dfc81c

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/bin/cratesfyi.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,16 @@ pub fn main() {
242242
db::update_search_index(&conn).expect("Failed to update search index");
243243
} else if let Some(_) = matches.subcommand_matches("move-to-s3") {
244244
let conn = db::connect_db().unwrap();
245-
db::file::move_to_s3(&conn, 5_000).expect("Failed to update search index");
245+
let mut count = 1;
246+
let mut total = 0;
247+
while count != 0 {
248+
count = db::file::move_to_s3(&conn, 5_000).expect("Failed to upload batch to S3");
249+
total += count;
250+
eprintln!(
251+
"moved {} rows to s3 in this batch, total moved so far: {}",
252+
count, total
253+
);
254+
}
246255
}
247256
} else if let Some(matches) = matches.subcommand_matches("start-web-server") {
248257
start_web_server(Some(matches.value_of("SOCKET_ADDR").unwrap_or("0.0.0.0:3000")));

src/db/file.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,14 @@ fn file_list_to_json(file_list: Vec<(String, PathBuf)>) -> Result<Json> {
224224
Ok(file_list_json.to_json())
225225
}
226226

227-
pub fn move_to_s3(conn: &Connection, n: usize) -> Result<()> {
227+
pub fn move_to_s3(conn: &Connection, n: usize) -> Result<usize> {
228228
let trans = try!(conn.transaction());
229229
let client = s3_client().expect("configured s3");
230230

231231
let rows = try!(trans.query(
232232
&format!("SELECT path, mime, content FROM files WHERE content != E'in-s3' LIMIT {}", n),
233233
&[]));
234+
let count = rows.len();
234235

235236
let mut rt = ::tokio::runtime::current_thread::Runtime::new().unwrap();
236237
let mut futures = Vec::new();
@@ -268,7 +269,7 @@ pub fn move_to_s3(conn: &Connection, n: usize) -> Result<()> {
268269

269270
try!(trans.commit());
270271

271-
Ok(())
272+
Ok(count)
272273
}
273274

274275
#[cfg(test)]

0 commit comments

Comments
 (0)