rc/api2/admin/datastore/h2upload.rs: implement BackupEnvironment

To pass arbitrary information/state to api methods.
This commit is contained in:
Dietmar Maurer 2019-05-08 11:26:54 +02:00
parent f757b30efc
commit 58c8d7d91f
2 changed files with 64 additions and 10 deletions

View File

@ -3,6 +3,7 @@ use lazy_static::lazy_static;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use std::any::Any;
use futures::*; use futures::*;
use hyper::header::{HeaderValue, UPGRADE}; use hyper::header::{HeaderValue, UPGRADE};
@ -15,7 +16,7 @@ use crate::tools;
use crate::api_schema::router::*; use crate::api_schema::router::*;
use crate::api_schema::*; use crate::api_schema::*;
use crate::server::formatter::*; use crate::server::formatter::*;
use crate::server::{WorkerTask, RestEnvironment}; use crate::server::WorkerTask;
pub fn api_method_upgrade_h2upload() -> ApiAsyncMethod { pub fn api_method_upgrade_h2upload() -> ApiAsyncMethod {
ApiAsyncMethod::new( ApiAsyncMethod::new(
@ -29,14 +30,62 @@ lazy_static!{
static ref BACKUP_ROUTER: Router = backup_api(); static ref BACKUP_ROUTER: Router = backup_api();
} }
/// `RpcEnvironmet` implementation for backup service
#[derive(Clone)]
pub struct BackupEnvironment {
env_type: RpcEnvironmentType,
result_attributes: HashMap<String, Value>,
user: String,
worker: Arc<WorkerTask>,
}
impl BackupEnvironment {
pub fn new(env_type: RpcEnvironmentType, user: String, worker: Arc<WorkerTask>) -> Self {
Self {
result_attributes: HashMap::new(),
env_type,
user,
worker,
}
}
pub fn log<S: AsRef<str>>(&self, msg: S) {
self.worker.log(msg);
}
}
impl RpcEnvironment for BackupEnvironment {
fn set_result_attrib(&mut self, name: &str, value: Value) {
self.result_attributes.insert(name.into(), value);
}
fn get_result_attrib(&self, name: &str) -> Option<&Value> {
self.result_attributes.get(name)
}
fn env_type(&self) -> RpcEnvironmentType {
self.env_type
}
fn set_user(&mut self, user: Option<String>) {
panic!("unable to change user");
}
fn get_user(&self) -> Option<String> {
Some(self.user.clone())
}
}
pub struct BackupService { pub struct BackupService {
rpcenv: RestEnvironment, rpcenv: BackupEnvironment,
worker: Arc<WorkerTask>, worker: Arc<WorkerTask>,
} }
impl BackupService { impl BackupService {
fn new(rpcenv: RestEnvironment, worker: Arc<WorkerTask>) -> Self { fn new(rpcenv: BackupEnvironment, worker: Arc<WorkerTask>) -> Self {
Self { rpcenv, worker } Self { rpcenv, worker }
} }
@ -155,12 +204,12 @@ fn upgrade_h2upload(
let worker_id = String::from("test2workerid"); let worker_id = String::from("test2workerid");
let username = rpcenv.get_user().unwrap();
let env_type = rpcenv.env_type();
let mut rpcenv1 = RestEnvironment::new(rpcenv.env_type()); WorkerTask::spawn("test2_download", Some(worker_id), &username.clone(), true, move |worker| {
rpcenv1.set_user(rpcenv.get_user()); let backup_env = BackupEnvironment::new(env_type, username.clone(), worker.clone());
let service = BackupService::new(backup_env, worker.clone());
WorkerTask::spawn("test2_download", Some(worker_id), &rpcenv.get_user().unwrap(), true, move |worker| {
let service = BackupService::new(rpcenv1, worker.clone());
let abort_future = worker.abort_future(); let abort_future = worker.abort_future();
@ -222,9 +271,15 @@ fn backup_api() -> Router {
fn test1_get ( fn test1_get (
_param: Value, _param: Value,
_info: &ApiMethod, _info: &ApiMethod,
_rpcenv: &mut RpcEnvironment, rpcenv: &mut RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
println!("TYPEID {:?}", (*rpcenv).type_id());
let env = rpcenv.as_any().downcast_ref::<BackupEnvironment>().unwrap();
env.log("Inside test1_get()");
Ok(Value::Null) Ok(Value::Null)
} }

View File

@ -4,7 +4,6 @@ use std::collections::HashMap;
use serde_json::Value; use serde_json::Value;
/// Encapsulates information about the runtime environment /// Encapsulates information about the runtime environment
#[derive(Clone)]
pub struct RestEnvironment { pub struct RestEnvironment {
env_type: RpcEnvironmentType, env_type: RpcEnvironmentType,
result_attributes: HashMap<String, Value>, result_attributes: HashMap<String, Value>,