golem-cli 0.0.23

Command line interface for OSS version of Golem. See also golem-cloud-cli.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use crate::context::golem_template_service::GolemTemplateServiceInfo;
use crate::context::golem_worker_service::GolemWorkerServiceInfo;
use crate::context::redis::RedisInfo;
use crate::context::{EnvConfig, ShardManagerInfo, NETWORK, TAG};
use libtest_mimic::Failed;
use std::collections::HashMap;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::process::{Child, Command, Stdio};
use std::time::Duration;
use testcontainers::core::WaitFor;
use testcontainers::{clients, Container, Image, RunnableImage};
use tokio::time::timeout;
use tonic::transport::Uri;
use tonic_health::pb::health_check_response::ServingStatus;
use tonic_health::pb::health_client::HealthClient;
use tonic_health::pb::HealthCheckRequest;

pub struct WorkerExecutor<'docker_client> {
    host: String,
    port: u16,
    inner: WorkerExecutorInner<'docker_client>,
}

enum WorkerExecutorInner<'docker_client> {
    Process(Child),
    Docker(Container<'docker_client, WorkerExecutorImage>),
}

#[derive(Debug)]
struct WorkerExecutorImage {
    grpc_port: u16,
    http_port: u16,
    env_vars: HashMap<String, String>,
}

impl WorkerExecutorImage {
    pub fn new(
        grpc_port: u16,
        http_port: u16,
        env_vars: HashMap<String, String>,
    ) -> WorkerExecutorImage {
        WorkerExecutorImage {
            grpc_port,
            http_port,
            env_vars,
        }
    }
}

impl Image for WorkerExecutorImage {
    type Args = ();

    fn name(&self) -> String {
        "golemservices/golem-worker-executor".to_string()
    }

    fn tag(&self) -> String {
        TAG.to_owned()
    }

    fn ready_conditions(&self) -> Vec<WaitFor> {
        vec![WaitFor::message_on_stdout("Registering worker executor")]
    }

    fn env_vars(&self) -> Box<dyn Iterator<Item = (&String, &String)> + '_> {
        Box::new(self.env_vars.iter())
    }

    fn expose_ports(&self) -> Vec<u16> {
        vec![self.grpc_port, self.http_port]
    }
}

impl<'docker_client> WorkerExecutor<'docker_client> {
    pub fn start(
        docker: &'docker_client clients::Cli,
        shard_id: u16,
        env_config: &EnvConfig,
        redis: &RedisInfo,
        worker: &GolemWorkerServiceInfo,
        template: &GolemTemplateServiceInfo,
        shard_manager: &ShardManagerInfo,
    ) -> Result<WorkerExecutor<'docker_client>, Failed> {
        if env_config.local_golem {
            WorkerExecutor::start_process(
                shard_id,
                env_config,
                redis,
                worker,
                template,
                shard_manager,
            )
        } else {
            WorkerExecutor::start_docker(
                docker,
                shard_id,
                env_config,
                redis,
                worker,
                template,
                shard_manager,
            )
        }
    }

    fn start_docker(
        docker: &'docker_client clients::Cli,
        shard_id: u16,
        env_config: &EnvConfig,
        redis: &RedisInfo,
        worker: &GolemWorkerServiceInfo,
        template: &GolemTemplateServiceInfo,
        shard_manager: &ShardManagerInfo,
    ) -> Result<WorkerExecutor<'docker_client>, Failed> {
        println!("Starting Worker Executor {shard_id} docker");

        let port = 9000 + shard_id;
        let http_port = 9100 + shard_id;

        let env_vars = WorkerExecutor::env_vars(
            port,
            http_port,
            env_config,
            redis,
            worker,
            template,
            shard_manager,
        );
        let name = format!("golem_worker_executor{shard_id}");

        let image = RunnableImage::from(WorkerExecutorImage::new(port, http_port, env_vars))
            .with_container_name(&name)
            .with_network(NETWORK);
        let node = docker.run(image);

        Ok(WorkerExecutor {
            host: name,
            port,
            inner: WorkerExecutorInner::Docker(node),
        })
    }

    fn env_vars(
        port: u16,
        http_port: u16,
        env_config: &EnvConfig,
        redis: &RedisInfo,
        worker: &GolemWorkerServiceInfo,
        template: &GolemTemplateServiceInfo,
        shard_manager: &ShardManagerInfo,
    ) -> HashMap<String, String> {
        let log_level = if env_config.verbose { "debug" } else { "info" };

        let env: &[(&str, &str)] = &[
            ("RUST_LOG"                                      , &format!("{log_level},cranelift_codegen=warn,wasmtime_cranelift=warn,wasmtime_jit=warn,h2=warn,hyper=warn,tower=warn")),
            ("ENVIRONMENT"                    , "local"),
            ("WASMTIME_BACKTRACE_DETAILS"                    , "1"),
            ("RUST_BACKTRACE"                                , "1"),
            ("GOLEM__REDIS__HOST"                            , &redis.host),
            ("GOLEM__REDIS__PORT"                            , &redis.port.to_string()),
            ("GOLEM__REDIS__KEY_PREFIX"                      , &env_config.redis_key_prefix),
            ("GOLEM__PUBLIC_WORKER_API__HOST"                , &worker.host),
            ("GOLEM__PUBLIC_WORKER_API__PORT"                , &worker.grpc_port.to_string()),
            ("GOLEM__PUBLIC_WORKER_API__ACCESS_TOKEN"        , "2A354594-7A63-4091-A46B-CC58D379F677"),
            ("GOLEM__TEMPLATE_SERVICE__CONFIG__HOST"         , &template.host),
            ("GOLEM__TEMPLATE_SERVICE__CONFIG__PORT"         , &template.grpc_port.to_string()),
            ("GOLEM__TEMPLATE_SERVICE__CONFIG__ACCESS_TOKEN" , "2A354594-7A63-4091-A46B-CC58D379F677"),
            ("GOLEM__COMPILED_TEMPLATE_SERVICE__TYPE"        , "Disabled"),
            // ("GOLEM__COMPILED_TEMPLATE_SERVICE__TYPE"        , "Local"),
            // ("GOLEM__COMPILED_TEMPLATE_SERVICE__CONFIG__ROOT"        , "data/templates"),
            ("GOLEM__BLOB_STORE_SERVICE__TYPE"               , "InMemory"),
            ("GOLEM__SHARD_MANAGER_SERVICE__TYPE"               , "Grpc"),
            ("GOLEM__SHARD_MANAGER_SERVICE__CONFIG__HOST"    , &shard_manager.host),
            ("GOLEM__SHARD_MANAGER_SERVICE__CONFIG__PORT"    , &shard_manager.grpc_port.to_string()),
            ("GOLEM__SHARD_MANAGER_SERVICE__CONFIG__RETRIES__MAX_ATTEMPTS"    , "5"),
            ("GOLEM__SHARD_MANAGER_SERVICE__CONFIG__RETRIES__MIN_DELAY"    , "100ms"),
            ("GOLEM__SHARD_MANAGER_SERVICE__CONFIG__RETRIES__MAX_DELAY"    , "2s"),
            ("GOLEM__SHARD_MANAGER_SERVICE__CONFIG__RETRIES__MULTIPLIER"    , "2"),
            ("GOLEM__PORT"                                   , &port.to_string()),
            ("GOLEM__HTTP_PORT"                              , &http_port.to_string()),
        ];

        env.iter()
            .map(|(k, v)| (k.to_string(), v.to_string()))
            .collect()
    }

    fn start_process(
        shard_id: u16,
        env_config: &EnvConfig,
        redis: &RedisInfo,
        worker: &GolemWorkerServiceInfo,
        template: &GolemTemplateServiceInfo,
        shard_manager: &ShardManagerInfo,
    ) -> Result<WorkerExecutor<'docker_client>, Failed> {
        let port = 9000 + shard_id;
        let http_port = 9100 + shard_id;

        println!("Starting Worker Executor {shard_id}");

        let service_dir = Path::new("../golem-worker-executor/");
        let service_run = Path::new("../target/debug/worker-executor");

        if !service_run.exists() {
            return Err(format!(
                "Expected to have precompiled Worker Executor at {}",
                service_run.to_str().unwrap_or("")
            )
            .into());
        }

        println!("Starting {}", service_run.to_str().unwrap_or(""));

        let mut child = Command::new(service_run)
            .current_dir(service_dir)
            .envs(WorkerExecutor::env_vars(
                port,
                http_port,
                env_config,
                redis,
                worker,
                template,
                shard_manager,
            ))
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()?;

        let quiet = env_config.quiet;

        let stdout = child
            .stdout
            .take()
            .ok_or::<Failed>("Can't get Worker Executor stdout".into())?;

        // TODO: use tokio
        std::thread::spawn(move || {
            let reader = BufReader::new(stdout);
            for line in reader.lines() {
                if !quiet {
                    println!("[workerexec{shard_id}]   {}", line.unwrap())
                }
            }
        });

        let stderr = child
            .stderr
            .take()
            .ok_or::<Failed>("Can't get Worker Executor stderr".into())?;

        std::thread::spawn(move || {
            let reader = BufReader::new(stderr);
            for line in reader.lines() {
                if !quiet {
                    eprintln!("[workerexec{shard_id}]   {}", line.unwrap())
                }
            }
        });

        WorkerExecutor::wait_for_health_check(port);

        println!("Worker Executor {shard_id} online");

        Ok(WorkerExecutor {
            host: "localhost".to_string(),
            port,
            inner: WorkerExecutorInner::Process(child),
        })
    }

    fn wait_for_health_check(port: u16) {
        let wait_loop = async {
            loop {
                if WorkerExecutor::health_check_address("localhost", port).await {
                    break;
                } else {
                    println!("Worker Executor healthcheck failed.");
                    tokio::time::sleep(Duration::from_secs(1)).await;
                }
            }
        };

        // TODO: async start
        tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .expect("Failed building the Runtime")
            .block_on(wait_loop)
    }

    async fn health_check_address(host: &str, port: u16) -> bool {
        let address = format!("http://{host}:{port}");

        println!("Worker executor health checking on {address}");
        match address.parse::<Uri>() {
            Ok(uri) => {
                let conn = timeout(
                    Duration::from_secs(2),
                    tonic::transport::Endpoint::from(uri).connect(),
                )
                .await;

                match conn {
                    Ok(conn) => match conn {
                        Ok(conn) => {
                            let request = HealthCheckRequest {
                                service: "".to_string(),
                            };
                            match HealthClient::new(conn).check(request).await {
                                Ok(response) => {
                                    response.into_inner().status == ServingStatus::Serving as i32
                                }
                                Err(err) => {
                                    println!("Health request returned with an error: {:?}", err);
                                    false
                                }
                            }
                        }
                        Err(err) => {
                            println!("Failed to connect to {address}: {:?}", err);
                            false
                        }
                    },
                    Err(_) => {
                        println!("Connection to {address} timed out");
                        false
                    }
                }
            }
            Err(_) => {
                println!("Worker executor has an invalid URI: {address}");
                false
            }
        }
    }

    pub fn info(&self) -> WorkerExecutorInfo {
        WorkerExecutorInfo {
            host: self.host.clone(),
            port: self.port,
        }
    }
}

impl Drop for WorkerExecutor<'_> {
    fn drop(&mut self) {
        match &mut self.inner {
            WorkerExecutorInner::Process(child) => {
                // TODO: SIGTERM, maybe with nix crate.
                match child.kill() {
                    Ok(_) => println!("Killed Worker Executor (port {}) process", self.port),
                    Err(e) => eprintln!(
                        "Failed to kill Worker Executor (port {}) process: {e:? }",
                        self.port
                    ),
                }
            }
            WorkerExecutorInner::Docker(_) => {}
        }

        println!("Stopped Worker Executor (port {})", self.port)
    }
}

pub struct WorkerExecutors<'docker_client> {
    worker_executors: Vec<WorkerExecutor<'docker_client>>,
}

impl<'docker_client> WorkerExecutors<'docker_client> {
    pub fn start(
        docker: &'docker_client clients::Cli,
        env_config: &EnvConfig,
        redis: &RedisInfo,
        worker: &GolemWorkerServiceInfo,
        template: &GolemTemplateServiceInfo,
        shard_manager: &ShardManagerInfo,
    ) -> Result<WorkerExecutors<'docker_client>, Failed> {
        let shards = 3;

        let mut worker_executors = Vec::with_capacity(shards);

        for shard_id in 0..shards {
            worker_executors.push(WorkerExecutor::start(
                docker,
                shard_id.try_into().unwrap(),
                env_config,
                redis,
                worker,
                template,
                shard_manager,
            )?)
        }

        Ok(WorkerExecutors { worker_executors })
    }

    pub fn info(&self) -> WorkerExecutorsInfo {
        WorkerExecutorsInfo {
            worker_executors: self.worker_executors.iter().map(|we| we.info()).collect(),
        }
    }
}

impl Drop for WorkerExecutors<'_> {
    fn drop(&mut self) {
        println!("Stopping {} worker executors", self.worker_executors.len())
    }
}

#[derive(Debug)]
pub struct WorkerExecutorInfo {
    host: String,
    pub port: u16,
}

impl WorkerExecutorInfo {
    pub fn health_check(&self) -> bool {
        let result = WorkerExecutor::health_check_address(&self.host, self.port);

        tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .expect("Failed building the Runtime")
            .block_on(result)
    }
}

#[derive(Debug)]
pub struct WorkerExecutorsInfo {
    pub worker_executors: Vec<WorkerExecutorInfo>,
}