#!/bin/bash
# parameter 1: job type
# parameter 2: job config
# parameter 3: tenant id
# parameter 4: project id
# parameter 5: selectors

set -eu

job_type=${1}
job_config=${2}
tenant_id=""
project_id=""
selectors="[]"
job_id=""

if [ $# -ge 3 ]; then
	job_id=${3}
fi
if [ $# -ge 4 ]; then
	tenant_id=${4}
fi
if [ $# -ge 5 ]; then
	project_id=${5}
fi
if [ $# -ge 6 ]; then
	selectors=${6}
fi

echo -e "\ncreate job using: $job_id, $job_config \n" >/dev/stderr
config=$(base64 -i ${job_config} | tr -d \\n)
create_job_json=""
if [ -z "$job_id" ]; then
	create_job_json=$(echo "" | jq -R --arg TYPE "$job_type" --arg CONFIG "$config" --argjson SELECTORS "$selectors" \
		'{ type: $TYPE, config: $CONFIG, selectors: $SELECTORS }')
else
	create_job_json=$(echo "" | jq -R --arg JOBID "$job_id" --arg TYPE "$job_type" --arg CONFIG "$config" --argjson SELECTORS "$selectors" \
		'{ id: $JOBID, type: $TYPE, config: $CONFIG, selectors: $SELECTORS }')
fi

echo -e "\ncreate_job_json: $create_job_json \n" >/dev/stderr

retry_time=15
i=0
set +e
while [ $i -lt $retry_time ]; do
	output=$(curl -X POST -H "Content-Type: application/json" -d "$create_job_json" "http://127.0.0.1:10245/api/v1/jobs?tenant_id=${tenant_id}&project_id=${project_id})" | tee /dev/stderr)
	code=$(echo $output | jq -r .code)
	if [ "$code" == "null" ]; then
		job_id=$(echo $output | jq -r .id)
		break
	else
		((i++))
		echo -e "create job failed $i-th time, retry later, error: $output" >/dev/stderr
		sleep 2
	fi
done
set -e

if [ $i -ge $retry_time ]; then
	echo "create job failed at last"
	exit 1
else
	echo $job_id
fi
