|
1 |
| -use cargo_registry::util::{CargoError, CargoResult}; |
2 |
| -use cargo_registry::{db, tasks}; |
3 |
| -use std::env::args; |
4 |
| -use swirl::Job; |
| 1 | +use cargo_registry::util::{human, CargoError, CargoResult}; |
| 2 | +use cargo_registry::{db, env, tasks}; |
| 3 | +use diesel::PgConnection; |
5 | 4 |
|
6 | 5 | fn main() -> CargoResult<()> {
|
7 | 6 | let conn = db::connect_now()?;
|
| 7 | + let mut args = std::env::args().skip(1); |
| 8 | + match &*args.next().unwrap_or_default() { |
| 9 | + "update_downloads" => tasks::update_downloads().enqueue(&conn), |
| 10 | + "dump_db" => { |
| 11 | + let database_url = args.next().unwrap_or_else(|| env("DATABASE_URL")); |
| 12 | + let target_name = args |
| 13 | + .next() |
| 14 | + .unwrap_or_else(|| String::from("db-dump.tar.gz")); |
| 15 | + tasks::dump_db(database_url, target_name).enqueue(&conn) |
| 16 | + } |
| 17 | + other => Err(human(&format!("Unrecognized job type `{}`", other))), |
| 18 | + } |
| 19 | +} |
8 | 20 |
|
9 |
| - match &*args().nth(1).unwrap_or_default() { |
10 |
| - "update_downloads" => tasks::update_downloads() |
11 |
| - .enqueue(&conn) |
12 |
| - .map_err(|e| CargoError::from_std_error(e))?, |
13 |
| - other => panic!("Unrecognized job type `{}`", other), |
14 |
| - }; |
15 |
| - |
16 |
| - Ok(()) |
| 21 | +/// Helper to map the `PerformError` returned by `swirl::Job::enqueue()` to a |
| 22 | +/// `CargoError`. Can be removed once `map_err()` isn't needed any more. |
| 23 | +trait Enqueue: swirl::Job { |
| 24 | + fn enqueue(self, conn: &PgConnection) -> CargoResult<()> { |
| 25 | + <Self as swirl::Job>::enqueue(self, conn).map_err(|e| CargoError::from_std_error(e)) |
| 26 | + } |
17 | 27 | }
|
| 28 | + |
| 29 | +impl<J: swirl::Job> Enqueue for J {} |
0 commit comments