Skip to content

Commit

Permalink
Fix default snapshot path
Browse files Browse the repository at this point in the history
  • Loading branch information
max-lt committed Mar 23, 2024
1 parent 714fdce commit 50fdebe
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[env]
RUNTIME_SNAPSHOT_PATH = { value = "/tmp/openworkers-runtime-snapshot.bin", relative = false }
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "openworkers-runtime"
version = "0.1.4"
version = "0.1.5"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
5 changes: 2 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use std::fs::File;
use std::path::PathBuf;

const RUNTIME_SNAPSHOT_PATH: Option<&str> = option_env!("RUNTIME_SNAPSHOT_PATH");
const DEFAULT_SNAPSHOT_PATH: &str = "/tmp/openworkers-runtime-snapshot.bin";
const RUNTIME_SNAPSHOT_PATH: &str = env!("RUNTIME_SNAPSHOT_PATH");

fn main () {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=src/ext/*");
println!("cargo:rerun-if-changed=src/runtime.rs");
println!("cargo:rerun-if-changed=src/extensions.rs");

let path = PathBuf::from(RUNTIME_SNAPSHOT_PATH.unwrap_or(DEFAULT_SNAPSHOT_PATH));
let path = PathBuf::from(RUNTIME_SNAPSHOT_PATH);

// Create the file if it doesn't exist
if !path.exists() {
Expand Down
37 changes: 24 additions & 13 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ use log::debug;

const USER_AGENT: &str = concat!("OpenWorkers/", env!("CARGO_PKG_VERSION"));

const RUNTIME_SNAPSHOT: &[u8] = include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/target/RUNTIME_SNAPSHOT.bin"
));
const RUNTIME_SNAPSHOT: &[u8] = include_bytes!(env!("RUNTIME_SNAPSHOT_PATH"));

pub (crate) fn user_agent() -> String {
pub(crate) fn user_agent() -> String {
USER_AGENT.to_string()
}

Expand Down Expand Up @@ -78,7 +75,7 @@ pub(crate) fn extensions(for_snapshot: bool) -> Vec<deno_core::Extension> {
pub struct Script {
pub specifier: deno_core::ModuleSpecifier,
pub code: Option<deno_core::ModuleCodeString>,
pub env: Option<String>
pub env: Option<String>,
}

pub struct Worker {
Expand All @@ -88,7 +85,10 @@ pub struct Worker {
}

impl Worker {
pub async fn new(script: Script, log_tx: Option<std::sync::mpsc::Sender<LogEvent>>) -> Result<Self, AnyError> {
pub async fn new(
script: Script,
log_tx: Option<std::sync::mpsc::Sender<LogEvent>>,
) -> Result<Self, AnyError> {
let mut js_runtime = match runtime_snapshot() {
None => {
debug!("no runtime snapshot");
Expand Down Expand Up @@ -120,16 +120,23 @@ impl Worker {
// Log event sender
{
match log_tx {
Some(tx) => js_runtime.op_state().borrow_mut().put::<std::sync::mpsc::Sender<LogEvent>>(tx),
Some(tx) => js_runtime
.op_state()
.borrow_mut()
.put::<std::sync::mpsc::Sender<LogEvent>>(tx),
None => {
log::warn!("no log event sender provided");
},
}
};
}

// Bootstrap
{
let script = format!("globalThis.bootstrap('{}', {})", user_agent(), script.env.unwrap_or("undefined".to_string()));
let script = format!(
"globalThis.bootstrap('{}', {})",
user_agent(),
script.env.unwrap_or("undefined".to_string())
);
let script = deno_core::ModuleCodeString::from(script);

match js_runtime.execute_script(deno_core::located_script_name!(), script) {
Expand All @@ -145,8 +152,10 @@ impl Worker {
Err(err) => panic!("failed to convert triggers to object: {:?}", err),
};

trigger_fetch = crate::util::extract_trigger("fetch", scope, object).expect("fetch trigger not found");
trigger_scheduled = crate::util::extract_trigger("scheduled", scope, object).expect("scheduled trigger not found");
trigger_fetch = crate::util::extract_trigger("fetch", scope, object)
.expect("fetch trigger not found");
trigger_scheduled = crate::util::extract_trigger("scheduled", scope, object)
.expect("scheduled trigger not found");
}
Err(err) => panic!("bootstrap failed: {:?}", err),
}
Expand All @@ -156,7 +165,9 @@ impl Worker {

// Eval main module
{
let mod_id = js_runtime.load_main_module(&script.specifier, script.code).await?;
let mod_id = js_runtime
.load_main_module(&script.specifier, script.code)
.await?;

let result = js_runtime.mod_evaluate(mod_id);

Expand Down
5 changes: 2 additions & 3 deletions src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ use std::env;
use std::path::PathBuf;
use std::fs::File;

const RUNTIME_SNAPSHOT_PATH: Option<&str> = option_env!("RUNTIME_SNAPSHOT_PATH");
const DEFAULT_SNAPSHOT_PATH: &str = "/tmp/openworkers-runtime-snapshot.bin";
const RUNTIME_SNAPSHOT_PATH: &str = env!("RUNTIME_SNAPSHOT_PATH");

pub fn create_runtime_snapshot() {
println!("Building snapshot");

// Build the file path to the snapshot.
let snapshot_path = PathBuf::from(RUNTIME_SNAPSHOT_PATH.unwrap_or(DEFAULT_SNAPSHOT_PATH));
let snapshot_path = PathBuf::from(RUNTIME_SNAPSHOT_PATH);

let serializer: SnapshotFileSerializer = SnapshotFileSerializer::new(File::create(snapshot_path).unwrap());

Expand Down

0 comments on commit 50fdebe

Please sign in to comment.