make OsApi manager-based

This commit is contained in:
Aiden McClelland
2023-02-01 18:42:32 -07:00
parent bec307d0e9
commit 550b17552b
5 changed files with 52 additions and 34 deletions

View File

@@ -0,0 +1,17 @@
use helpers::{Callback, OsApi};
use models::PackageId;
use crate::manager::Manager;
use crate::Error;
#[async_trait::async_trait]
impl OsApi for Manager {
async fn get_service_config(
&self,
id: PackageId,
path: &str,
callback: Callback,
) -> Result<serde_json::Value, Error> {
todo!()
}
}

View File

@@ -49,6 +49,7 @@ use crate::volume::Volume;
use crate::Error;
pub mod health;
mod js_api;
mod manager_container;
mod manager_map;
pub mod manager_seed;
@@ -205,8 +206,8 @@ impl Manager {
pub async fn signal(&self, signal: Signal) -> Result<(), Error> {
let rpc_client = self.rpc_client();
let seed = self.seed.clone();
let git = self.gid.clone();
send_signal(rpc_client, seed, git, signal).await
let gid = self.gid.clone();
send_signal(self, gid, signal).await
}
pub fn rpc_client(&self) -> Option<Arc<UnixRpcClient>> {
@@ -884,28 +885,23 @@ async fn get_running_ip(seed: &ManagerSeed, mut runtime: &mut RuntimeOfCommand)
}
}
async fn send_signal(
rpc_client: Option<Arc<UnixRpcClient>>,
seed: Arc<ManagerSeed>,
gid: Arc<Gid>,
signal: Signal,
) -> Result<(), Error> {
async fn send_signal(manager: &Manager, gid: Arc<Gid>, signal: Signal) -> Result<(), Error> {
// stop health checks from committing their results
// shared
// .commit_health_check_results
// .store(false, Ordering::SeqCst);
if let Some(rpc_client) = rpc_client {
if let Some(rpc_client) = manager.rpc_client() {
let main_gid = *gid.main_gid.borrow();
let next_gid = gid.new_gid();
#[cfg(feature = "js_engine")]
if let Err(e) = crate::procedure::js_scripts::JsProcedure::default()
.execute::<_, NoOutput>(
&seed.ctx.datadir,
&seed.manifest.id,
&seed.manifest.version,
&manager.seed.ctx.datadir,
&manager.seed.manifest.id,
&manager.seed.manifest.version,
ProcedureName::Signal,
&seed.manifest.volumes,
&manager.seed.manifest.volumes,
Some(embassy_container_init::SignalGroupParams {
gid: main_gid,
signal: signal as u32,
@@ -913,7 +909,7 @@ async fn send_signal(
None, // TODO
next_gid,
Some(rpc_client),
Arc::new(seed.ctx.clone()),
Arc::new(manager.clone()),
)
.await?
{
@@ -922,10 +918,12 @@ async fn send_signal(
}
} else {
// send signal to container
seed.ctx
manager
.seed
.ctx
.docker
.kill_container(
&seed.container_name,
&manager.seed.container_name,
Some(bollard::container::KillContainerOptions {
signal: signal.to_string(),
}),

View File

@@ -4,7 +4,7 @@ use std::time::Duration;
use color_eyre::eyre::eyre;
use embassy_container_init::{ProcessGroupId, SignalGroup, SignalGroupParams};
use helpers::{Callback, OsApi, UnixRpcClient};
use helpers::{OsApi, UnixRpcClient};
pub use js_engine::JsError;
use js_engine::{JsExecutionEnvironment, PathForVolumeId};
use models::{ErrorKind, VolumeId};
@@ -19,18 +19,6 @@ use crate::util::{GeneralGuard, Version};
use crate::volume::Volumes;
use crate::Error;
#[async_trait::async_trait]
impl OsApi for RpcContext {
async fn get_service_config(
&self,
id: PackageId,
path: &str,
callback: Callback,
) -> Result<serde_json::Value, Error> {
todo!()
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
@@ -57,6 +45,12 @@ impl PathForVolumeId for Volumes {
}
}
struct SandboxOsApi {
_ctx: RpcContext,
}
#[async_trait::async_trait]
impl OsApi for SandboxOsApi {}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct JsProcedure {
@@ -138,7 +132,7 @@ impl JsProcedure {
) -> Result<Result<O, (i32, String)>, Error> {
Ok(async move {
let running_action = JsExecutionEnvironment::load_from_package(
Arc::new(ctx.clone()),
Arc::new(SandboxOsApi { _ctx: ctx.clone() }),
&ctx.datadir,
pkg_id,
pkg_version,

View File

@@ -1,5 +1,4 @@
use std::collections::BTreeSet;
use std::sync::Arc;
use std::time::Duration;
use color_eyre::eyre::eyre;
@@ -96,7 +95,6 @@ impl PackageProcedure {
})?;
let gid;
let rpc_client = man.rpc_client();
let os = Arc::new(ctx.clone());
if matches!(name, ProcedureName::Main) {
gid = man.gid.new_main_gid();
} else {
@@ -114,7 +112,7 @@ impl PackageProcedure {
timeout,
gid,
rpc_client,
os,
man,
)
.await
}

View File

@@ -1,3 +1,4 @@
use color_eyre::eyre::eyre;
use models::Error;
use models::PackageId;
use serde_json::Value;
@@ -6,12 +7,22 @@ pub struct RuntimeDropped;
pub type Callback = Box<dyn Fn(Value) -> Result<(), RuntimeDropped> + Send + Sync + 'static>; // bool indicating if
fn method_not_available() -> Error {
Error::new(
eyre!("method not available"),
models::ErrorKind::InvalidRequest,
)
}
#[async_trait::async_trait]
#[allow(unused_variables)]
pub trait OsApi: Send + Sync + 'static {
async fn get_service_config(
&self,
id: PackageId,
path: &str,
callback: Callback,
) -> Result<Value, Error>;
) -> Result<Value, Error> {
Err(method_not_available())
}
}