11// Copyright 2018-2025 the Deno authors. MIT license.
22
33use super :: Output ;
4- use super :: create_runtime;
4+ use super :: Snapshot ;
5+ use super :: create_runtime_from_snapshot;
56use super :: run_async;
67use anyhow:: anyhow;
78use deno_core:: GarbageCollected ;
@@ -25,6 +26,7 @@ use tokio::sync::mpsc::unbounded_channel;
2526use tokio:: sync:: watch;
2627
2728/// Our cppgc object.
29+ #[ derive( Debug ) ]
2830pub struct WorkerControl {
2931 worker_channel : WorkerChannel ,
3032 close_watcher : WorkerCloseWatcher ,
@@ -34,16 +36,18 @@ pub struct WorkerControl {
3436
3537impl GarbageCollected for WorkerControl { }
3638
39+ #[ derive( Debug ) ]
3740pub struct WorkerChannel {
3841 tx : UnboundedSender < String > ,
3942 rx : Mutex < UnboundedReceiver < String > > ,
4043}
4144
42- #[ derive( Clone ) ]
45+ #[ derive( Debug , Clone ) ]
4346pub struct WorkerCloseWatcher {
4447 watcher : Arc < Mutex < watch:: Receiver < bool > > > ,
4548}
4649
50+ #[ derive( Debug ) ]
4751pub struct Worker {
4852 _close_send : watch:: Sender < bool > ,
4953 pub ( crate ) close_watcher : WorkerCloseWatcher ,
@@ -92,16 +96,22 @@ pub fn worker_create(
9296pub fn op_worker_spawn (
9397 #[ state] this_worker : & Worker ,
9498 #[ state] output : & Output ,
99+ #[ state] snapshot : & Snapshot ,
95100 #[ string] base_url : String ,
96101 #[ string] main_script : String ,
97102) -> Result < WorkerControl , std:: sync:: mpsc:: RecvError > {
98103 let output = output. clone ( ) ;
104+ let snapshot = snapshot. 0 ;
99105 let close_watcher = this_worker. close_watcher . clone ( ) ;
100106 let ( init_send, init_recv) = channel ( ) ;
101107 let ( shutdown_tx, shutdown_rx) = unbounded_channel ( ) ;
102108 std:: thread:: spawn ( move || {
103- let ( mut runtime, worker_host_side) =
104- create_runtime ( Some ( close_watcher) , vec ! [ ] ) ;
109+ let ( mut runtime, worker_host_side) = create_runtime_from_snapshot (
110+ snapshot,
111+ false ,
112+ Some ( close_watcher) ,
113+ vec ! [ ] ,
114+ ) ;
105115 runtime. op_state ( ) . borrow_mut ( ) . put ( output. clone ( ) ) ;
106116 init_send
107117 . send ( WorkerControl {
@@ -147,11 +157,21 @@ async fn run_worker_task(
147157 let state = state. borrow ( ) ;
148158 let output: & Output = state. borrow ( ) ;
149159 for line in e. to_string ( ) . split ( '\n' ) {
160+ println ! ( "[ERR] {line}" ) ;
161+ output. line ( format ! ( "[ERR] {line}" ) ) ;
162+ }
163+ return Ok ( ( ) ) ;
164+ } else if let Err ( e) = f. await {
165+ let state = runtime. op_state ( ) . clone ( ) ;
166+ let state = state. borrow ( ) ;
167+ let output: & Output = state. borrow ( ) ;
168+ for line in e. to_string ( ) . split ( '\n' ) {
169+ println ! ( "[ERR] {line}" ) ;
150170 output. line ( format ! ( "[ERR] {line}" ) ) ;
151171 }
152172 return Ok ( ( ) ) ;
153173 }
154- _ = f . await ;
174+
155175 Ok ( ( ) )
156176}
157177
0 commit comments