2020-01-02 20:13:47 +00:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 00:34:02 +00:00
|
|
|
|
2020-10-20 04:05:42 +00:00
|
|
|
use crate::colors;
|
2020-11-19 19:37:22 +00:00
|
|
|
use crate::fmt_errors::PrettyJsError;
|
2020-04-16 21:40:29 +00:00
|
|
|
use crate::ops::io::get_stdio;
|
2020-05-04 18:10:59 +00:00
|
|
|
use crate::permissions::Permissions;
|
2020-10-13 11:35:35 +00:00
|
|
|
use crate::program_state::ProgramState;
|
2020-02-11 09:04:59 +00:00
|
|
|
use crate::tokio_util::create_basic_runtime;
|
2020-09-28 10:14:11 +00:00
|
|
|
use crate::worker::WebWorker;
|
|
|
|
use crate::worker::WebWorkerHandle;
|
2020-02-11 09:04:59 +00:00
|
|
|
use crate::worker::WorkerEvent;
|
2020-10-20 04:05:42 +00:00
|
|
|
use deno_core::error::generic_error;
|
2020-09-14 16:48:57 +00:00
|
|
|
use deno_core::error::AnyError;
|
2020-10-20 04:05:42 +00:00
|
|
|
use deno_core::futures::channel::mpsc;
|
2020-09-21 16:36:37 +00:00
|
|
|
use deno_core::futures::future::FutureExt;
|
|
|
|
use deno_core::serde_json;
|
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_json::Value;
|
2020-08-28 15:08:24 +00:00
|
|
|
use deno_core::BufVec;
|
2020-04-23 09:51:07 +00:00
|
|
|
use deno_core::ModuleSpecifier;
|
2020-09-10 13:57:45 +00:00
|
|
|
use deno_core::OpState;
|
2020-04-23 09:51:07 +00:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-16 16:43:08 +00:00
|
|
|
use serde::Deserialize;
|
2020-09-10 13:57:45 +00:00
|
|
|
use std::cell::RefCell;
|
2020-09-19 23:17:35 +00:00
|
|
|
use std::collections::HashMap;
|
2019-08-14 15:03:02 +00:00
|
|
|
use std::convert::From;
|
2020-08-18 16:30:13 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::sync::Arc;
|
2020-02-18 19:47:11 +00:00
|
|
|
use std::thread::JoinHandle;
|
2019-10-11 18:41:54 +00:00
|
|
|
|
2020-10-20 04:05:42 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct HostUnhandledErrorArgs {
|
|
|
|
message: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init(
|
|
|
|
rt: &mut deno_core::JsRuntime,
|
|
|
|
sender: Option<mpsc::Sender<WorkerEvent>>,
|
|
|
|
) {
|
2020-09-28 10:14:11 +00:00
|
|
|
{
|
|
|
|
let op_state = rt.op_state();
|
|
|
|
let mut state = op_state.borrow_mut();
|
|
|
|
state.put::<WorkersTable>(WorkersTable::default());
|
|
|
|
state.put::<WorkerId>(WorkerId::default());
|
|
|
|
}
|
2020-09-10 13:57:45 +00:00
|
|
|
super::reg_json_sync(rt, "op_create_worker", op_create_worker);
|
|
|
|
super::reg_json_sync(
|
|
|
|
rt,
|
|
|
|
"op_host_terminate_worker",
|
|
|
|
op_host_terminate_worker,
|
|
|
|
);
|
|
|
|
super::reg_json_sync(rt, "op_host_post_message", op_host_post_message);
|
|
|
|
super::reg_json_async(rt, "op_host_get_message", op_host_get_message);
|
2020-10-20 04:05:42 +00:00
|
|
|
super::reg_json_sync(
|
|
|
|
rt,
|
|
|
|
"op_host_unhandled_error",
|
|
|
|
move |_state, args, _zero_copy| {
|
|
|
|
if let Some(mut sender) = sender.clone() {
|
|
|
|
let args: HostUnhandledErrorArgs = serde_json::from_value(args)?;
|
|
|
|
sender
|
|
|
|
.try_send(WorkerEvent::Error(generic_error(args.message)))
|
|
|
|
.expect("Failed to propagate error event to parent worker");
|
|
|
|
Ok(json!(true))
|
|
|
|
} else {
|
|
|
|
Err(generic_error("Cannot be called from main worker."))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2020-02-11 09:04:59 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
pub type WorkersTable = HashMap<u32, (JoinHandle<()>, WebWorkerHandle)>;
|
|
|
|
pub type WorkerId = u32;
|
|
|
|
|
2020-02-11 09:04:59 +00:00
|
|
|
fn create_web_worker(
|
2020-04-14 15:41:06 +00:00
|
|
|
worker_id: u32,
|
2020-02-11 09:04:59 +00:00
|
|
|
name: String,
|
2020-10-13 11:35:35 +00:00
|
|
|
program_state: &Arc<ProgramState>,
|
2020-05-04 18:10:59 +00:00
|
|
|
permissions: Permissions,
|
2020-02-11 09:04:59 +00:00
|
|
|
specifier: ModuleSpecifier,
|
2020-04-16 21:40:29 +00:00
|
|
|
has_deno_namespace: bool,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<WebWorker, AnyError> {
|
2020-09-19 23:17:35 +00:00
|
|
|
let mut worker = WebWorker::new(
|
|
|
|
name.clone(),
|
|
|
|
permissions,
|
2020-09-10 13:57:45 +00:00
|
|
|
specifier,
|
2020-10-13 11:35:35 +00:00
|
|
|
program_state.clone(),
|
2020-09-19 23:17:35 +00:00
|
|
|
has_deno_namespace,
|
|
|
|
);
|
2020-04-16 21:40:29 +00:00
|
|
|
|
2020-04-21 13:48:44 +00:00
|
|
|
if has_deno_namespace {
|
2020-10-07 15:20:20 +00:00
|
|
|
let state = worker.js_runtime.op_state();
|
2020-09-10 13:57:45 +00:00
|
|
|
let mut state = state.borrow_mut();
|
2020-04-21 13:48:44 +00:00
|
|
|
let (stdin, stdout, stderr) = get_stdio();
|
2020-07-09 19:06:51 +00:00
|
|
|
if let Some(stream) = stdin {
|
2020-09-10 13:57:45 +00:00
|
|
|
state.resource_table.add("stdin", Box::new(stream));
|
2020-07-09 19:06:51 +00:00
|
|
|
}
|
|
|
|
if let Some(stream) = stdout {
|
2020-09-10 13:57:45 +00:00
|
|
|
state.resource_table.add("stdout", Box::new(stream));
|
2020-07-09 19:06:51 +00:00
|
|
|
}
|
|
|
|
if let Some(stream) = stderr {
|
2020-09-10 13:57:45 +00:00
|
|
|
state.resource_table.add("stderr", Box::new(stream));
|
2020-07-09 19:06:51 +00:00
|
|
|
}
|
2020-04-21 13:48:44 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 15:41:06 +00:00
|
|
|
// Instead of using name for log we use `worker-${id}` because
|
|
|
|
// WebWorkers can have empty string as name.
|
|
|
|
let script = format!(
|
2020-04-24 23:03:45 +00:00
|
|
|
"bootstrap.workerRuntime(\"{}\", {}, \"worker-{}\")",
|
2020-04-16 21:40:29 +00:00
|
|
|
name, worker.has_deno_namespace, worker_id
|
2020-04-14 15:41:06 +00:00
|
|
|
);
|
2020-02-11 09:04:59 +00:00
|
|
|
worker.execute(&script)?;
|
|
|
|
|
|
|
|
Ok(worker)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bartlomieju): check if order of actions is aligned to Worker spec
|
|
|
|
fn run_worker_thread(
|
2020-04-14 15:41:06 +00:00
|
|
|
worker_id: u32,
|
2020-02-11 09:04:59 +00:00
|
|
|
name: String,
|
2020-10-13 11:35:35 +00:00
|
|
|
program_state: &Arc<ProgramState>,
|
2020-05-04 18:10:59 +00:00
|
|
|
permissions: Permissions,
|
2020-02-11 09:04:59 +00:00
|
|
|
specifier: ModuleSpecifier,
|
2020-04-16 21:40:29 +00:00
|
|
|
has_deno_namespace: bool,
|
|
|
|
maybe_source_code: Option<String>,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<(JoinHandle<()>, WebWorkerHandle), AnyError> {
|
2020-10-13 11:35:35 +00:00
|
|
|
let program_state = program_state.clone();
|
2020-02-11 09:04:59 +00:00
|
|
|
let (handle_sender, handle_receiver) =
|
2020-09-14 16:48:57 +00:00
|
|
|
std::sync::mpsc::sync_channel::<Result<WebWorkerHandle, AnyError>>(1);
|
2020-02-11 09:04:59 +00:00
|
|
|
|
|
|
|
let builder =
|
2020-04-14 15:41:06 +00:00
|
|
|
std::thread::Builder::new().name(format!("deno-worker-{}", worker_id));
|
2020-02-18 19:47:11 +00:00
|
|
|
let join_handle = builder.spawn(move || {
|
2020-02-11 09:04:59 +00:00
|
|
|
// Any error inside this block is terminal:
|
|
|
|
// - JS worker is useless - meaning it throws an exception and can't do anything else,
|
|
|
|
// all action done upon it should be noops
|
|
|
|
// - newly spawned thread exits
|
2020-04-14 15:41:06 +00:00
|
|
|
let result = create_web_worker(
|
|
|
|
worker_id,
|
|
|
|
name,
|
2020-10-13 11:35:35 +00:00
|
|
|
&program_state,
|
2020-04-14 15:41:06 +00:00
|
|
|
permissions,
|
|
|
|
specifier.clone(),
|
2020-04-16 21:40:29 +00:00
|
|
|
has_deno_namespace,
|
2020-04-14 15:41:06 +00:00
|
|
|
);
|
2020-02-11 09:04:59 +00:00
|
|
|
|
|
|
|
if let Err(err) = result {
|
|
|
|
handle_sender.send(Err(err)).unwrap();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut worker = result.unwrap();
|
2020-04-09 22:15:17 +00:00
|
|
|
let name = worker.name.to_string();
|
2020-02-11 09:04:59 +00:00
|
|
|
// Send thread safe handle to newly created worker to host thread
|
|
|
|
handle_sender.send(Ok(worker.thread_safe_handle())).unwrap();
|
|
|
|
drop(handle_sender);
|
|
|
|
|
|
|
|
// At this point the only method of communication with host
|
|
|
|
// is using `worker.internal_channels`.
|
|
|
|
//
|
|
|
|
// Host can already push messages and interact with worker.
|
|
|
|
//
|
|
|
|
// Next steps:
|
|
|
|
// - create tokio runtime
|
|
|
|
// - load provided module or code
|
|
|
|
// - start driving worker's event loop
|
|
|
|
|
|
|
|
let mut rt = create_basic_runtime();
|
|
|
|
|
|
|
|
// TODO: run with using select with terminate
|
|
|
|
|
|
|
|
// Execute provided source code immediately
|
2020-04-16 21:40:29 +00:00
|
|
|
let result = if let Some(source_code) = maybe_source_code {
|
2020-02-11 09:04:59 +00:00
|
|
|
worker.execute(&source_code)
|
|
|
|
} else {
|
|
|
|
// TODO(bartlomieju): add "type": "classic", ie. ability to load
|
|
|
|
// script instead of module
|
2020-02-18 15:08:18 +00:00
|
|
|
let load_future = worker.execute_module(&specifier).boxed_local();
|
2020-02-11 09:04:59 +00:00
|
|
|
|
|
|
|
rt.block_on(load_future)
|
|
|
|
};
|
|
|
|
|
2020-10-14 12:04:09 +00:00
|
|
|
let mut sender = worker.internal_channels.sender.clone();
|
2020-10-11 11:20:40 +00:00
|
|
|
|
2020-10-14 12:04:09 +00:00
|
|
|
// If sender is closed it means that worker has already been closed from
|
|
|
|
// within using "globalThis.close()"
|
|
|
|
if sender.is_closed() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(e) = result {
|
2020-10-20 04:05:42 +00:00
|
|
|
eprintln!(
|
|
|
|
"{}: Uncaught (in worker \"{}\") {}",
|
|
|
|
colors::red_bold("error"),
|
|
|
|
name,
|
|
|
|
e.to_string().trim_start_matches("Uncaught "),
|
|
|
|
);
|
2020-04-09 22:15:17 +00:00
|
|
|
sender
|
|
|
|
.try_send(WorkerEvent::TerminalError(e))
|
2020-02-11 09:04:59 +00:00
|
|
|
.expect("Failed to post message to host");
|
|
|
|
|
|
|
|
// Failure to execute script is a terminal error, bye, bye.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bartlomieju): this thread should return result of event loop
|
|
|
|
// that means that we should store JoinHandle to thread to ensure
|
|
|
|
// that it actually terminates.
|
2020-10-11 11:20:40 +00:00
|
|
|
rt.block_on(worker.run_event_loop())
|
|
|
|
.expect("Panic in event loop");
|
2020-04-09 22:15:17 +00:00
|
|
|
debug!("Worker thread shuts down {}", &name);
|
2020-02-11 09:04:59 +00:00
|
|
|
})?;
|
|
|
|
|
2020-02-18 19:47:11 +00:00
|
|
|
let worker_handle = handle_receiver.recv().unwrap()?;
|
|
|
|
Ok((join_handle, worker_handle))
|
2019-10-11 18:41:54 +00:00
|
|
|
}
|
2019-08-14 15:03:02 +00:00
|
|
|
|
2019-08-26 12:50:21 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct CreateWorkerArgs {
|
2020-01-29 17:54:23 +00:00
|
|
|
name: Option<String>,
|
2019-08-26 12:50:21 +00:00
|
|
|
specifier: String,
|
|
|
|
has_source_code: bool,
|
|
|
|
source_code: String,
|
2020-04-16 21:40:29 +00:00
|
|
|
use_deno_namespace: bool,
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create worker as the host
|
2019-10-11 18:41:54 +00:00
|
|
|
fn op_create_worker(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2019-08-26 12:50:21 +00:00
|
|
|
args: Value,
|
2020-06-01 18:20:47 +00:00
|
|
|
_data: &mut [ZeroCopyBuf],
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<Value, AnyError> {
|
2019-08-26 12:50:21 +00:00
|
|
|
let args: CreateWorkerArgs = serde_json::from_value(args)?;
|
|
|
|
|
2020-02-03 23:08:44 +00:00
|
|
|
let specifier = args.specifier.clone();
|
2020-04-16 21:40:29 +00:00
|
|
|
let maybe_source_code = if args.has_source_code {
|
|
|
|
Some(args.source_code.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2020-02-03 23:08:44 +00:00
|
|
|
let args_name = args.name;
|
2020-04-16 21:40:29 +00:00
|
|
|
let use_deno_namespace = args.use_deno_namespace;
|
2020-05-07 19:15:59 +00:00
|
|
|
if use_deno_namespace {
|
2020-09-26 18:26:51 +00:00
|
|
|
super::check_unstable(state, "Worker.deno");
|
2020-05-07 19:15:59 +00:00
|
|
|
}
|
2020-09-19 23:17:35 +00:00
|
|
|
let permissions = state.borrow::<Permissions>().clone();
|
|
|
|
let worker_id = state.take::<WorkerId>();
|
|
|
|
state.put::<WorkerId>(worker_id + 1);
|
2020-02-08 19:34:31 +00:00
|
|
|
|
2020-06-09 12:33:52 +00:00
|
|
|
let module_specifier = ModuleSpecifier::resolve_url(&specifier)?;
|
2020-04-14 15:41:06 +00:00
|
|
|
let worker_name = args_name.unwrap_or_else(|| "".to_string());
|
2020-10-13 11:35:35 +00:00
|
|
|
let cli_state = super::program_state(state);
|
2020-01-17 23:43:53 +00:00
|
|
|
|
2020-02-18 19:47:11 +00:00
|
|
|
let (join_handle, worker_handle) = run_worker_thread(
|
2020-04-14 15:41:06 +00:00
|
|
|
worker_id,
|
2020-02-11 09:04:59 +00:00
|
|
|
worker_name,
|
2020-09-19 23:17:35 +00:00
|
|
|
&cli_state,
|
2020-02-11 09:04:59 +00:00
|
|
|
permissions,
|
|
|
|
module_specifier,
|
2020-04-16 21:40:29 +00:00
|
|
|
use_deno_namespace,
|
|
|
|
maybe_source_code,
|
2020-08-25 22:22:15 +00:00
|
|
|
)?;
|
2020-02-11 09:04:59 +00:00
|
|
|
// At this point all interactions with worker happen using thread
|
|
|
|
// safe handler returned from previous function call
|
2020-09-19 23:17:35 +00:00
|
|
|
state
|
|
|
|
.borrow_mut::<WorkersTable>()
|
2020-02-18 19:47:11 +00:00
|
|
|
.insert(worker_id, (join_handle, worker_handle));
|
2019-08-26 12:50:21 +00:00
|
|
|
|
2020-08-28 15:08:24 +00:00
|
|
|
Ok(json!({ "id": worker_id }))
|
2019-11-17 00:17:47 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 12:50:21 +00:00
|
|
|
#[derive(Deserialize)]
|
2020-01-17 23:43:53 +00:00
|
|
|
struct WorkerArgs {
|
2019-11-09 20:07:14 +00:00
|
|
|
id: i32,
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 09:04:59 +00:00
|
|
|
fn op_host_terminate_worker(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2020-01-17 23:43:53 +00:00
|
|
|
args: Value,
|
2020-06-01 18:20:47 +00:00
|
|
|
_data: &mut [ZeroCopyBuf],
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-01-17 23:43:53 +00:00
|
|
|
let args: WorkerArgs = serde_json::from_value(args)?;
|
|
|
|
let id = args.id as u32;
|
2020-09-19 23:17:35 +00:00
|
|
|
let (join_handle, worker_handle) = state
|
|
|
|
.borrow_mut::<WorkersTable>()
|
2020-08-18 16:30:13 +00:00
|
|
|
.remove(&id)
|
|
|
|
.expect("No worker handle found");
|
2020-02-11 09:04:59 +00:00
|
|
|
worker_handle.terminate();
|
2020-02-18 19:47:11 +00:00
|
|
|
join_handle.join().expect("Panic in worker thread");
|
2020-08-28 15:08:24 +00:00
|
|
|
Ok(json!({}))
|
2020-01-17 23:43:53 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 09:04:59 +00:00
|
|
|
fn serialize_worker_event(event: WorkerEvent) -> Value {
|
|
|
|
match event {
|
|
|
|
WorkerEvent::Message(buf) => json!({ "type": "msg", "data": buf }),
|
2020-04-09 22:15:17 +00:00
|
|
|
WorkerEvent::TerminalError(error) => {
|
|
|
|
let mut serialized_error = json!({
|
|
|
|
"type": "terminalError",
|
|
|
|
"error": {
|
|
|
|
"message": error.to_string(),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-19 19:37:22 +00:00
|
|
|
if let Ok(js_error) = error.downcast::<PrettyJsError>() {
|
2020-04-09 22:15:17 +00:00
|
|
|
serialized_error = json!({
|
|
|
|
"type": "terminalError",
|
|
|
|
"error": {
|
|
|
|
"message": js_error.message,
|
|
|
|
"fileName": js_error.script_resource_name,
|
|
|
|
"lineNumber": js_error.line_number,
|
|
|
|
"columnNumber": js_error.start_column,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
serialized_error
|
|
|
|
}
|
2020-02-21 15:36:13 +00:00
|
|
|
WorkerEvent::Error(error) => {
|
|
|
|
let mut serialized_error = json!({
|
|
|
|
"type": "error",
|
|
|
|
"error": {
|
|
|
|
"message": error.to_string(),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-11-19 19:37:22 +00:00
|
|
|
if let Ok(js_error) = error.downcast::<PrettyJsError>() {
|
2020-02-21 15:36:13 +00:00
|
|
|
serialized_error = json!({
|
2020-02-11 09:04:59 +00:00
|
|
|
"type": "error",
|
|
|
|
"error": {
|
2020-03-02 22:20:16 +00:00
|
|
|
"message": js_error.message,
|
|
|
|
"fileName": js_error.script_resource_name,
|
|
|
|
"lineNumber": js_error.line_number,
|
|
|
|
"columnNumber": js_error.start_column,
|
2020-02-11 09:04:59 +00:00
|
|
|
}
|
2020-02-21 15:36:13 +00:00
|
|
|
});
|
2020-02-11 09:04:59 +00:00
|
|
|
}
|
2020-02-21 15:36:13 +00:00
|
|
|
|
|
|
|
serialized_error
|
|
|
|
}
|
2020-02-11 09:04:59 +00:00
|
|
|
}
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get message from guest worker as host
|
2020-08-28 15:08:24 +00:00
|
|
|
async fn op_host_get_message(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2019-08-26 12:50:21 +00:00
|
|
|
args: Value,
|
2020-08-28 15:08:24 +00:00
|
|
|
_zero_copy: BufVec,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-02-11 09:04:59 +00:00
|
|
|
let args: WorkerArgs = serde_json::from_value(args)?;
|
2019-11-09 20:07:14 +00:00
|
|
|
let id = args.id as u32;
|
2020-08-28 15:08:24 +00:00
|
|
|
|
2020-09-10 13:57:45 +00:00
|
|
|
let worker_handle = {
|
2020-09-19 23:17:35 +00:00
|
|
|
let s = state.borrow();
|
|
|
|
let workers_table = s.borrow::<WorkersTable>();
|
2020-09-10 13:57:45 +00:00
|
|
|
let maybe_handle = workers_table.get(&id);
|
|
|
|
if let Some(handle) = maybe_handle {
|
|
|
|
handle.1.clone()
|
|
|
|
} else {
|
|
|
|
// If handle was not found it means worker has already shutdown
|
|
|
|
return Ok(json!({ "type": "close" }));
|
|
|
|
}
|
2020-08-28 15:08:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let response = match worker_handle.get_event().await? {
|
|
|
|
Some(event) => {
|
|
|
|
// Terminal error means that worker should be removed from worker table.
|
|
|
|
if let WorkerEvent::TerminalError(_) = &event {
|
2020-09-19 23:17:35 +00:00
|
|
|
let mut s = state.borrow_mut();
|
2020-08-28 15:08:24 +00:00
|
|
|
if let Some((join_handle, mut worker_handle)) =
|
2020-09-19 23:17:35 +00:00
|
|
|
s.borrow_mut::<WorkersTable>().remove(&id)
|
2020-08-28 15:08:24 +00:00
|
|
|
{
|
2020-04-09 22:15:17 +00:00
|
|
|
worker_handle.sender.close_channel();
|
|
|
|
join_handle.join().expect("Worker thread panicked");
|
2020-09-10 13:57:45 +00:00
|
|
|
};
|
2020-02-11 09:04:59 +00:00
|
|
|
}
|
2020-08-28 15:08:24 +00:00
|
|
|
serialize_worker_event(event)
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// Worker shuts down
|
2020-09-19 23:17:35 +00:00
|
|
|
let mut s = state.borrow_mut();
|
|
|
|
let workers = s.borrow_mut::<WorkersTable>();
|
2020-08-28 15:08:24 +00:00
|
|
|
// Try to remove worker from workers table - NOTE: `Worker.terminate()` might have been called
|
|
|
|
// already meaning that we won't find worker in table - in that case ignore.
|
|
|
|
if let Some((join_handle, mut worker_handle)) = workers.remove(&id) {
|
|
|
|
worker_handle.sender.close_channel();
|
|
|
|
join_handle.join().expect("Worker thread panicked");
|
|
|
|
}
|
|
|
|
json!({ "type": "close" })
|
|
|
|
}
|
2020-01-04 10:20:52 +00:00
|
|
|
};
|
2020-08-28 15:08:24 +00:00
|
|
|
Ok(response)
|
2019-08-26 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 15:03:02 +00:00
|
|
|
/// Post message to guest worker as host
|
2019-10-11 18:41:54 +00:00
|
|
|
fn op_host_post_message(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2019-08-26 12:50:21 +00:00
|
|
|
args: Value,
|
2020-06-01 18:20:47 +00:00
|
|
|
data: &mut [ZeroCopyBuf],
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-06-01 18:20:47 +00:00
|
|
|
assert_eq!(data.len(), 1, "Invalid number of arguments");
|
2020-02-11 09:04:59 +00:00
|
|
|
let args: WorkerArgs = serde_json::from_value(args)?;
|
2019-11-09 20:07:14 +00:00
|
|
|
let id = args.id as u32;
|
2020-06-01 18:20:47 +00:00
|
|
|
let msg = Vec::from(&*data[0]).into_boxed_slice();
|
2019-11-09 20:07:14 +00:00
|
|
|
|
|
|
|
debug!("post message to worker {}", id);
|
2020-09-19 23:17:35 +00:00
|
|
|
let workers = state.borrow::<WorkersTable>();
|
2020-08-18 16:30:13 +00:00
|
|
|
let worker_handle = workers[&id].1.clone();
|
2020-08-25 22:22:15 +00:00
|
|
|
worker_handle.post_message(msg)?;
|
2020-08-28 15:08:24 +00:00
|
|
|
Ok(json!({}))
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|