From d0fd8eff4ebd5a9c26ea8d6c39b2ac80a126a8e4 Mon Sep 17 00:00:00 2001 From: max-lt Date: Sat, 23 Mar 2024 20:28:06 +0100 Subject: [PATCH] Handle log --- Cargo.lock | 2 +- Cargo.toml | 2 +- examples/scheduled.rs | 4 ++-- examples/serve-new.rs | 4 ++-- examples/serve-same.rs | 4 ++-- src/ext/event_scheduled.js | 2 +- src/ext/mod.rs | 1 + src/ext/runtime.js | 5 ++++- src/ext/runtime.rs | 43 ++++++++++++++++++++++++++++++++++++-- src/lib.rs | 1 + src/runtime.rs | 16 +++++++++++--- 11 files changed, 69 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b1c2542..d0911d5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1664,7 +1664,7 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openworkers-runtime" -version = "0.1.3" +version = "0.1.4" dependencies = [ "actix-web", "bytes", diff --git a/Cargo.toml b/Cargo.toml index 24a4f8a..a9057ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "openworkers-runtime" -version = "0.1.3" +version = "0.1.4" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/examples/scheduled.rs b/examples/scheduled.rs index ceb6605..1b79c70 100644 --- a/examples/scheduled.rs +++ b/examples/scheduled.rs @@ -39,7 +39,7 @@ async fn main() -> Result<(), ()> { let script = Script { specifier: module_url(file_path.as_str()), code: None, - env: None, + env: None }; let time = std::time::SystemTime::now() @@ -51,7 +51,7 @@ async fn main() -> Result<(), ()> { let local = tokio::task::LocalSet::new(); local.spawn_local(async move { - let mut worker = Worker::new(script).await.unwrap(); + let mut worker = Worker::new(script, None).await.unwrap(); match worker .exec(Task::Scheduled(Some(ScheduledInit::new(res_tx, time)))) diff --git a/examples/serve-new.rs b/examples/serve-new.rs index b460c27..02011d2 100644 --- a/examples/serve-new.rs +++ b/examples/serve-new.rs @@ -41,7 +41,7 @@ async fn handle_request(data: Data, req: HttpRequest) -> HttpResponse let script = Script { specifier: url.clone(), code: None, - env: None, + env: None }; let (res_tx, res_rx) = channel::>(); @@ -52,7 +52,7 @@ async fn handle_request(data: Data, req: HttpRequest) -> HttpResponse let tasks = local.spawn_local(async move { debug!("create worker"); - let mut worker = Worker::new(script).await.unwrap(); + let mut worker = Worker::new(script, None).await.unwrap(); debug!("exec fetch task"); match worker.exec(task).await { diff --git a/examples/serve-same.rs b/examples/serve-same.rs index 55dc9ad..1b6e4ac 100644 --- a/examples/serve-same.rs +++ b/examples/serve-same.rs @@ -110,7 +110,7 @@ async fn main() -> std::io::Result<()> { let script = Script { specifier: url.clone(), code: None, - env: None, + env: None }; let (task_tx, mut task_rx) = tokio::sync::mpsc::channel(1); @@ -119,7 +119,7 @@ async fn main() -> std::io::Result<()> { let local = tokio::task::LocalSet::new(); let tasks = local.spawn_local(async move { - let mut worker = Worker::new(script).await.unwrap(); + let mut worker = Worker::new(script, None).await.unwrap(); loop { match task_rx.recv().await { diff --git a/src/ext/event_scheduled.js b/src/ext/event_scheduled.js index 5025bc4..65e168f 100644 --- a/src/ext/event_scheduled.js +++ b/src/ext/event_scheduled.js @@ -19,7 +19,7 @@ function triggerScheduledEvent(rid) { const evt = op_scheduled_init(rid); // Convert seconds to milliseconds - const scheduledTime = evt.time * 1000; + const scheduledTime = evt.time; scheduledEventListener({ scheduledTime, diff --git a/src/ext/mod.rs b/src/ext/mod.rs index 53a60aa..21202d0 100644 --- a/src/ext/mod.rs +++ b/src/ext/mod.rs @@ -4,6 +4,7 @@ mod event_fetch; mod event_scheduled; pub use runtime::runtime as runtime_ext; +pub use runtime::LogEvent; pub use event_fetch::fetch_event as fetch_event_ext; pub use event_fetch::FetchInit; diff --git a/src/ext/runtime.js b/src/ext/runtime.js index eca472c..20ae2cd 100644 --- a/src/ext/runtime.js +++ b/src/ext/runtime.js @@ -1,4 +1,5 @@ // runtime.js +import { op_log } from "ext:core/ops"; // deno_core import { core, primordials } from "ext:core/mod.js"; @@ -178,7 +179,9 @@ import * as eventSource from "ext:deno_fetch/27_eventsource.js"; console: nonEnumerable( // https://choubey.gitbook.io/internals-of-deno/bridge/4.2-print - new console.Console((msg, level) => core.print(msg, level > 1)) + new console.Console((msg, level) => + op_log(level > 1 ? "error" : "log", msg) + ) ), // DOM Exception diff --git a/src/ext/runtime.rs b/src/ext/runtime.rs index efb0bcc..ef61bc6 100644 --- a/src/ext/runtime.rs +++ b/src/ext/runtime.rs @@ -1,11 +1,50 @@ use deno_core::Extension; use deno_core::ExtensionFileSource; +use deno_core::OpState; +use deno_core::serde::Serialize; deno_core::extension!( runtime, - deps = [deno_console, deno_web, deno_crypto, deno_fetch, fetch_event, scheduled_event], + deps = [ + deno_console, + deno_web, + deno_crypto, + deno_fetch, + fetch_event, + scheduled_event + ], + ops = [op_log], customizer = |ext: &mut Extension| { - ext.esm_files.to_mut().push(ExtensionFileSource::new("ext:runtime.js", include_str!("runtime.js"))); + ext.esm_files.to_mut().push(ExtensionFileSource::new( + "ext:runtime.js", + include_str!("runtime.js"), + )); ext.esm_entry_point = Some("ext:runtime.js"); } ); + +#[derive(Debug, Serialize)] +pub struct LogEvent { + pub level: String, + pub message: String, +} + +#[deno_core::op2(fast)] +fn op_log(state: &mut OpState, #[string] level: &str, #[string] message: &str) { + let evt = LogEvent { + level: level.to_string(), + message: message.to_string(), + }; + + log::debug!("op_log {:?}", evt); + + let tx = state.try_borrow_mut::>(); + + match tx { + None => log::warn!("failed to borrow log event sender"), + Some(tx) => match tx.send(evt) { + Ok(_) => {}, + Err(_) => log::error!("failed to send log event"), + }, + } +} diff --git a/src/lib.rs b/src/lib.rs index 2ec944d..9a23f85 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ pub (crate) use runtime::extensions; pub use runtime::Script; pub use runtime::Worker; +pub use ext::LogEvent; pub use ext::FetchInit; pub use ext::ScheduledInit; pub use deno_core::error::AnyError; diff --git a/src/runtime.rs b/src/runtime.rs index e6079dc..3ea05b8 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -2,8 +2,8 @@ use crate::ext::fetch_event_ext; use crate::ext::permissions_ext; use crate::ext::runtime_ext; use crate::ext::scheduled_event_ext; - use crate::ext::Permissions; +use crate::LogEvent; use crate::Task; use std::rc::Rc; @@ -78,7 +78,7 @@ pub(crate) fn extensions(for_snapshot: bool) -> Vec { pub struct Script { pub specifier: deno_core::ModuleSpecifier, pub code: Option, - pub env: Option, + pub env: Option } pub struct Worker { @@ -88,7 +88,7 @@ pub struct Worker { } impl Worker { - pub async fn new(script: Script) -> Result { + pub async fn new(script: Script, log_tx: Option>) -> Result { let mut js_runtime = match runtime_snapshot() { None => { debug!("no runtime snapshot"); @@ -117,6 +117,16 @@ impl Worker { let trigger_fetch; let trigger_scheduled; + // Log event sender + { + match log_tx { + Some(tx) => js_runtime.op_state().borrow_mut().put::>(tx), + None => { + log::warn!("no log event sender provided"); + }, + }; + } + // Bootstrap { let script = format!("globalThis.bootstrap('{}', {})", user_agent(), script.env.unwrap_or("undefined".to_string()));