Skip to content

Commit

Permalink
pass data to ts workflow (#2049)
Browse files Browse the repository at this point in the history
  • Loading branch information
DatGuyJonathan authored Feb 19, 2025
1 parent 96fff12 commit cff2f16
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
6 changes: 5 additions & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
}
},
{
"label": "Moose lib build",
"label": "TS moose lib build",
"type": "shell",
"command": "pnpm --filter=@514labs/moose-lib run build",
"options": {
"cwd": "${workspaceRoot}"
}
},
{
"label": "Build all",
"dependsOn": ["CLI build", "TS moose lib build"]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ def __init__(self, temporal_client: TemporalClient):
# Test workflow executor in rust if this changes significantly
def execute(self, name: str, input_data: Any) -> Dict[str, Any]:
try:
asyncio.run(self._start_workflow_async(name, input_data))
run_id = asyncio.run(self._start_workflow_async(name, input_data))
print(f"WorkflowClient - started workflow: {name}")
return {
"status": 200,
"body": f"Workflow started: {name}"
"body": f"Workflow started: {name}. View it in the Temporal dashboard: http://localhost:8080/namespaces/default/workflows/{name}/{run_id}/history"
}
except Exception as e:
print(f"WorkflowClient - error while starting workflow: {e}")
Expand Down Expand Up @@ -160,7 +160,7 @@ async def _start_workflow_async(self, name: str, input_data: Any):
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSON input data: {e}")

await self.temporal_client.start_workflow(
workflow_handle = await self.temporal_client.start_workflow(
"ScriptWorkflow",
args=[f"{os.getcwd()}/app/scripts/{name}", input_data],
id=name,
Expand All @@ -169,6 +169,8 @@ async def _start_workflow_async(self, name: str, input_data: Any):
run_timeout=run_timeout
)

return workflow_handle.result_run_id

def load_consolidated_configs(self):
try:
file_path = os.path.join(os.getcwd(), ".moose", "workflow_configs.json")
Expand Down
6 changes: 4 additions & 2 deletions apps/framework-cli/src/framework/typescript/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ export interface ParsedActivity {

pub static TS_BASE_SCRIPT_TEMPLATE: &str = r#"import { TaskFunction, TaskDefinition } from "@514labs/moose-lib";
const {{name}}: TaskFunction = async () => {
// The initial input data and data passed between tasks can be
// defined in the task function parameter
const {{name}}: TaskFunction = async (input?: any) => {
// The body of your script goes here
console.log("Hello world from {{name}}");
Expand All @@ -187,7 +189,7 @@ const {{name}}: TaskFunction = async () => {
};
};
export default function createTask(input?: object) {
export default function createTask() {
return {
task: {{name}},
config: {
Expand Down
3 changes: 1 addition & 2 deletions packages/ts-moose-lib/src/scripts/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ export const activities = {

logger.info(`Task received input: ${JSON.stringify(inputData)}`);

// TODO: Handle initial input data & passing data between steps
const processedInput = (inputData || {})?.data || {};
const scriptModule = await require(scriptPath);
const execResult = await scriptModule.default();
const result = await execResult.task();
const result = await execResult.task(processedInput);

return result;
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-moose-lib/src/scripts/task.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface TaskFunction {
(): Promise<{ task: string; data: Record<string, any> }>;
(input?: any): Promise<{ task: string; data: any }>;
}

export interface TaskConfig {
Expand Down

0 comments on commit cff2f16

Please sign in to comment.