Skip to content

Commit 635e91e

Browse files
Merge pull request #377 from Mark-Simulacrum/s3-migrate
Migrate files database to S3
2 parents 5727d15 + 1dfc81c commit 635e91e

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ schemamama_postgres = "0.2"
3434
rusoto_s3 = "0.40"
3535
rusoto_core = "0.40"
3636
rusoto_credential = "0.40"
37-
37+
futures = "0.1"
38+
tokio = "0.1"
3839

3940
# iron dependencies
4041
iron = "0.5"

src/bin/cratesfyi.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ pub fn main() {
109109
.subcommand(SubCommand::with_name("daemon").about("Starts cratesfyi daemon"))
110110
.subcommand(SubCommand::with_name("database")
111111
.about("Database operations")
112+
.subcommand(SubCommand::with_name("move-to-s3"))
112113
.subcommand(SubCommand::with_name("migrate")
113114
.about("Run database migrations")
114115
.arg(Arg::with_name("VERSION")))
@@ -239,6 +240,18 @@ pub fn main() {
239240
} else if let Some(_) = matches.subcommand_matches("update-search-index") {
240241
let conn = db::connect_db().unwrap();
241242
db::update_search_index(&conn).expect("Failed to update search index");
243+
} else if let Some(_) = matches.subcommand_matches("move-to-s3") {
244+
let conn = db::connect_db().unwrap();
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+
}
242255
}
243256
} else if let Some(matches) = matches.subcommand_matches("start-web-server") {
244257
start_web_server(Some(matches.value_of("SOCKET_ADDR").unwrap_or("0.0.0.0:3000")));

src/db/file.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
160160

161161
let content: Option<Vec<u8>> = if let Some(client) = &client {
162162
let s3_res = client.put_object(PutObjectRequest {
163-
acl: Some("public-read".into()),
164163
bucket: "rust-docs-rs".into(),
165164
key: bucket_path.clone(),
166165
body: Some(content.clone().into()),
@@ -225,7 +224,53 @@ fn file_list_to_json(file_list: Vec<(String, PathBuf)>) -> Result<Json> {
225224
Ok(file_list_json.to_json())
226225
}
227226

227+
pub fn move_to_s3(conn: &Connection, n: usize) -> Result<usize> {
228+
let trans = try!(conn.transaction());
229+
let client = s3_client().expect("configured s3");
230+
231+
let rows = try!(trans.query(
232+
&format!("SELECT path, mime, content FROM files WHERE content != E'in-s3' LIMIT {}", n),
233+
&[]));
234+
let count = rows.len();
235+
236+
let mut rt = ::tokio::runtime::current_thread::Runtime::new().unwrap();
237+
let mut futures = Vec::new();
238+
for row in &rows {
239+
let path: String = row.get(0);
240+
let mime: String = row.get(1);
241+
let content: Vec<u8> = row.get(2);
242+
let path_1 = path.clone();
243+
futures.push(client.put_object(PutObjectRequest {
244+
bucket: "rust-docs-rs".into(),
245+
key: path.clone(),
246+
body: Some(content.into()),
247+
content_type: Some(mime),
248+
..Default::default()
249+
}).map(move |_| {
250+
path_1
251+
}).map_err(move |e| {
252+
panic!("failed to upload to {}: {:?}", path, e)
253+
}));
254+
}
228255

256+
use ::futures::future::Future;
257+
match rt.block_on(::futures::future::join_all(futures)) {
258+
Ok(paths) => {
259+
let statement = trans.prepare("UPDATE files SET content = E'in-s3' WHERE path = $1")
260+
.unwrap();
261+
for path in paths {
262+
statement.execute(&[&path]).unwrap();
263+
}
264+
}
265+
Err(e) => {
266+
panic!("results err: {:?}", e);
267+
}
268+
}
269+
270+
try!(trans.commit());
271+
272+
Ok(count)
273+
}
229274

230275
#[cfg(test)]
231276
mod test {

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ extern crate schemamama_postgres;
3333
extern crate rusoto_s3;
3434
extern crate rusoto_core;
3535
extern crate rusoto_credential;
36+
extern crate futures;
37+
extern crate tokio;
3638

3739
pub use self::docbuilder::DocBuilder;
3840
pub use self::docbuilder::ChrootBuilderResult;

0 commit comments

Comments
 (0)