From 13ed949dfbdef136afede09b04c5825061ff0fbe Mon Sep 17 00:00:00 2001 From: andreespirela Date: Wed, 2 Aug 2023 10:01:15 -0400 Subject: [PATCH 1/6] Core refactoring part 2 --- crates/base/src/commands.rs | 4 ++-- crates/base/src/rt_worker/worker.rs | 6 +++--- crates/base/src/rt_worker/worker_ctx.rs | 4 ++-- crates/base/src/utils.rs | 8 ++++---- crates/cli/src/main.rs | 4 ++-- scripts/run.sh | 2 +- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/base/src/commands.rs b/crates/base/src/commands.rs index 8bb68b293..f38ce4d24 100644 --- a/crates/base/src/commands.rs +++ b/crates/base/src/commands.rs @@ -6,7 +6,7 @@ pub async fn start_server( ip: &str, port: u16, main_service_path: String, - event_manager_path: Option, + event_worker_path: Option, import_map_path: Option, no_module_cache: bool, callback_tx: Option>, @@ -15,7 +15,7 @@ pub async fn start_server( ip, port, main_service_path, - event_manager_path, + event_worker_path, import_map_path, no_module_cache, callback_tx, diff --git a/crates/base/src/rt_worker/worker.rs b/crates/base/src/rt_worker/worker.rs index 7e3e9557c..a1931d2e8 100644 --- a/crates/base/src/rt_worker/worker.rs +++ b/crates/base/src/rt_worker/worker.rs @@ -1,7 +1,7 @@ use crate::deno_runtime::DenoRuntime; use crate::rt_worker::utils::{get_event_metadata, parse_worker_conf}; use crate::rt_worker::worker_ctx::create_supervisor; -use crate::utils::send_event_if_event_manager_available; +use crate::utils::send_event_if_event_worker_available; use anyhow::{anyhow, bail, Error}; use cpu_timer::get_thread_time; use event_worker::events::{ @@ -130,7 +130,7 @@ impl Worker { match result { Ok(event) => { - send_event_if_event_manager_available( + send_event_if_event_worker_available( events_msg_tx.clone(), event, event_metadata.clone(), @@ -144,7 +144,7 @@ impl Worker { format!("CPU time used: {:?}ms", (end_time - start_time) / 1_000_000); debug!("{}", cpu_time_msg); - send_event_if_event_manager_available( + send_event_if_event_worker_available( events_msg_tx, WorkerEvents::Log(LogEvent { msg: cpu_time_msg, diff --git a/crates/base/src/rt_worker/worker_ctx.rs b/crates/base/src/rt_worker/worker_ctx.rs index 568a6dd61..6b575bdf7 100644 --- a/crates/base/src/rt_worker/worker_ctx.rs +++ b/crates/base/src/rt_worker/worker_ctx.rs @@ -1,5 +1,5 @@ use crate::deno_runtime::DenoRuntime; -use crate::utils::send_event_if_event_manager_available; +use crate::utils::send_event_if_event_worker_available; use crate::utils::units::bytes_to_display; use crate::rt_worker::worker::{Worker, WorkerHandler}; @@ -191,7 +191,7 @@ pub async fn create_worker( .worker_boot_start_time .elapsed() .as_millis(); - send_event_if_event_manager_available( + send_event_if_event_worker_available( worker_struct_ref.events_msg_tx.clone(), WorkerEvents::Boot(BootEvent { boot_time: elapsed as usize, diff --git a/crates/base/src/utils.rs b/crates/base/src/utils.rs index c71fbab95..908cf8813 100644 --- a/crates/base/src/utils.rs +++ b/crates/base/src/utils.rs @@ -3,12 +3,12 @@ use tokio::sync::mpsc; pub mod units; -pub fn send_event_if_event_manager_available( - maybe_event_manager: Option>, +pub fn send_event_if_event_worker_available( + maybe_event_worker: Option>, event: WorkerEvents, metadata: EventMetadata, ) { - if let Some(event_manager) = maybe_event_manager { - let _ = event_manager.send(WorkerEventWithMetadata { event, metadata }); + if let Some(event_worker) = maybe_event_worker { + let _ = event_worker.send(WorkerEventWithMetadata { event, metadata }); } } diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 7280fe0bc..af1c96853 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -38,7 +38,7 @@ fn cli() -> Command { .arg(arg!(--"main-service" "Path to main service directory").default_value("examples/main")) .arg(arg!(--"disable-module-cache" "Disable using module cache").default_value("false").value_parser(FalseyValueParser::new())) .arg(arg!(--"import-map" "Path to import map file")) - .arg(arg!(--"event-manager" "Path to event manager directory")) + .arg(arg!(--"event-worker" "Path to event worker directory")) ) } @@ -85,7 +85,7 @@ fn main() -> Result<(), anyhow::Error> { .cloned() .unwrap(); let event_service_manager_path = - sub_matches.get_one::("event-manager").cloned(); + sub_matches.get_one::("event-worker").cloned(); start_server( ip.as_str(), diff --git a/scripts/run.sh b/scripts/run.sh index e6e5713ad..20fc07129 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -1,3 +1,3 @@ #!/usr/bin/env bash - cargo build && RUST_BACKTRACE=full ./target/debug/edge-runtime "$@" start --main-service ./examples/main --event-manager ./examples/event-manager + cargo build && RUST_BACKTRACE=full ./target/debug/edge-runtime "$@" start --main-service ./examples/main --event-worker ./examples/event-manager From 388e63e4fd80bfd9001a6ffa2eed3a1990985288 Mon Sep 17 00:00:00 2001 From: andreespirela Date: Wed, 2 Aug 2023 10:13:26 -0400 Subject: [PATCH 2/6] Util Import maps --- crates/base/src/deno_runtime.rs | 14 +------------- crates/module_fetcher/src/args/import_map.rs | 14 +------------- crates/module_fetcher/src/util.rs | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 26 deletions(-) diff --git a/crates/base/src/deno_runtime.rs b/crates/base/src/deno_runtime.rs index 5f2faf90f..91f29d80e 100644 --- a/crates/base/src/deno_runtime.rs +++ b/crates/base/src/deno_runtime.rs @@ -21,6 +21,7 @@ use crate::{errors_rt, snapshot}; use event_worker::events::{EventMetadata, WorkerEventWithMetadata}; use event_worker::js_interceptors::sb_events_js_interceptors; use event_worker::sb_user_event_worker; +use module_fetcher::util::print_import_map_diagnostics; use module_loader::DefaultModuleLoader; use sb_core::http_start::sb_core_http; use sb_core::net::sb_core_net; @@ -61,19 +62,6 @@ fn load_import_map(maybe_path: Option) -> Result, Erro } } -fn print_import_map_diagnostics(diagnostics: &[ImportMapDiagnostic]) { - if !diagnostics.is_empty() { - warn!( - "Import map diagnostics:\n{}", - diagnostics - .iter() - .map(|d| format!(" - {d}")) - .collect::>() - .join("\n") - ); - } -} - pub struct DenoRuntimeError(Error); impl PartialEq for DenoRuntimeError { diff --git a/crates/module_fetcher/src/args/import_map.rs b/crates/module_fetcher/src/args/import_map.rs index 490859374..1114aa6f1 100644 --- a/crates/module_fetcher/src/args/import_map.rs +++ b/crates/module_fetcher/src/args/import_map.rs @@ -11,6 +11,7 @@ use log::warn; use crate::file_fetcher::get_source_from_data_url; use crate::file_fetcher::FileFetcher; use crate::permissions::Permissions; +use crate::util::print_import_map_diagnostics; pub async fn resolve_import_map_from_specifier( specifier: &Url, @@ -49,16 +50,3 @@ fn import_map_from_value( print_import_map_diagnostics(&result.diagnostics); Ok(result.import_map) } - -fn print_import_map_diagnostics(diagnostics: &[ImportMapDiagnostic]) { - if !diagnostics.is_empty() { - warn!( - "Import map diagnostics:\n{}", - diagnostics - .iter() - .map(|d| format!(" - {d}")) - .collect::>() - .join("\n") - ); - } -} diff --git a/crates/module_fetcher/src/util.rs b/crates/module_fetcher/src/util.rs index ca8cd287a..7a309ef86 100644 --- a/crates/module_fetcher/src/util.rs +++ b/crates/module_fetcher/src/util.rs @@ -1,3 +1,6 @@ +use import_map::ImportMapDiagnostic; +use log::warn; + pub mod cache_setting; pub mod checksum; pub mod config_file; @@ -6,3 +9,16 @@ pub mod fs; pub mod path; pub mod text_encoding; pub mod version; + +pub fn print_import_map_diagnostics(diagnostics: &[ImportMapDiagnostic]) { + if !diagnostics.is_empty() { + warn!( + "Import map diagnostics:\n{}", + diagnostics + .iter() + .map(|d| format!(" - {d}")) + .collect::>() + .join("\n") + ); + } +} \ No newline at end of file From a161fdcaa2194f6127e63b217a2a6d4290087797 Mon Sep 17 00:00:00 2001 From: andreespirela Date: Wed, 2 Aug 2023 10:13:37 -0400 Subject: [PATCH 3/6] Fmt --- crates/module_fetcher/src/util.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/module_fetcher/src/util.rs b/crates/module_fetcher/src/util.rs index 7a309ef86..60d76cb64 100644 --- a/crates/module_fetcher/src/util.rs +++ b/crates/module_fetcher/src/util.rs @@ -21,4 +21,4 @@ pub fn print_import_map_diagnostics(diagnostics: &[ImportMapDiagnostic]) { .join("\n") ); } -} \ No newline at end of file +} From 7c8704b7201bfbdc22e2fcfbf7dd2ff280ae0ac0 Mon Sep 17 00:00:00 2001 From: andreespirela Date: Wed, 2 Aug 2023 10:16:08 -0400 Subject: [PATCH 4/6] Clippy --- crates/base/src/deno_runtime.rs | 4 ++-- crates/module_fetcher/src/args/import_map.rs | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/base/src/deno_runtime.rs b/crates/base/src/deno_runtime.rs index 91f29d80e..769ae1952 100644 --- a/crates/base/src/deno_runtime.rs +++ b/crates/base/src/deno_runtime.rs @@ -5,8 +5,8 @@ use anyhow::{anyhow, Error}; use deno_core::error::AnyError; use deno_core::url::Url; use deno_core::{located_script_name, serde_v8, JsRuntime, ModuleId, RuntimeOptions}; -use import_map::{parse_from_json, ImportMap, ImportMapDiagnostic}; -use log::{error, warn}; +use import_map::{parse_from_json, ImportMap}; +use log::{error}; use serde::de::DeserializeOwned; use std::collections::HashMap; use std::path::{Path, PathBuf}; diff --git a/crates/module_fetcher/src/args/import_map.rs b/crates/module_fetcher/src/args/import_map.rs index 1114aa6f1..6c8f04777 100644 --- a/crates/module_fetcher/src/args/import_map.rs +++ b/crates/module_fetcher/src/args/import_map.rs @@ -5,8 +5,6 @@ use deno_core::error::AnyError; use deno_core::serde_json; use deno_core::url::Url; use import_map::ImportMap; -use import_map::ImportMapDiagnostic; -use log::warn; use crate::file_fetcher::get_source_from_data_url; use crate::file_fetcher::FileFetcher; From d12a448a92971d3002800d272874e2f5507579b2 Mon Sep 17 00:00:00 2001 From: andreespirela Date: Wed, 2 Aug 2023 10:46:32 -0400 Subject: [PATCH 5/6] Remove repeated op_user_worker_log --- crates/sb_workers/lib.rs | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/crates/sb_workers/lib.rs b/crates/sb_workers/lib.rs index 267684446..84c5e3877 100644 --- a/crates/sb_workers/lib.rs +++ b/crates/sb_workers/lib.rs @@ -7,13 +7,9 @@ use deno_core::{ AsyncRefCell, AsyncResult, BufView, ByteString, CancelFuture, CancelHandle, CancelTryFuture, OpState, RcRef, Resource, ResourceId, WriteOutcome, }; -use event_worker::events::{ - EventMetadata, LogEvent, LogLevel, WorkerEventWithMetadata, WorkerEvents, -}; use hyper::body::HttpBody; use hyper::header::{HeaderName, HeaderValue}; use hyper::{Body, Request, Response}; -use log::error; use sb_worker_context::essentials::{ CreateUserWorkerResult, UserWorkerMsgs, UserWorkerRuntimeOpts, WorkerContextInitOpts, WorkerRuntimeOpts, @@ -34,7 +30,6 @@ deno_core::extension!( op_user_worker_create, op_user_worker_fetch_build, op_user_worker_fetch_send, - op_user_worker_log, ], esm = ["user_workers.js"] ); @@ -137,31 +132,6 @@ pub async fn op_user_worker_create( Ok(result.unwrap().key.to_string()) } -#[op] -pub fn op_user_worker_log(state: &mut OpState, msg: &str, is_err: bool) -> Result<(), AnyError> { - let maybe_tx = state.try_borrow::>(); - let mut level = LogLevel::Info; - if is_err { - level = LogLevel::Error; - } - if maybe_tx.is_some() { - let event_metadata = state - .try_borrow::() - .unwrap_or(&EventMetadata::default()) - .clone(); - maybe_tx.unwrap().send(WorkerEventWithMetadata { - event: WorkerEvents::Log(LogEvent { - msg: msg.to_string(), - level, - }), - metadata: event_metadata, - })?; - } else { - error!("[{:?}] {}", level, msg); - } - Ok(()) -} - #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct UserWorkerRequest { From 6adf76dd7a16a2c634bcbb5a3acb95718ad8b239 Mon Sep 17 00:00:00 2001 From: andreespirela Date: Wed, 2 Aug 2023 12:08:39 -0400 Subject: [PATCH 6/6] fmt --- crates/base/src/deno_runtime.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/base/src/deno_runtime.rs b/crates/base/src/deno_runtime.rs index 769ae1952..3befa6819 100644 --- a/crates/base/src/deno_runtime.rs +++ b/crates/base/src/deno_runtime.rs @@ -6,7 +6,7 @@ use deno_core::error::AnyError; use deno_core::url::Url; use deno_core::{located_script_name, serde_v8, JsRuntime, ModuleId, RuntimeOptions}; use import_map::{parse_from_json, ImportMap}; -use log::{error}; +use log::error; use serde::de::DeserializeOwned; use std::collections::HashMap; use std::path::{Path, PathBuf};