Created
May 10, 2024 04:09
-
-
Save tuulos/89cb6b8ca6fc6d974b3378f47ac2abce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { spawnSync, ChildProcessWithoutNullStreams } from "child_process"; | |
import { readFileSync } from 'fs'; | |
import tmp from 'tmp'; | |
tmp.setGracefulCleanup(); | |
function waitFlow(flowname: string, | |
runID: string, | |
resolve: (reason?: any) => void, | |
reject: (value?: any) => void) { | |
const cmd = spawnSync('python', [ | |
flowname + '.py', | |
'--quiet', | |
'argo-workflows', | |
'status', | |
runID | |
]); | |
if (cmd.error){ | |
console.log("Status failed: " + cmd.error); | |
reject("Status failed: " + cmd.error); | |
}else{ | |
const output = cmd.stderr; | |
if (output.includes("Succeeded")){ | |
console.log(`Run ${runID} succeeded`); | |
resolve(); | |
}else if (output.includes("Failed")){ | |
console.log(`Run ${runID} failed`); | |
reject("run failed"); | |
}else{ | |
setTimeout(() => waitFlow(flowname, runID, resolve, reject), 500); | |
} | |
} | |
} | |
async function triggerAndWait(flowname: string) { | |
const tmpFile = tmp.fileSync().name; | |
const cmd = spawnSync('python', [ | |
flowname + '.py', | |
'argo-workflows', | |
'trigger', | |
'--run-id-file', | |
tmpFile | |
]); | |
if (cmd.error){ | |
console.log("Trigger failed: " + cmd.error); | |
}else{ | |
const runID = readFileSync(tmpFile, { encoding: 'utf-8' }); | |
console.log(`Triggered run ${runID}`); | |
return new Promise((resolve, reject) => { | |
waitFlow(flowname, runID, resolve, reject); | |
}); | |
} | |
} | |
triggerAndWait("helloflow"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment