|
| 1 | +import uuid |
| 2 | +import subprocess |
| 3 | +import os |
| 4 | +from flowcept.flowcept_api.flowcept_controller import Flowcept |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +def start_workflow(): |
| 8 | + """Initialize and start the Flowcept workflow.""" |
| 9 | + workflow_id = str(uuid.uuid4()) |
| 10 | + flowcept_instance = Flowcept( |
| 11 | + workflow_id=workflow_id, |
| 12 | + workflow_name="Test-Workflow", |
| 13 | + bundle_exec_id=workflow_id |
| 14 | + ) |
| 15 | + flowcept_instance.start() |
| 16 | + return workflow_id, flowcept_instance |
| 17 | + |
| 18 | + |
| 19 | +def run_task(workflow_id, script="simple_task.py"): |
| 20 | + """Run an external Python script with the given workflow ID.""" |
| 21 | + process = subprocess.Popen( |
| 22 | + ["python", script, workflow_id], |
| 23 | + stdout=subprocess.PIPE, |
| 24 | + stderr=subprocess.PIPE, |
| 25 | + text=True |
| 26 | + ) |
| 27 | + |
| 28 | + for line in iter(process.stdout.readline, ""): |
| 29 | + print(line, end="") |
| 30 | + |
| 31 | + process.stdout.close() |
| 32 | + process.wait() |
| 33 | + |
| 34 | + |
| 35 | +def main(): |
| 36 | + """Main function to manage workflow execution.""" |
| 37 | + |
| 38 | + parent_dir = Path(__file__).resolve().parent |
| 39 | + script = os.path.join(parent_dir, "simple_task.py") |
| 40 | + |
| 41 | + workflow_id, f = start_workflow() |
| 42 | + |
| 43 | + try: |
| 44 | + run_task(workflow_id, script=script) |
| 45 | + finally: |
| 46 | + f.stop() |
| 47 | + |
| 48 | + return workflow_id |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == "__main__": |
| 52 | + workflow_id = main() |
| 53 | + tasks = Flowcept.db.query({"workflow_id": workflow_id}) |
| 54 | + assert len(tasks) == 1 |
| 55 | + print(f"There is one task for the workflow {workflow_id}.") |
0 commit comments