Skip to content

Commit 38411da

Browse files
authored
Fix Argument list too long error (#84)
1 parent a400751 commit 38411da

File tree

5 files changed

+56
-29
lines changed

5 files changed

+56
-29
lines changed

src/services/simulation_service/core/connectors/common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
WellName: TypeAlias = str
1212
GridCell: TypeAlias = tuple[int, int, int]
1313
Point: TypeAlias = tuple[float, float, float]
14-
SerializedJson: TypeAlias = str
14+
JsonPath: TypeAlias = str
1515

1616

1717
class PerforationSchema(TypedDict):
@@ -76,5 +76,5 @@ class ConnectorInterface(ABC):
7676
@staticmethod
7777
@abstractmethod
7878
def run(
79-
config: SerializedJson, stop: threading.Event | None = None
79+
config: JsonPath, stop: threading.Event | None = None
8080
) -> tuple[SimulationStatus, SimulationResults]: ...

src/services/simulation_service/core/connectors/open_darts.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
from .common import (
2323
ConnectorInterface,
2424
GridCell,
25+
JsonPath,
2526
Point,
26-
SerializedJson,
2727
SimulationResults,
2828
SimulationResultType,
2929
SimulationStatus,
@@ -88,7 +88,7 @@ def open_darts_input_configuration_injector(func: Callable[..., None]) -> Any:
8888
The decorator should be used on top of the main function which trigger Open-Darts simulation
8989
Decorator allow to inject configuration json directly from the command line
9090
91-
Usage: python3 main.py (or other file name with open-darts simulation) 'configuration.json'
91+
Usage: python3 main.py (or other file name with open-darts simulation) <path-to-configuration.json>
9292
9393
example:
9494
@@ -101,25 +101,41 @@ def main(configuration_content):
101101
@wraps(func)
102102
def wrapper(*args: Any, **kwargs: Any) -> None:
103103
if len(sys.argv) < 2:
104-
logger.info("Usage: python main.py 'configuration.json (TBD)'")
104+
logger.info("Usage: python main.py <path-to-configuration.json>")
105105
sys.exit(1)
106106
return
107107
json_config_str = sys.argv[1]
108-
try:
109-
config = json.loads(json_config_str)
110-
except json.JSONDecodeError:
111-
logger.error("Invalid JSON input.")
112-
sys.exit(1)
113-
return
114-
115-
if not isinstance(config, dict):
116-
logger.error("Invalid JSON input.")
117-
sys.exit(1)
118-
return
119-
if not all(isinstance(k, str) for k in config.keys()):
120-
logger.error("Invalid JSON input.")
108+
if os.path.isfile(json_config_str):
109+
try:
110+
with open(json_config_str, "r") as f:
111+
config = json.load(f)
112+
if isinstance(config, str):
113+
config = json.loads(config)
114+
except Exception:
115+
logger.error("Failed to read configuration file.")
116+
sys.exit(1)
117+
return
118+
else:
119+
# For legacy reason, allowing passing json string directly
120+
try:
121+
config = json.loads(json_config_str)
122+
except json.JSONDecodeError:
123+
logger.error("Invalid JSON input.")
124+
sys.exit(1)
125+
return
126+
127+
except Exception:
128+
pass
129+
130+
if not isinstance(config, (dict, list)):
131+
logger.error(f"Invalid JSON input, got:{type(config).__name__}")
121132
sys.exit(1)
122133
return
134+
if isinstance(config, dict):
135+
if not all(isinstance(k, str) for k in config.keys()):
136+
logger.error(f"Invalid JSON input:{config}")
137+
sys.exit(1)
138+
return
123139

124140
func(config, *args, **kwargs)
125141

@@ -135,7 +151,7 @@ class OpenDartsConnector(ConnectorInterface):
135151

136152
@staticmethod
137153
def run(
138-
config: SerializedJson,
154+
config_path: JsonPath,
139155
stop: threading.Event | None = None,
140156
) -> tuple[SimulationStatus, SimulationResults]:
141157
# Choose runner implementation via environment. Default uses subprocess runner.
@@ -150,9 +166,9 @@ def run(
150166

151167
if runner_mode == "thread":
152168
thread_runner = ThreadRunner(subprocess_runner)
153-
return thread_runner.run(config, stop)
169+
return thread_runner.run(config_path, stop)
154170

155-
return subprocess_runner.run(config, stop)
171+
return subprocess_runner.run(config_path, stop)
156172

157173
@staticmethod
158174
def _get_broadcast_results(

src/services/simulation_service/core/connectors/runner.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from logger import get_logger, stream_reader
1515

1616
from .common import (
17-
SerializedJson,
17+
JsonPath,
1818
SimulationResults,
1919
SimulationResultType,
2020
SimulationStatus,
@@ -26,7 +26,7 @@
2626

2727
class SimulationRunner(Protocol):
2828
def run(
29-
self, config: SerializedJson, stop: threading.Event | None = None
29+
self, config: JsonPath, stop: threading.Event | None = None
3030
) -> Tuple[SimulationStatus, SimulationResults]: ...
3131

3232

@@ -47,7 +47,7 @@ def __init__(
4747
self._timeout_duration = get_timeout_value()
4848

4949
def run(
50-
self, config: SerializedJson, stop: threading.Event | None = None
50+
self, config: JsonPath, stop: threading.Event | None = None
5151
) -> Tuple[SimulationStatus, SimulationResults]:
5252
managed_factory = self._managed_subprocess_factory
5353
if managed_factory is None:
@@ -222,7 +222,7 @@ def __init__(self, subprocess_runner: SubprocessRunner | None = None):
222222
self._subprocess_runner = subprocess_runner or SubprocessRunner()
223223

224224
def run(
225-
self, config: SerializedJson, stop: threading.Event | None = None
225+
self, config: JsonPath, stop: threading.Event | None = None
226226
) -> Tuple[SimulationStatus, SimulationResults]:
227227
os.environ.setdefault("OPEN_DARTS_THREAD_MODE", "1")
228228
return self._subprocess_runner.run(config, stop)

src/services/simulation_service/core/infrastructure/worker/src/_simulation_worker_grpc_docker.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import asyncio
22
import io
3+
import json
34
import os
5+
import tempfile
46
import uuid
57
from zipfile import BadZipFile, ZipFile
68

@@ -27,7 +29,10 @@
2729

2830
def _run_simulator(simulation_job) -> tuple[SimulationStatus, SimulationResults]:
2931
connector = ConnectorFactory.get_connector(simulation_job.simulator)
30-
return connector.run(simulation_job.simulation.input.wells)
32+
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".json") as tf:
33+
json.dump(simulation_job.simulation.input.wells, tf)
34+
tf_path = tf.name
35+
return connector.run(tf_path)
3136

3237

3338
async def request_simulation_job(stub, worker_id):

src/services/simulation_service/core/infrastructure/worker/src/_simulation_worker_grpc_thread.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import asyncio
22
import io
3+
import json
34
import os
5+
import tempfile
46
import threading
57
import uuid
68
from contextlib import contextmanager
@@ -37,10 +39,14 @@
3739

3840

3941
def _run_simulator(
40-
simulation_job, stop_flag: threading.Event | None = None
42+
simulation_job,
43+
stop_flag: threading.Event | None = None,
4144
) -> Tuple[SimulationStatus, SimulationResults]:
4245
connector = ConnectorFactory.get_connector(simulation_job.simulator)
43-
return connector.run(simulation_job.simulation.input.wells, stop=stop_flag)
46+
with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".json") as tf:
47+
json.dump(simulation_job.simulation.input.wells, tf)
48+
tf_path = tf.name
49+
return connector.run(tf_path, stop=stop_flag)
4450

4551

4652
async def request_simulation_job(stub, worker_id):
@@ -64,7 +70,7 @@ async def submit_simulation_job(stub, simulation_job, simulation_result, status)
6470

6571

6672
async def handle_simulation_job(
67-
stub, simulation_job, worker_id, stop_flag: threading.Event | None = None
73+
stub, simulation_job, worker_id: str, stop_flag: threading.Event | None = None
6874
):
6975
"""
7076
Handle a single simulation job with proper error handling and logging.

0 commit comments

Comments
 (0)