1
1
use crate :: js_worker:: { MainWorker , UserWorker } ;
2
2
3
- use anyhow:: Error ;
3
+ use anyhow:: { bail , Error } ;
4
4
use hyper:: { Body , Request , Response } ;
5
5
use log:: { debug, error} ;
6
6
use std:: collections:: HashMap ;
@@ -11,7 +11,9 @@ use std::thread;
11
11
use tokio:: net:: UnixStream ;
12
12
use tokio:: sync:: RwLock ;
13
13
use tokio:: sync:: { mpsc, oneshot} ;
14
+ use uuid:: Uuid ;
14
15
16
+ #[ derive( Debug ) ]
15
17
pub struct WorkerContext {
16
18
handle : thread:: JoinHandle < Result < ( ) , Error > > ,
17
19
request_sender : hyper:: client:: conn:: SendRequest < Body > ,
@@ -41,6 +43,10 @@ impl WorkerContext {
41
43
let import_map_path = options. import_map_path ;
42
44
let user_worker_msgs_tx = options. user_worker_msgs_tx ;
43
45
46
+ if ( !service_path. exists ( ) ) {
47
+ return bail ! ( "main function does not exist {:?}" , & service_path) ;
48
+ }
49
+
44
50
// create a unix socket pair
45
51
let ( sender_stream, recv_stream) = UnixStream :: pair ( ) ?;
46
52
@@ -90,6 +96,10 @@ impl WorkerContext {
90
96
let import_map_path = options. import_map_path ;
91
97
let env_vars = options. env_vars ;
92
98
99
+ if ( !service_path. exists ( ) ) {
100
+ return bail ! ( "user function does not exist {:?}" , & service_path) ;
101
+ }
102
+
93
103
// create a unix socket pair
94
104
let ( sender_stream, recv_stream) = UnixStream :: pair ( ) ?;
95
105
@@ -141,13 +151,16 @@ impl WorkerContext {
141
151
142
152
#[ derive( Debug ) ]
143
153
pub struct CreateUserWorkerResult {
144
- pub key : String ,
154
+ pub key : Uuid ,
145
155
}
146
156
147
157
#[ derive( Debug ) ]
148
158
pub enum UserWorkerMsgs {
149
- Create ( UserWorkerOptions , oneshot:: Sender < CreateUserWorkerResult > ) ,
150
- SendRequest ( String , Request < Body > , oneshot:: Sender < Response < Body > > ) ,
159
+ Create (
160
+ UserWorkerOptions ,
161
+ oneshot:: Sender < Result < CreateUserWorkerResult , Error > > ,
162
+ ) ,
163
+ SendRequest ( Uuid , Request < Body > , oneshot:: Sender < Response < Body > > ) ,
151
164
}
152
165
153
166
pub struct WorkerPool {
@@ -174,23 +187,24 @@ impl WorkerPool {
174
187
let main_worker = Arc :: new ( RwLock :: new ( main_worker_ctx) ) ;
175
188
176
189
tokio:: spawn ( async move {
177
- let mut user_workers: HashMap < String , Arc < RwLock < WorkerContext > > > = HashMap :: new ( ) ;
190
+ let mut user_workers: HashMap < Uuid , Arc < RwLock < WorkerContext > > > = HashMap :: new ( ) ;
178
191
179
192
loop {
180
193
match user_worker_msgs_rx. recv ( ) . await {
181
194
None => break ,
182
195
Some ( UserWorkerMsgs :: Create ( worker_options, tx) ) => {
183
- let key = worker_options. service_path . display ( ) . to_string ( ) ;
184
- if !user_workers. contains_key ( & key) {
185
- // TODO: handle errors
186
- let user_worker_ctx = WorkerContext :: new_user_worker ( worker_options)
187
- . await
188
- . unwrap ( ) ;
189
- user_workers
190
- . insert ( key. clone ( ) , Arc :: new ( RwLock :: new ( user_worker_ctx) ) ) ;
196
+ let key = Uuid :: new_v4 ( ) ;
197
+ let user_worker_ctx = WorkerContext :: new_user_worker ( worker_options) . await ;
198
+ if !user_worker_ctx. is_err ( ) {
199
+ user_workers. insert (
200
+ key. clone ( ) ,
201
+ Arc :: new ( RwLock :: new ( user_worker_ctx. unwrap ( ) ) ) ,
202
+ ) ;
203
+
204
+ tx. send ( Ok ( CreateUserWorkerResult { key } ) ) ;
205
+ } else {
206
+ tx. send ( Err ( user_worker_ctx. unwrap_err ( ) ) ) ;
191
207
}
192
-
193
- tx. send ( CreateUserWorkerResult { key } ) ;
194
208
}
195
209
Some ( UserWorkerMsgs :: SendRequest ( key, req, tx) ) => {
196
210
// TODO: handle errors
0 commit comments