-
Bijun Li authored06187082
use crate::api_model::RunDownloader;
use crate::api_model::RunImporter;
use crate::api_model::RunIndexer;
use crate::command_line_interface::CLIOptions;
use crate::error::Error;
use crate::error::Result;
use crate::internal_api;
use log::info;
use rusqlite::Connection;
use std::env;
use std::ops::Deref;
use std::process::Command;
use warp::http::status::StatusCode;
pub fn run_downloader(
conn: &Connection,
payload: RunDownloader,
cli_options: &CLIOptions,
) -> Result<()> {
info!("Trying to run downloader on item {}", payload.uid);
let result = internal_api::get_item(conn.deref(), payload.uid)?;
if result.first().is_none() {
return Err(Error {
code: StatusCode::BAD_REQUEST,
msg: format!("Failed to get item {}", payload.uid),
});
};
let mut args: Vec<String> = Vec::new();
args.push("run".to_string());
for arg in docker_arguments(cli_options) {
args.push(arg);
}
args.push(format!(
"--env=POD_SERVICE_PAYLOAD={}",
payload.service_payload
));
args.push("--rm".to_string());
args.push("--name=memri-downloaders_1".to_string());
args.push(format!("--env=RUN_UID={}", payload.uid));
args.push("--volume=download-volume:/usr/src/importers/data".to_string());
args.push("memri-downloaders:latest".to_string());
log::debug!("Starting downloader docker command {:?}", args);
let command = Command::new("docker").args(&args).spawn();
match command {
Ok(_child) => {
log::debug!("Successfully started downloader for {}", payload.uid);
Ok(())
}
Err(err) => {
log::warn!("Failed to run downloader {}", payload.uid);
Err(Error {
code: StatusCode::INTERNAL_SERVER_ERROR,
msg: format!("Failed to run downloader with uid {}, {}", payload.uid, err),
})
}
}
}
pub fn run_importer(
conn: &Connection,
payload: RunImporter,
cli_options: &CLIOptions,
) -> Result<()> {
info!("Trying to run importer on item {}", payload.uid);
let result = internal_api::get_item(conn.deref(), payload.uid)?;
if result.first().is_none() {
return Err(Error {
code: StatusCode::BAD_REQUEST,
msg: format!("Failed to get item {}", payload.uid),
});