49 lines
1.5 KiB
TypeScript
49 lines
1.5 KiB
TypeScript
import type {
|
|
ExecutionContext,
|
|
ExecutorExecutionResult,
|
|
TaskRecord,
|
|
} from "../contracts/execution-context.ts";
|
|
|
|
export class HttpExecutor {
|
|
executionCount = 0;
|
|
|
|
async execute(task: TaskRecord, context: ExecutionContext): Promise<ExecutorExecutionResult> {
|
|
this.executionCount += 1;
|
|
|
|
const url = typeof task.executorConfig?.url === "string" ? task.executorConfig.url : undefined;
|
|
if (url) {
|
|
const method = typeof task.executorConfig?.method === "string" ? task.executorConfig.method : "POST";
|
|
const response = await fetch(url, {
|
|
method,
|
|
headers: {
|
|
"content-type": "application/json",
|
|
},
|
|
body: JSON.stringify({
|
|
task,
|
|
context,
|
|
}),
|
|
});
|
|
const responseText = await response.text();
|
|
if (!response.ok) {
|
|
throw Object.assign(new Error(`http executor request failed: ${response.status}`), {
|
|
stdoutLines: [],
|
|
stderrLines: responseText ? [responseText] : [],
|
|
});
|
|
}
|
|
|
|
const payload = responseText ? JSON.parse(responseText) as ExecutorExecutionResult : { result: null };
|
|
return {
|
|
result: payload.result,
|
|
stdoutLines: payload.stdoutLines ?? [`http executor called ${url}`],
|
|
stderrLines: payload.stderrLines ?? [],
|
|
};
|
|
}
|
|
|
|
return {
|
|
result: { taskId: task.id, executor: "http" as const },
|
|
stdoutLines: [`http executor processed ${task.nodeId}`],
|
|
stderrLines: [],
|
|
};
|
|
}
|
|
}
|