From 27bc493884eeae1e6089f9f828d625ec3e9c7959 Mon Sep 17 00:00:00 2001 From: J M <2364004+Blu-J@users.noreply.github.com> Date: Thu, 16 Jun 2022 11:58:55 -0600 Subject: [PATCH] Feat: Make the js check for health (#1543) * Feat: Make the js check for health * chore: Add in the migration types * feat: type up the migration --- backend/src/procedure/js_scripts.rs | 6 +- libs/artifacts/types.d.ts | 90 +++++++++++++++++------------ libs/js_engine/src/lib.rs | 30 +++++++--- 3 files changed, 81 insertions(+), 45 deletions(-) diff --git a/backend/src/procedure/js_scripts.rs b/backend/src/procedure/js_scripts.rs index 2605053ae..cb8f801ab 100644 --- a/backend/src/procedure/js_scripts.rs +++ b/backend/src/procedure/js_scripts.rs @@ -22,6 +22,7 @@ pub use js_engine::JsError; enum ErrorValue { Error(String), + ErrorCode((i32, String)), Result(serde_json::Value), } @@ -81,7 +82,7 @@ impl JsProcedure { Ok(output) } .await - .map_err(|(error, message)| (error as i32, message))) + .map_err(|(error, message)| (error.as_code_num(), message))) } #[instrument(skip(ctx, input))] @@ -115,7 +116,7 @@ impl JsProcedure { Ok(output) } .await - .map_err(|(error, message)| (error as i32, message))) + .map_err(|(error, message)| (error.as_code_num(), message))) } } @@ -124,6 +125,7 @@ fn unwrap_known_error Deserialize<'de>>( ) -> Result { match error_value { ErrorValue::Error(error) => Err((JsError::Javascript, error)), + ErrorValue::ErrorCode((code, message)) => Err((JsError::Code(code), message)), ErrorValue::Result(ref value) => match serde_json::from_value(value.clone()) { Ok(a) => Ok(a), Err(err) => { diff --git a/libs/artifacts/types.d.ts b/libs/artifacts/types.d.ts index 61031f2e2..559e53396 100644 --- a/libs/artifacts/types.d.ts +++ b/libs/artifacts/types.d.ts @@ -12,6 +12,18 @@ export namespace ExpectedExports { export type properties = ( effects: Effects, ) => Promise>; + + export type health = { + /** Should be the health check id */ + [id: string]: ( + effects: Effects, + dateMs: number, + ) => Promise>; + }; + export type migration = ( + effects: Effects, + version: string, + ) => Promise>; } /** Used to reach out from the pure js runtime */ @@ -50,6 +62,10 @@ export type Effects = { is_sandboxed(): boolean; }; +export type MigrationRes = { + configured: boolean; +}; + export type ActionResult = { version: "0"; message: string; @@ -260,52 +276,54 @@ export type ValueSpecList = WithDescription, string[]>> >; export type ValueSpecEnum = { - values: string[], + values: string[]; "value-names": { [key: string]: string }; -} +}; export type SetResult = { /** These are the unix process signals */ signal: - | "SIGTERM" - | "SIGHUP" - | "SIGINT" - | "SIGQUIT" - | "SIGILL" - | "SIGTRAP" - | "SIGABRT" - | "SIGBUS" - | "SIGFPE" - | "SIGKILL" - | "SIGUSR1" - | "SIGSEGV" - | "SIGUSR2" - | "SIGPIPE" - | "SIGALRM" - | "SIGSTKFLT" - | "SIGCHLD" - | "SIGCONT" - | "SIGSTOP" - | "SIGTSTP" - | "SIGTTIN" - | "SIGTTOU" - | "SIGURG" - | "SIGXCPU" - | "SIGXFSZ" - | "SIGVTALRM" - | "SIGPROF" - | "SIGWINCH" - | "SIGIO" - | "SIGPWR" - | "SIGSYS" - | "SIGEMT" - | "SIGINFO"; + | "SIGTERM" + | "SIGHUP" + | "SIGINT" + | "SIGQUIT" + | "SIGILL" + | "SIGTRAP" + | "SIGABRT" + | "SIGBUS" + | "SIGFPE" + | "SIGKILL" + | "SIGUSR1" + | "SIGSEGV" + | "SIGUSR2" + | "SIGPIPE" + | "SIGALRM" + | "SIGSTKFLT" + | "SIGCHLD" + | "SIGCONT" + | "SIGSTOP" + | "SIGTSTP" + | "SIGTTIN" + | "SIGTTOU" + | "SIGURG" + | "SIGXCPU" + | "SIGXFSZ" + | "SIGVTALRM" + | "SIGPROF" + | "SIGWINCH" + | "SIGIO" + | "SIGPWR" + | "SIGSYS" + | "SIGEMT" + | "SIGINFO"; "depends-on": { [packageId: string]: string[]; }; }; -export type KnownError = { error: String }; +export type KnownError = { error: String } | { + "error-code": [number, string] | readonly [number, string]; +}; export type ResultType = KnownError | { result: T }; export type PackagePropertiesV2 = { diff --git a/libs/js_engine/src/lib.rs b/libs/js_engine/src/lib.rs index 01787c432..2213da433 100644 --- a/libs/js_engine/src/lib.rs +++ b/libs/js_engine/src/lib.rs @@ -35,13 +35,29 @@ pub struct JsCode(String); #[derive(Debug, Clone, Copy)] pub enum JsError { - Unknown = 1, - Javascript = 2, - Engine = 3, - BoundryLayerSerDe = 4, - Tokio = 5, - FileSystem = 6, - Timeout = 143, + Unknown, + Javascript, + Engine, + BoundryLayerSerDe, + Tokio, + FileSystem, + Code(i32), + Timeout, +} + +impl JsError { + pub fn as_code_num(&self) -> i32 { + match self { + JsError::Unknown => 1, + JsError::Javascript => 2, + JsError::Engine => 3, + JsError::BoundryLayerSerDe => 4, + JsError::Tokio => 5, + JsError::FileSystem => 6, + JsError::Code(code) => *code, + JsError::Timeout => 143, + } + } } #[cfg(target_arch = "x86_64")]