Skip to content

Commit

Permalink
fix: js read body blocking, save failed deploy message failed, fix tr…
Browse files Browse the repository at this point in the history
…affic period with current time
  • Loading branch information
Gitea committed Mar 31, 2024
1 parent f99831d commit 4827d09
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 26 deletions.
4 changes: 2 additions & 2 deletions crates/kernel/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::Result;

/// componentize_js compile to js to wasm component
pub fn componentize_js(src: &str, target: &str) -> Result<()> {
pub fn componentize_js(src: &str, target: &str, js_engine: Option<String>) -> Result<()> {
// compile js to wizer
land_wit::compile_js(src, target, None)?;
land_wit::compile_js(src, target, js_engine)?;
componentize_wasm(target)
}

Expand Down
30 changes: 20 additions & 10 deletions crates/kernel/src/tasks/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::DeployTask;
use anyhow::{anyhow, Result};
use land_dao::deployment::{self, StorageInfo};
use land_dao::deployment::{self, DeployStatus, StorageInfo};
use land_dao::models::playground::Model as PlaygroundModel;
use land_dao::models::project::Model as ProjectModel;
use land_dao::{playground, project};
Expand All @@ -24,14 +24,20 @@ pub async fn init() -> Result<()> {
Ok(_) => (),
Err(e) => {
warn!(dp_id = deploy_id, "Send_deploy_task error: {:?}", e);
let _ = deployment::mark_status(
deploy_id,
deployment::DeployStatus::Failed,
format!("failed:{}", e),
None,
None,
)
.await;
let mut msg = format!("failed:{}", e);
// msg should less 250 chars
if msg.len() > 250 {
msg = msg[..250].to_string() + "...";
}
let res =
deployment::mark_status(deploy_id, DeployStatus::Failed, msg, None, None)
.await;
if res.is_err() {
warn!(
dp_id = deploy_id,
"Update deployment status error: {:?}", res
);
}
}
}
}
Expand Down Expand Up @@ -97,7 +103,11 @@ async fn compile_and_upload_playground(
let target_wasm = dir.path().join(format!("{}_{}.wasm", pl.project_id, pl.id));

// 2. compile js source to wasm
crate::builder::componentize_js(source_js.to_str().unwrap(), target_wasm.to_str().unwrap())?;
crate::builder::componentize_js(
source_js.to_str().unwrap(),
target_wasm.to_str().unwrap(),
None,
)?;
debug!("Compile success");

// 3. upload to r2
Expand Down
Binary file modified crates/wit/engine/js-engine.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions crates/wit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ pub fn compile_js(src_path: &str, dst_path: &str, js_engine: Option<String>) ->

// copy js_engine.wasm to path dir
let js_engine_bytes = if let Some(js_engine) = js_engine {
debug!("Read js_engine: {}", js_engine);
std::fs::read(js_engine)?
} else {
debug!("Read js_engine from embedded");
include_bytes!("../engine/js-engine.wasm").to_vec()
};
debug!("Read js_engine_bytes: {}", js_engine_bytes.len());
Expand Down
12 changes: 8 additions & 4 deletions crates/worker-impl/src/hostcall/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ static REDIRECT_FOLLOW_POOL: OnceCell<Client> = OnceCell::new();
static REDIRECT_ERROR_POOL: OnceCell<Client> = OnceCell::new();
static REDIRECT_MANUAL_POOL: OnceCell<Client> = OnceCell::new();

/// READ_DEFAULT_SIZE is the default read size in once read if not specified
const READ_DEFAULT_SIZE: u32 = 128 * 1024;

fn init_clients() {
CLIENT_INIT_ONCE.call_once(|| {
REDIRECT_ERROR_POOL
Expand Down Expand Up @@ -139,10 +142,11 @@ impl HttpContext {
handle: u32,
size: u32,
) -> Result<(Vec<u8>, bool), BodyError> {
let read_size = if size == 0 { READ_DEFAULT_SIZE } else { size };
// use buffer first
let mut prev_buffer = self.body_buffer_map.remove(&handle).unwrap_or_default();
if prev_buffer.len() >= size as usize {
let (first, second) = prev_buffer.split_at(size as usize);
if prev_buffer.len() >= read_size as usize {
let (first, second) = prev_buffer.split_at(read_size as usize);
self.body_buffer_map.insert(handle, second.to_vec());
return Ok((first.to_vec(), false));
}
Expand Down Expand Up @@ -174,8 +178,8 @@ impl HttpContext {
)));
}
prev_buffer.extend_from_slice(&chunk.unwrap());
if prev_buffer.len() >= size as usize {
let (first, second) = prev_buffer.split_at(size as usize);
if prev_buffer.len() >= read_size as usize {
let (first, second) = prev_buffer.split_at(read_size as usize);
self.body_buffer_map.insert(handle, second.to_vec());
return Ok((first.to_vec(), false));
}
Expand Down
11 changes: 8 additions & 3 deletions crates/worker-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,14 @@ async fn wasm_caller_handler(
}
let method = req.method().clone();
let mut context = Context::new();
// let req_id = context.req_id();
let body = req.into_body();
let body_handle = context.set_body(0, body); // 0 means creating new body handle
// if method is GET or DELETE, set body to None
let body_handle = if method == "GET" || method == "DELETE" {
0
} else {
let body = req.into_body();
context.set_body(0, body)
};
debug!("Set body_handle: {:?}", body_handle);
let wasm_req = WasmRequest {
method: method.to_string(),
uri: uri.to_string(),
Expand Down
6 changes: 3 additions & 3 deletions land-cli/src/cmds/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Build {
impl Build {
pub async fn run(&self) -> Result<()> {
let metadata = manifest::Data::from_file(manifest::MANIFEST_FILE)?;
build_internal(&metadata)?;
build_internal(&metadata, self.js_engine.clone())?;

cprintln!(
"<bright-cyan,bold>Finished</> building project '{}'.",
Expand All @@ -37,7 +37,7 @@ fn run_command(cmd_str: &str) -> Result<()> {
Ok(())
}

pub fn build_internal(metadata: &manifest::Data) -> Result<()> {
pub fn build_internal(metadata: &manifest::Data, js_engine: Option<String>) -> Result<()> {
let build_command = metadata.build.command.clone();
if !build_command.is_empty() {
cprintln!("Run build command: {}", build_command);
Expand All @@ -46,7 +46,7 @@ pub fn build_internal(metadata: &manifest::Data) -> Result<()> {
let target = metadata.build.target.clone();
if metadata.project.language == "javascript" {
let wasm_target = metadata.wasm_target();
return land_kernel::builder::componentize_js(&target, &wasm_target);
return land_kernel::builder::componentize_js(&target, &wasm_target, js_engine);
}
land_kernel::builder::componentize_wasm(&target)
}
4 changes: 3 additions & 1 deletion land-cli/src/cmds/up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub struct Up {
pub address: Option<String>,
#[clap(long = "build")]
pub build: bool,
#[clap(short = 'j', long = "js-engine")]
pub js_engine: Option<String>,
}

fn validate_address(listen: &str) -> Result<String, String> {
Expand All @@ -25,7 +27,7 @@ impl Up {
pub async fn run(&self) -> Result<()> {
let metadata = Data::from_file(MANIFEST_FILE)?;
if self.build {
build_internal(&metadata)?;
build_internal(&metadata, self.js_engine.clone())?;
}
if !std::path::Path::new(&metadata.build.target).exists() {
return Err(anyhow::anyhow!(
Expand Down
7 changes: 4 additions & 3 deletions land-server/src/server/dash/traffic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ struct PeriodParams {

impl RequestsQuery {
fn get_period(&self) -> PeriodParams {
let st = chrono::Utc::now().timestamp();
if let Some(p) = self.period.as_ref() {
if p.eq("weekly") {
let end = chrono::Utc::now().timestamp() / 3600 * 3600;
let start = end - 604800; // 86400 * 7
let end = (st + 3599) / 3600 * 3600; // add 3500 for round hour, use next hour
let start = end - 604800; // 86400 * 7 + 2
let sequence = (0..169).map(|i| start + i * 3600).collect();
return PeriodParams {
start,
Expand All @@ -36,7 +37,7 @@ impl RequestsQuery {
};
}
}
let end = chrono::Utc::now().timestamp() / 600 * 600;
let end = (st + 599) / 600 * 600; // use next 10 min
let start = end - 86400; // oneday 1440/10+1
let sequence = (0..145).map(|i| start + i * 600).collect();
PeriodParams {
Expand Down

0 comments on commit 4827d09

Please sign in to comment.