spiff-arena/spiffworkflow-backend/bin/run_process_model_with_api

98 lines
3.6 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
function error_handler() {
echo >&2 "Exited with BAD EXIT CODE '${2}' in ${0} script at line: ${1}."
exit "$2"
}
trap 'error_handler ${LINENO} $?' ERR
set -o errtrace -o errexit -o nounset -o pipefail
script_dir="$(
cd -- "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
if [[ -z "${KEYCLOAK_BASE_URL:-}" ]]; then
export KEYCLOAK_BASE_URL="http://localhost:7002"
fi
if [[ -z "${BACKEND_BASE_URL:-}" ]]; then
export BACKEND_BASE_URL=http://localhost:7000
fi
process_model_identifier="${1:-}"
username="${2:-admin}"
password="${3:-admin}"
realm_name="${4:-spiffworkflow}"
if [[ -z "${1:-}" ]]; then
echo >&2 "usage: $(basename "$0") [process_model_identifier] [username: OPTONAL] [password: OPTONAL] [realm_name: OPTONAL]"
exit 1
fi
modified_process_model_identifier=$(tr '/' ':' <<<"$process_model_identifier")
function check_result_for_error() {
local result="$1"
error_code=$(jq '.error_code' <<<"$result")
if [[ -n "$error_code" && "$error_code" != "null" ]]; then
echo >&2 "ERROR: Failed to run process instance. Received error: $result"
exit 1
fi
}
function process_next_task() {
local task="$1"
if [[ -n "$task" && "$task" != "null" ]]; then
task_type=$(jq -r '.type' <<<"$task")
task_state=$(jq -r '.state' <<<"$task")
task_guid=$(jq -r '.id' <<<$"$task")
if grep -qE "Manual ?Task" <<<"$task_type" && [[ "${task_state}" == "READY" ]]; then
new_task=$(curl --silent -X PUT "${BACKEND_BASE_URL}/v1.0/tasks/${process_instance_id}/${task_guid}" -H "Authorization: Bearer $access_token")
check_result_for_error "$new_task"
process_next_task "$new_task"
elif grep -qE "User ?Task" <<<"$task_type" && [[ "${task_state}" == "READY" ]]; then
echo -e "\n\nThe next task is not a Manual Task and requires user input. It must be completed manually."
echo "$task"
exit
fi
fi
}
function check_on_proces_instance() {
local process_instance="$1"
status=$(jq -r '.status' <<<"$process_instance")
if [[ -n "$status" ]] && ! grep -qE '^(complete|error|suspendend)$' <<<"$status"; then
progress_result=$(curl --silent "${BACKEND_BASE_URL}/v1.0/tasks/progress/${process_instance_id}" -H "Authorization: Bearer $access_token")
task=$(jq '.task' <<<"$progress_result")
instructions=$(jq -r '.instructions' <<<"$progress_result")
new_process_instance=$(jq '.process_instance' <<<"$progress_result")
if [[ -n "$task" && "$task" != "null" ]]; then
process_next_task "$task"
elif [[ -n "$instructions" && "$instructions" != "null" && "$instructions" != '[]' ]]; then
echo "$instructions"
fi
sleep 1
check_on_proces_instance "$new_process_instance"
fi
}
access_token=$("${script_dir}/get_token" "$username" "$password" "$realm_name")
curl --silent -X POST "${BACKEND_BASE_URL}/v1.0/login_with_access_token?access_token=${access_token}" -H "Authorization: Bearer $access_token" >/dev/null
result=$(curl --silent -X POST "${BACKEND_BASE_URL}/v1.0/process-instances/${modified_process_model_identifier}" -H "Authorization: Bearer $access_token")
process_instance_id=$(jq -r '.id' <<<"$result")
if ! grep -qE '^[0-9]+$' <<<"$process_instance_id"; then
echo >&2 "ERROR: Did not receive valid process instance id when instantiating process model. result was ${result}"
exit 1
fi
result=$(curl --silent -X POST "${BACKEND_BASE_URL}/v1.0/process-instances/${modified_process_model_identifier}/${process_instance_id}/run" -H "Authorization: Bearer $access_token")
check_result_for_error "$result"
Feature/background proc with celery (#788) * WIP: some initial test code to test out celery w/ burnettk * some cleanup for celery and added base model to put tasks waiting on timers * removed dup bpmn file * some more cleanup and added strategy to queue instructions * some minor code changes w/ burnettk * remove the unused next_task key from api calls since nobody uses it w/ burnettk essweine * added migration for future tasks and added test to make sure we are inserting into it w/ burnettk essweine * ensure future task run at time can be updated w/ burnettk * added table to queue instructions for end user in w/ burnettk * added test to ensure we are storing instructions for end users w/ burnettk * added progress page to display new instructions to user * ignore dup instructions on db insert w/ burnettk * some more updates for celery w/ burnettk * some pyl and test fixes w/ burnettk * fixed tests w/ burnettk * WIP: added in page to show instructions on pi show page w/ burnettk * pi show page is fully using not interstitial now w/ burnettk * fixed broken test w/ burnettk * moved background processing items to own module w/ burnettk * fixed apscheduler start script * updated celery task queue to handle future tasks and upgraded black and set its line-length to match ruff w/ burnettk * added support to run future tasks using countdown w/ burnettk * build image for celery branch w/ burnettk * poet does not exist in the image w/ burnettk * start blocking scheduler should always start the scheduler w/ burnettk * add init and stuff for this branch * make this work not just on my mac * send other args to only * added running status for process instance and use that on fe to go to show page and added additional identifier to locking system to isolate celery workers better w/ burnettk * fixed typing error that typeguard found, not sure why mypy did not w/ burnettk * do not check for no instructions on interstitial page for cypress tests on frontend w/ burnettk * do not queue process instances twice w/ burnettk * removed bad file w/ burnettk * queue tasks using strings to avoid circular imports when attmepting to queue w/ burnettk * only queue imminent new timer events and mock celery * some keyboard shortcut support on frontend and added ability to force run a process instance over the api w/ burnettk * some styles added for the shortcut menu w/ burnettk * pyl w/ burnettk * fixed test w/ burnettk * removed temporary celery script and added support for celery worker in run server locally w/ burnettk * cleaned up migrations w/ burnettk * created new migration to clean up old migrations --------- Co-authored-by: jasquat <jasquat@users.noreply.github.com> Co-authored-by: burnettk <burnettk@users.noreply.github.com>
2023-12-05 16:41:59 +00:00
if [[ "$(jq -r '.status' <<<"$result")" == "complete" ]]; then
echo "Process instance completed"
exit 0
fi
check_on_proces_instance "$result"