github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/test/utils/create_job (about)

     1  #!/bin/bash
     2  # parameter 1: job type
     3  # parameter 2: job config
     4  # parameter 3: tenant id
     5  # parameter 4: project id
     6  # parameter 5: selectors
     7  
     8  set -eu
     9  
    10  job_type=${1}
    11  job_config=${2}
    12  tenant_id=""
    13  project_id=""
    14  selectors="[]"
    15  job_id=""
    16  
    17  if [ $# -ge 3 ]; then
    18  	job_id=${3}
    19  fi
    20  if [ $# -ge 4 ]; then
    21  	tenant_id=${4}
    22  fi
    23  if [ $# -ge 5 ]; then
    24  	project_id=${5}
    25  fi
    26  if [ $# -ge 6 ]; then
    27  	selectors=${6}
    28  fi
    29  
    30  echo -e "\ncreate job using: $job_id, $job_config \n" >/dev/stderr
    31  config=$(base64 -i ${job_config} | tr -d \\n)
    32  create_job_json=""
    33  if [ -z "$job_id" ]; then
    34  	create_job_json=$(echo "" | jq -R --arg TYPE "$job_type" --arg CONFIG "$config" --argjson SELECTORS "$selectors" \
    35  		'{ type: $TYPE, config: $CONFIG, selectors: $SELECTORS }')
    36  else
    37  	create_job_json=$(echo "" | jq -R --arg JOBID "$job_id" --arg TYPE "$job_type" --arg CONFIG "$config" --argjson SELECTORS "$selectors" \
    38  		'{ id: $JOBID, type: $TYPE, config: $CONFIG, selectors: $SELECTORS }')
    39  fi
    40  
    41  echo -e "\ncreate_job_json: $create_job_json \n" >/dev/stderr
    42  
    43  retry_time=15
    44  i=0
    45  set +e
    46  while [ $i -lt $retry_time ]; do
    47  	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)
    48  	code=$(echo $output | jq -r .code)
    49  	if [ "$code" == "null" ]; then
    50  		job_id=$(echo $output | jq -r .id)
    51  		break
    52  	else
    53  		((i++))
    54  		echo -e "create job failed $i-th time, retry later, error: $output" >/dev/stderr
    55  		sleep 2
    56  	fi
    57  done
    58  set -e
    59  
    60  if [ $i -ge $retry_time ]; then
    61  	echo "create job failed at last"
    62  	exit 1
    63  else
    64  	echo $job_id
    65  fi