Skip to content

Commit d44341c

Browse files
committed
exec: extract common option as record
1 parent 7463643 commit d44341c

File tree

6 files changed

+95
-133
lines changed

6 files changed

+95
-133
lines changed

exec/exec/src/component.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::durability::{DurableExec, EmptySnapshot, SessionSnapshot};
22
use crate::golem::exec::executor::{
3-
Error, ExecResult, File, Guest, GuestSession, Language, Limits,
3+
Error, ExecResult, File, Guest, GuestSession, Language, RunOptions,
44
};
55
use crate::golem::exec::types::LanguageKind;
66
use crate::{get_contents, io_error, stage_result_failure};
@@ -13,19 +13,16 @@ impl Guest for Component {
1313

1414
fn run(
1515
lang: Language,
16-
snippet: String,
1716
modules: Vec<File>,
18-
stdin: Option<String>,
19-
args: Vec<String>,
20-
env: Vec<(String, String)>,
21-
constraints: Option<Limits>,
17+
snippet: String,
18+
options: RunOptions,
2219
) -> Result<ExecResult, Error> {
2320
match &lang.kind {
2421
LanguageKind::Javascript => {
2522
#[cfg(feature = "javascript")]
2623
{
2724
let session = crate::javascript::JavaScriptSession::new(lang, modules);
28-
session.run(snippet, args, stdin, env, constraints)
25+
session.run(snippet, options)
2926
}
3027
#[cfg(not(feature = "javascript"))]
3128
{
@@ -36,7 +33,7 @@ impl Guest for Component {
3633
#[cfg(feature = "python")]
3734
{
3835
let session = crate::python::PythonSession::new(lang, modules);
39-
session.run(snippet, args, stdin, env, constraints)
36+
session.run(snippet, options)
4037
}
4138
#[cfg(not(feature = "python"))]
4239
{
@@ -121,19 +118,12 @@ impl GuestSession for Session {
121118
Ok(())
122119
}
123120

124-
fn run(
125-
&self,
126-
snippet: String,
127-
args: Vec<String>,
128-
stdin: Option<String>,
129-
env: Vec<(String, String)>,
130-
constraints: Option<Limits>,
131-
) -> Result<ExecResult, Error> {
121+
fn run(&self, snippet: String, options: RunOptions) -> Result<ExecResult, Error> {
132122
match self {
133123
#[cfg(feature = "javascript")]
134-
Session::Javascript(session) => session.run(snippet, args, stdin, env, constraints),
124+
Session::Javascript(session) => session.run(snippet, options),
135125
#[cfg(feature = "python")]
136-
Session::Python(session) => session.run(snippet, args, stdin, env, constraints),
126+
Session::Python(session) => session.run(snippet, options),
137127
Session::Unsupported => Err(Error::UnsupportedLanguage),
138128
}
139129
}

exec/exec/src/durability.rs

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,20 @@ pub struct EmptySnapshot {}
2424
#[cfg(not(feature = "durability"))]
2525
mod passthrough_impl {
2626
use crate::durability::DurableExec;
27-
use crate::golem::exec::executor::{Error, ExecResult, File, Guest, Language, Limits};
27+
use crate::golem::exec::executor::{
28+
Error, ExecResult, File, Guest, Language, RunOptions,
29+
};
2830

2931
impl<Impl: Guest> Guest for DurableExec<Impl> {
3032
type Session = Impl::Session;
3133

3234
fn run(
3335
lang: Language,
34-
snippet: String,
3536
modules: Vec<File>,
36-
stdin: Option<String>,
37-
args: Vec<String>,
38-
env: Vec<(String, String)>,
39-
constraints: Option<Limits>,
37+
snippet: String,
38+
options: RunOptions,
4039
) -> Result<ExecResult, Error> {
41-
Impl::run(lang, snippet, modules, stdin, args, env, constraints)
40+
Impl::run(lang, modules, snippet, options)
4241
}
4342
}
4443
}
@@ -47,7 +46,7 @@ mod passthrough_impl {
4746
mod durable_impl {
4847
use crate::durability::{DurableExec, SessionSnapshot};
4948
use crate::golem::exec::executor::{
50-
Error, ExecResult, File, Guest, GuestSession, Language, Limits,
49+
Error, ExecResult, File, Guest, GuestSession, Language, RunOptions,
5150
};
5251
use golem_rust::bindings::golem::durability::durability::DurableFunctionType;
5352
use golem_rust::durability::Durability;
@@ -62,12 +61,9 @@ mod durable_impl {
6261

6362
fn run(
6463
lang: Language,
65-
snippet: String,
6664
modules: Vec<File>,
67-
stdin: Option<String>,
68-
args: Vec<String>,
69-
env: Vec<(String, String)>,
70-
constraints: Option<Limits>,
65+
snippet: String,
66+
options: RunOptions,
7167
) -> Result<ExecResult, Error> {
7268
let durability = Durability::<ExecResult, Error>::new(
7369
"golem_exec",
@@ -78,23 +74,17 @@ mod durable_impl {
7874
let result = with_persistence_level(PersistenceLevel::PersistNothing, || {
7975
Impl::run(
8076
lang.clone(),
81-
snippet.clone(),
8277
modules.clone(),
83-
stdin.clone(),
84-
args.clone(),
85-
env.clone(),
86-
constraints,
78+
snippet.clone(),
79+
options.clone(),
8780
)
8881
});
8982
durability.persist_serializable(
9083
RunInput {
9184
language: lang,
9285
modules: modules.iter().map(|f| f.name.clone()).collect(),
9386
snippet,
94-
args,
95-
stdin,
96-
env,
97-
constraints,
87+
options,
9888
},
9989
result.clone(),
10090
);
@@ -124,14 +114,7 @@ mod durable_impl {
124114
self.inner.upload(file)
125115
}
126116

127-
fn run(
128-
&self,
129-
snippet: String,
130-
args: Vec<String>,
131-
stdin: Option<String>,
132-
env: Vec<(String, String)>,
133-
constraints: Option<Limits>,
134-
) -> Result<ExecResult, Error> {
117+
fn run(&self, snippet: String, options: RunOptions) -> Result<ExecResult, Error> {
135118
let durability = Durability::<SessionRunResult<Impl::Snapshot>, UnusedError>::new(
136119
"golem_exec",
137120
"session_run",
@@ -141,17 +124,14 @@ mod durable_impl {
141124
language: self.lang.clone(),
142125
modules: self.module_names.clone(),
143126
snippet: snippet.clone(),
144-
args: args.clone(),
145-
stdin: stdin.clone(),
146-
env: env.clone(),
147-
constraints,
127+
options: options.clone(),
148128
};
149129
if Impl::supports_snapshot(&self.inner) {
150130
// We can take a snapshot of the session and restore it during replay without
151131
// actually running the snippet.
152132
if durability.is_live() {
153133
let result = with_persistence_level(PersistenceLevel::PersistNothing, || {
154-
self.inner.run(snippet, args, stdin, env, constraints)
134+
self.inner.run(snippet, options)
155135
});
156136
let snapshot = Impl::take_snapshot(&self.inner);
157137
let result = SessionRunResult {
@@ -172,7 +152,7 @@ mod durable_impl {
172152
// in both live and replay modes.
173153
//
174154
// We still persist a custom oplog entry to increase oplog readability
175-
let result = self.inner.run(snippet, args, stdin, env, constraints);
155+
let result = self.inner.run(snippet, options);
176156
let result = SessionRunResult {
177157
result,
178158
snapshot: None,
@@ -205,10 +185,7 @@ mod durable_impl {
205185
language: Language,
206186
modules: Vec<String>,
207187
snippet: String,
208-
args: Vec<String>,
209-
stdin: Option<String>,
210-
env: Vec<(String, String)>,
211-
constraints: Option<Limits>,
188+
options: RunOptions,
212189
}
213190

214191
#[derive(Debug, Clone)]

exec/exec/src/javascript/mod.rs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod builtin;
22

33
use crate::durability::{EmptySnapshot, SessionSnapshot};
4-
use crate::golem::exec::executor::{Error, ExecResult, File, Language, Limits};
4+
use crate::golem::exec::executor::{Error, ExecResult, File, Language, RunOptions};
55
use crate::golem::exec::types::{LanguageKind, StageResult};
66
use crate::{get_contents_as_string, stage_result_failure};
77
use futures::TryFutureExt;
@@ -26,15 +26,12 @@ pub struct JavascriptComponent;
2626
impl JavascriptComponent {
2727
pub fn run(
2828
lang: Language,
29-
snippet: String,
3029
files: Vec<File>,
31-
stdin: Option<String>,
32-
args: Vec<String>,
33-
env: Vec<(String, String)>,
34-
constraints: Option<Limits>,
30+
snippet: String,
31+
options: RunOptions,
3532
) -> Result<ExecResult, Error> {
3633
let session = JavaScriptSession::new(lang, files);
37-
session.run(snippet, args, stdin, env, constraints)
34+
session.run(snippet, options)
3835
}
3936
}
4037

@@ -127,18 +124,11 @@ impl JavaScriptSession {
127124
}
128125
}
129126

130-
pub fn run(
131-
&self,
132-
snippet: String,
133-
args: Vec<String>,
134-
stdin: Option<String>,
135-
env: Vec<(String, String)>,
136-
constraints: Option<Limits>,
137-
) -> Result<ExecResult, Error> {
127+
pub fn run(&self, snippet: String, options: RunOptions) -> Result<ExecResult, Error> {
138128
ensure_language_is_supported(&self.lang)?;
139129
self.ensure_initialized()?;
140130

141-
block_on(async { self.run_async(snippet, args, stdin, env, constraints).await })
131+
block_on(async { self.run_async(snippet, options).await })
142132
}
143133

144134
fn ensure_initialized(&self) -> Result<(), Error> {
@@ -188,26 +178,26 @@ impl JavaScriptSession {
188178
}
189179

190180
#[allow(clippy::await_holding_refcell_ref)]
191-
async fn run_async(
192-
&self,
193-
snippet: String,
194-
args: Vec<String>,
195-
stdin: Option<String>,
196-
env: Vec<(String, String)>,
197-
constraints: Option<Limits>,
198-
) -> Result<ExecResult, Error> {
181+
async fn run_async(&self, snippet: String, options: RunOptions) -> Result<ExecResult, Error> {
199182
let maybe_state = self.state.borrow();
200183
let state = maybe_state.as_ref().unwrap();
201184
let start = Instant::now();
202185

203-
if let Some(constraints) = constraints {
186+
if let Some(constraints) = options.constraints {
204187
if let Some(memory_bytes) = constraints.memory_bytes {
205188
state.rt.set_memory_limit(memory_bytes as usize).await;
206189
}
207190
}
208191

209192
let abort_state = async_with!(state.ctx => |ctx| {
210-
set_globals(ctx.clone(), stdin, args, env, state.cwd.clone(), constraints.and_then(|c| c.file_size_bytes) ).map_err(js_engine_error)?;
193+
set_globals(
194+
ctx.clone(),
195+
options.stdin,
196+
options.args.unwrap_or_default(),
197+
options.env.unwrap_or_default(),
198+
state.cwd.clone(),
199+
options.constraints.and_then(|c| c.file_size_bytes),
200+
).map_err(js_engine_error)?;
211201
builtin::timeout::init_abort(ctx)
212202
})
213203
.await?;
@@ -229,7 +219,7 @@ impl JavaScriptSession {
229219
})
230220
.await
231221
};
232-
let result = if let Some(timeout_ms) = constraints.and_then(|c| c.time_ms) {
222+
let result = if let Some(timeout_ms) = options.constraints.and_then(|c| c.time_ms) {
233223
future
234224
.timeout(Duration::from_millis(timeout_ms))
235225
.map_err(|err| match err.kind() {

exec/exec/src/python/mod.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::durability::{EmptySnapshot, SessionSnapshot};
2-
use crate::golem::exec::executor::{Error, ExecResult, File, Language, Limits};
2+
use crate::golem::exec::executor::{Error, ExecResult, File, Language, RunOptions};
33
use crate::golem::exec::types::{LanguageKind, StageResult};
44
use crate::{get_contents_as_string, io_error, stage_result_failure};
55
use indoc::indoc;
@@ -33,15 +33,12 @@ pub struct PythonComponent;
3333
impl PythonComponent {
3434
pub fn run(
3535
lang: Language,
36-
snippet: String,
3736
files: Vec<File>,
38-
stdin: Option<String>,
39-
args: Vec<String>,
40-
env: Vec<(String, String)>,
41-
constraints: Option<Limits>,
37+
snippet: String,
38+
options: RunOptions,
4239
) -> Result<ExecResult, Error> {
4340
let session = PythonSession::new(lang, files);
44-
session.run(snippet, args, stdin, env, constraints)
41+
session.run(snippet, options)
4542
}
4643
}
4744

@@ -124,14 +121,7 @@ impl PythonSession {
124121
}
125122
}
126123

127-
pub fn run(
128-
&self,
129-
snippet: String,
130-
args: Vec<String>,
131-
stdin: Option<String>,
132-
env: Vec<(String, String)>,
133-
_constraints: Option<Limits>,
134-
) -> Result<ExecResult, Error> {
124+
pub fn run(&self, snippet: String, options: RunOptions) -> Result<ExecResult, Error> {
135125
self.ensure_initialized()?;
136126
ensure_language_is_supported(&self.lang)?;
137127

@@ -149,11 +139,13 @@ impl PythonSession {
149139
let scope = vm.new_scope_with_builtins();
150140
scope.globals.set_item(
151141
"__external_stdin",
152-
vm.new_pyobj(stdin.unwrap_or_default()),
142+
vm.new_pyobj(options.stdin.unwrap_or_default()),
153143
vm,
154144
)?;
155145

156-
let env_pairs = env
146+
let env_pairs = options
147+
.env
148+
.unwrap_or_default()
157149
.iter()
158150
.map(|(k, v)| vm.new_pyobj((k, v)))
159151
.collect::<Vec<_>>();
@@ -163,7 +155,14 @@ impl PythonSession {
163155

164156
scope.globals.set_item(
165157
"__argv",
166-
vm.new_pyobj(args.iter().map(|s| vm.new_pyobj(s)).collect::<Vec<_>>()),
158+
vm.new_pyobj(
159+
options
160+
.args
161+
.unwrap_or_default()
162+
.iter()
163+
.map(|s| vm.new_pyobj(s))
164+
.collect::<Vec<_>>(),
165+
),
167166
vm,
168167
)?;
169168

0 commit comments

Comments
 (0)