def run_workflow(ctx, fullname, payload): """Execute a workflow""" try: wf = cel_workflows.get_by_name(fullname) payload = json.loads(payload) if "schema" in wf: try: validate(payload, wf["schema"]) except JsonValidationError as e: result = format_schema_errors(e) click.echo(f"Error: {result['error']}") for err in result["errors"]: click.echo(f"- {err}") raise click.Abort() except WorkflowNotFound as e: click.echo(f"Error: {e}") raise click.Abort() except (JSONDecodeError) as e: click.echo(f"Error in the payload : {e}") raise click.Abort() # Create the workflow object project, name = fullname.split(".") obj = Workflow(project=project, name=name, payload=payload) obj.save() # Build the canvas and execute it _workflow = WorkflowBuilder(obj.id) _workflow.run()
def _create_builder(project, name, payload, periodic=False, keys=KEYS_TO_REMOVE): with app.app_context(): obj = Workflow( project=project, name=name, payload=payload, periodic=periodic ) obj.save() data = obj.to_dict() wf = WorkflowBuilder(obj.id) wf.build() return _remove_keys(data), wf
def execute(workflow, payload): project, name = workflow.split(".") c_obj = Workflow(project=project, name=name, payload=payload, periodic=True) c_obj.save() # Build the workflow and execute it workflow = WorkflowBuilder(c_obj.id) workflow.run() return c_obj.to_dict()
def run_workflow(ctx, fullname, payload): """Execute a workflow""" try: cel_workflows.get_by_name(fullname) payload = json.loads(payload) except WorkflowNotFound as e: click.echo(f"Error: {e}") raise click.Abort() except JSONDecodeError as e: click.echo(f"Error parsing the JSON payload : {e}") raise click.Abort() # Create the workflow object project, name = fullname.split(".") obj = Workflow(project=project, name=name, payload=payload) obj.save() # Build the canvas and execute it _workflow = WorkflowBuilder(obj.id) _workflow.run()
def create_workflow(): data = request.get_json() project = data["project"] name = data["name"] fullname = f"{project}.{name}" # Check if the workflow exists try: cel_workflows.get_by_name(fullname) except WorkflowNotFound: return jsonify({"error": f"Workflow {fullname} not found"}), 404 # Create the workflow in DB obj = Workflow(project=project, name=name, payload=data["payload"]) obj.save() # Build the workflow and execute it data = obj.to_dict() workflow = WorkflowBuilder(obj.id) workflow.run() app.logger.info(f"Workflow ready : {workflow.canvas}") return jsonify(data), 201
def execute(workflow, payload): project, name = workflow.split(".") c_obj = Workflow(project=project, name=name, payload=payload, periodic=True) c_obj.save() # Build the workflow and execute it workflow = WorkflowBuilder(c_obj.id) workflow.run() c_obj_dict = c_obj.to_dict() # Force commit before ending the function to ensure the ongoing transaction # does not end up in a "idle in transaction" state on PostgreSQL c_obj.commit() return c_obj_dict
def test_list_paginated_workflows(app, client, no_worker): # SQLite db.Datetime() does not include microseconds, so # we need to fix dates to be able to sort workflows. now = datetime.now() with app.app_context(): for i in range(20): date = now + timedelta(minutes=i) w = Workflow( created_at=date, updated_at=date, project="example", name="WORKFLOW", payload={"i": i + 1}, ) db.session.add(w) db.session.commit() # DIRECTOR_WORKFLOWS_PER_PAGE=15 in tests/workflows/.env resp = client.get("/api/workflows") assert len(resp.json) == 15 # Last item resp = client.get("/api/workflows?per_page=1") assert len(resp.json) == 1 assert resp.json[0]["payload"]["i"] == 20 # Last X items resp = client.get("/api/workflows?per_page=4&page=1") assert len(resp.json) == 4 assert [w["payload"]["i"] for w in resp.json] == [20, 19, 18, 17] # Last X items, second page resp = client.get("/api/workflows?per_page=4&page=2") assert len(resp.json) == 4 assert [w["payload"]["i"] for w in resp.json] == [16, 15, 14, 13] # Remaining items resp = client.get("/api/workflows?per_page=3&page=7") assert len(resp.json) == 2 assert [w["payload"]["i"] for w in resp.json] == [2, 1] # Out of range resp = client.get("/api/workflows?per_page=4&page=6") assert resp.status_code == 404
def test_list_paginated_workflows(app, client, no_worker): # SQLite db.Datetime() does not include microseconds, so # we need to fix dates to be able to sort workflows. now = datetime.now() with app.app_context(): for i in range(10): date = now + timedelta(minutes=i) w = Workflow( created_at=date, updated_at=date, project="example", name="WORKFLOW", payload={"i": i + 1}, ) db.session.add(w) db.session.commit() resp = client.get("/api/workflows") assert len(resp.json) == 10 resp = client.get("/api/workflows?per_page=1") assert len(resp.json) == 1 assert resp.json[0]["payload"]["i"] == 10 resp = client.get("/api/workflows?per_page=4&page=1") assert len(resp.json) == 4 assert [w["payload"]["i"] for w in resp.json] == [10, 9, 8, 7] resp = client.get("/api/workflows?per_page=4&page=2") assert len(resp.json) == 4 assert [w["payload"]["i"] for w in resp.json] == [6, 5, 4, 3] resp = client.get("/api/workflows?per_page=4&page=3") assert len(resp.json) == 2 assert [w["payload"]["i"] for w in resp.json] == [2, 1] resp = client.get("/api/workflows?per_page=4&page=4") assert resp.status_code == 404
def _execute_workflow(project, name, payload={}): fullname = f"{project}.{name}" # Check if the workflow exists try: cel_workflows.get_by_name(fullname) except WorkflowNotFound: abort(404, f"Workflow {fullname} not found") # Create the workflow in DB obj = Workflow(project=project, name=name, payload=payload) obj.save() # Build the workflow and execute it data = obj.to_dict() workflow = WorkflowBuilder(obj.id) workflow.run() app.logger.info(f"Workflow sent : {workflow.canvas}") return obj.to_dict(), workflow