Unify batch and simulation task handling with TaskFactory pattern #3016
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Addresses review feedback on PR #2978 to eliminate redundant API calls and unify task handling between
SimulationTaskandBatchTask.Changes
TaskFactory.get()that returns the appropriateWebTasksubclass (SimulationTaskorBatchTask) based on task type, with registry caching to avoid repeated API callsget_log(),get_data_hdf5(),get_url(),is_batch()) into sharedWebTaskbase classdownload(),get_info(),start(),abort(),monitor(), etc.) to useTaskFactory.get()instead of_is_modeler_batch()checks, eliminating redundant HTTP callsclear_task_factory_registry()autouse fixture to prevent stateful test dependenciesNotImplementedErrorfor batch-unsupported operations (get_run_info(),download_json(),load_simulation(),download_log(),delete(),download_simulation())compose_modeler()function in favor ofTidy3dBaseModel.from_file(), exposedTidy3dBaseModelin public APIArchitecture
Before:
After:
The factory maintains a registry mapping
task_id → task_typeto eliminate redundant type-checking API calls across webapi function calls.💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
Greptile Overview
Greptile Summary
This PR introduces a
TaskFactorypattern to unify handling ofSimulationTaskandBatchTask, eliminating redundant API calls through registry caching. The refactoring extracts common operations into aWebTaskbase class and updates all webapi functions to useTaskFactory.get()instead of manual type checking.Key Changes
TaskFactory.get()that returns the appropriateWebTasksubclass based on task type, with registry caching to avoid repeated API callsget_log(),get_data_hdf5(),get_url(),is_batch()) into base class inherited by bothSimulationTaskandBatchTaskdownload(),get_info(),start(),abort(),monitor()) now useTaskFactory.get()instead of_is_modeler_batch()checksTaskFactoryregistry between testsIssues Found
BatchTask.submit()acceptspay_typeandpriorityparameters but raisesNotImplementedErrorinstead of sending them to the API. This breaks the unified interface and could confuse users who expect these parameters to work (tidy3d/web/core/task_core.py:896-940)TaskFactory.get()makes redundant HTTP calls when task not in registry - checksis_batch(), registers, then calls.get()which fetches again (tidy3d/web/core/task_core.py:978-991)TaskFactory.reset()call in test fixture (tidy3d/web/tests/conftest.py:12-13)Confidence Score: 3/5
BatchTask.submit()acceptspay_type/priorityparameters but raisesNotImplementedErrorinstead of using them, breaking API contract and potentially causing silent failures or confusing errors. (2) Inefficiency inTaskFactory.get()making redundant API calls defeats the caching optimization goal. (3) The refactoring itself is well-structured with good separation of concerns and the test fixture addition is appropriate. However, the critical parameter handling issue must be fixed.tidy3d/web/core/task_core.py- fix theBatchTask.submit()parameter handling issue and optimizeTaskFactory.get()to avoid redundant HTTP callsImportant Files Changed
File Analysis
WebTaskbase class,TaskFactorypattern with registry caching, unified task operations. Critical issue:BatchTask.submit()accepts but doesn't sendpay_type/priorityto API. Inefficiency inTaskFactory.get()making redundant HTTP calls.TaskFactory.get()instead of manual type checking, eliminating redundant API calls. Clean unified interface acrossdownload(),get_info(),start(),abort(),monitor()etc.TaskFactoryregistry before/after each test. Minor issue: duplicate reset call at line 12-13.Sequence Diagram
sequenceDiagram participant User participant webapi participant TaskFactory participant SimulationTask participant BatchTask participant API as Server API User->>webapi: upload(simulation) webapi->>WebTask: create(task_type, task_name) alt RF Task Type WebTask->>API: POST rf/task API-->>WebTask: batchId/groupId else Standard Task Type WebTask->>API: POST tidy3d/projects/{folder}/tasks API-->>WebTask: taskId end WebTask->>API: Upload simulation to S3 webapi->>User: return task_id User->>webapi: start(task_id) webapi->>TaskFactory: get(task_id) alt task_id in registry cache TaskFactory-->>webapi: return cached task type else task_id not in registry TaskFactory->>API: GET rf/task/{id}/statistics alt is batch TaskFactory->>TaskFactory: register(task_id, "batch") TaskFactory->>BatchTask: get(task_id) BatchTask->>API: GET rf/task/{id}/statistics BatchTask-->>TaskFactory: BatchTask instance else is simulation TaskFactory->>SimulationTask: get(task_id) SimulationTask->>API: GET tidy3d/tasks/{id}/detail SimulationTask-->>TaskFactory: SimulationTask instance TaskFactory->>TaskFactory: register(task_id, "simulation") end end TaskFactory-->>webapi: WebTask instance alt BatchTask webapi->>BatchTask: submit(solver_version, worker_group) Note right of BatchTask: pay_type/priority NOT sent BatchTask->>API: POST rf/task/{id}/submit else SimulationTask webapi->>SimulationTask: submit(solver_version, worker_group, pay_type, priority) SimulationTask->>API: POST tidy3d/tasks/{id}/submit end User->>webapi: monitor(task_id) webapi->>TaskFactory: get(task_id) Note right of TaskFactory: Uses registry cache (no extra HTTP call) TaskFactory-->>webapi: WebTask instance loop Poll Status alt BatchTask webapi->>BatchTask: detail() BatchTask->>API: GET rf/task/{id}/statistics else SimulationTask webapi->>SimulationTask: get_running_info() SimulationTask->>API: GET tidy3d/tasks/{id}/progress end end User->>webapi: download(task_id) webapi->>TaskFactory: get(task_id) TaskFactory-->>webapi: WebTask instance alt BatchTask webapi->>BatchTask: get_data_hdf5(CM_DATA_HDF5_GZ) BatchTask->>API: Download from S3 else SimulationTask webapi->>SimulationTask: get_data_hdf5(SIMULATION_DATA_HDF5_GZ) SimulationTask->>API: Download from S3 end