-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor-job.sh
executable file
·75 lines (56 loc) · 2.03 KB
/
monitor-job.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env bash
source fn_get_token.sh
BEARER_TOKEN=$(fn_get_token "$IDP_URL" "$AUTH_FILE")
if [ "$BEARER_TOKEN" == "null" ]
then
printf "Failed to retrieve bearer token is base64 token accurate?\nIs %s available from this computer?\n", $IDP_URL
exit 1
fi
JOB=$(cat "$DIRECTORY/jobId.txt")
if [ "$JOB" == "" ]
then
printf "Failed to retrieve job ID from jobId.txt\n"
exit 1
fi
echo "Id of job being monitored $JOB"
# Get the status
RESPONSE=$(curl "${API_URL}/Job/${JOB}/\$status" -sD - -H "accept: application/json" -H "Authorization: Bearer ${BEARER_TOKEN}")
URLS=$(echo "$RESPONSE" | grep ExplanationOfBenefit)
sleep 5
# If there are no results, wait x seconds and try again
while [ "$URLS" == '' ]
do
# If response is unauthorized refresh token and try again
HTTP_CODE=$(echo "$RESPONSE" | grep "HTTP/" | awk '{print $2}')
if [ "$HTTP_CODE" == 403 ]
then
echo "Token expired refreshing and then attempting to check status again"
BEARER_TOKEN=$(fn_get_token "$IDP_URL" "$AUTH_FILE")
sleep 30
RESPONSE=$(curl "${API_URL}/Job/${JOB}/\$status" -sD - -H "accept: application/json" -H "Authorization: Bearer ${BEARER_TOKEN}")
elif [ "$HTTP_CODE" != 202 ] && [ "$HTTP_CODE" != 200 ]
then
echo "Error making rest call $HTTP_CODE"
echo "Exiting without saving results"
exit 1
else
echo "$RESPONSE"
URLS=$(echo "$RESPONSE" | grep ExplanationOfBenefit)
# Sleep and increment counter
if [ "$URLS" == '' ]
then
sleep 60
COUNTER=$(( COUNTER +1 ))
# Log every ten seconds
if [ $((COUNTER % 10)) == 0 ]
then
echo "Running for $COUNTER minutes"
fi
RESPONSE=$(curl "${API_URL}/Job/${JOB}/\$status" -sD - -H "accept: application/json" -H "Authorization: Bearer ${BEARER_TOKEN}")
fi
fi
done
echo "Job finished with
$RESPONSE"
echo "$RESPONSE" > "$DIRECTORY/response.json"
echo "Saved response to $DIRECTORY/response.json"