Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 33 additions & 34 deletions amorphouspy_api/src/amorphouspy_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,41 +107,40 @@ def submit_to_executor(request_data: dict) -> dict:
"""
try:
# Create fresh executor to properly detect cached results
exe = get_executor(cache_directory=MELTQUENCH_PROJECT_DIR)

# Get LAMMPS-specific resource configuration
lammps_resource_dict = get_lammps_resource_dict()

# Submit the workflow - this returns a future for the final result
future = run_meltquench_workflow(
executor=exe,
components=request_data["components"],
values=request_data["values"],
n_atoms=request_data["n_atoms"],
potential_type=request_data["potential_type"],
heating_rate=request_data["heating_rate"],
cooling_rate=request_data["cooling_rate"],
n_print=request_data["n_print"],
lammps_resource_dict=lammps_resource_dict,
)
with get_executor(cache_directory=MELTQUENCH_PROJECT_DIR) as exe:
# Get LAMMPS-specific resource configuration
lammps_resource_dict = get_lammps_resource_dict()

# Submit the workflow - this returns a future for the final result
future = run_meltquench_workflow(
executor=exe,
components=request_data["components"],
values=request_data["values"],
n_atoms=request_data["n_atoms"],
potential_type=request_data["potential_type"],
heating_rate=request_data["heating_rate"],
cooling_rate=request_data["cooling_rate"],
n_print=request_data["n_print"],
lammps_resource_dict=lammps_resource_dict,
)

# Wait briefly for cache check to complete (happens in background thread)
# With wait=False, executorlib checks cache asynchronously
for _ in range(10): # Up to 1 second
if future.done():
break
time.sleep(0.1)

# Check if result is already available (from cache or completed)
if future.done() and not future.cancelled():
try:
result = future.result()
# Serialize using MeltquenchResult to handle ASE Atoms objects
serialized_result = MeltquenchResult(**result).model_dump()
return {"state": "complete", "result": serialized_result}
except Exception as e:
logger.exception("Job failed with exception")
return {"state": "error", "error": str(e)}
# Wait briefly for cache check to complete (happens in background thread)
# With wait=False, executorlib checks cache asynchronously
for _ in range(10): # Up to 1 second
if future.done():
break
time.sleep(0.1)

# Check if result is already available (from cache or completed)
if future.done() and not future.cancelled():
try:
result = future.result()
# Serialize using MeltquenchResult to handle ASE Atoms objects
serialized_result = MeltquenchResult(**result).model_dump()
return {"state": "complete", "result": serialized_result}
except Exception as e:
logger.exception("Job failed with exception")
return {"state": "error", "error": str(e)}

# Job is running in background
return {"state": "running"}
Expand Down
3 changes: 3 additions & 0 deletions amorphouspy_api/src/amorphouspy_api/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ def _task_to_dict(self, task: Task) -> dict[str, Any]:
if task.error_message:
task_dict["error"] = task.error_message

if task.request_data:
task_dict["request_data"] = task.request_data

return task_dict

def _update_task_from_dict(self, task: Task, task_data: dict[str, Any]) -> None:
Expand Down
11 changes: 3 additions & 8 deletions amorphouspy_api/src/amorphouspy_api/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
from executorlib.executor.single import TestClusterExecutor
from executorlib.api import TestClusterExecutor
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


logger = logging.getLogger(__name__)

Expand All @@ -40,7 +40,7 @@ def get_executor_class() -> type:
else:
# Use TestClusterExecutor for local - it supports wait=False
# (SingleNodeExecutor does not support wait=False)
from executorlib.executor.single import TestClusterExecutor
from executorlib.api import TestClusterExecutor
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return TestClusterExecutor

Expand Down Expand Up @@ -105,9 +105,4 @@ def get_executor(cache_directory: Path) -> "TestClusterExecutor":
cache_directory,
)

executor = executor_class(cache_directory=cache_directory, **executor_config)

# Enter context manager
executor.__enter__()

return executor
return executor_class(cache_directory=cache_directory, **executor_config)
17 changes: 15 additions & 2 deletions amorphouspy_api/src/tests/test_meltquench.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
def __init__(self, result: dict[str, Any]) -> None:
"""Initialize mock future with result."""
self._result = result
self._time = time.time()

def done(self) -> bool:
"""Return True to indicate job is complete."""
return True
if time.time() - self._time > 5:
return True
else:
return False

Check failure on line 31 in amorphouspy_api/src/tests/test_meltquench.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (SIM103)

amorphouspy_api/src/tests/test_meltquench.py:28:9: SIM103 Return the condition `time.time() - self._time > 5` directly

def cancelled(self) -> bool:
"""Return False to indicate job was not cancelled."""
Expand Down Expand Up @@ -154,7 +158,16 @@
task_id,
{
"state": "running",
"request_data": {"components": ["SiO2"], "values": [100.0], "unit": "wt"},
"request_data": {
"components": ["SiO2"],
"values": [100.0],
"unit": "wt",
"n_atoms": 3,
"potential_type": "test",
"heating_rate": 1e12,
"cooling_rate": 1e12,
"n_print": 100,
},
"request_hash": "test-hash-running",
},
)
Expand Down
Loading