Created
March 29, 2021 03:55
-
-
Save onefoursix/bd4c4f3b99424feae2f03b2dd4e4d633 to your computer and use it in GitHub Desktop.
A bash script that stops a StreamSets Job for an Oracle CDC pipeline, waits for the Job to transition to an INACTIVE state and then starts the Job with the Runtime Parameter ORACLE_CDC_DICTIONARY_SOURCE set to either "DICT_FROM_ONLINE_CATALOG" or "DICT_FROM_REDO_LOGS"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
## restart-streamsets-oracle-cdc-job.sh | |
## A bash script that stops a StreamSets Job for an Oracle CDC pipeline, waits for the Job | |
## to transition to an INACTIVE state and then starts the Job with the Runtime Parameter | |
## ORACLE_CDC_DICTIONARY_SOURCE set to either "DICT_FROM_ONLINE_CATALOG" or "DICT_FROM_REDO_LOGS" | |
## | |
## If the Job is currently in an INACTIVE state, the Job is started immediately | |
## | |
## The Job must be configured with a Parameter named "ORACLE_CDC_DICTIONARY_SOURCE" that is | |
## set as the value used for the pipeline's Oracle CDC Dictionary Source property. | |
## | |
## This script must be called with two arguments: | |
## | |
## 1) the JOB_ID | |
## | |
## 2) the value for ORACLE_CDC_DICTIONARY_SOURCE which must be one of these two values: | |
## DICT_FROM_ONLINE_CATALOG | |
## DICT_FROM_REDO_LOGS | |
## | |
## Example usage: $ ./restart-streamsets-oracle-cdc-job.sh 3d314420-66ec-46f5-90dd-e3cb7c7e9678:schbrooks DICT_FROM_ONLINE_CATALOG | |
## | |
## Dependencies: | |
## | |
## jq must be installed - see https://stedolan.github.io/jq/ | |
## jq installation commands on RHEL7: | |
## sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm | |
## sudo yum install -y jq | |
## User variables ################################################ | |
# Control Hub User in the form <user>@<org> | |
SCH_USER= | |
# Control Hub Password | |
SCH_PASSWORD= | |
# Control Hub Base URL | |
SCH_BASE_URL=https://cloud.streamsets.com | |
# How long to sleep between status checks when | |
# waiting for Job to become INACTIVE | |
# Do not set SLEEP_SECONDS to a value less than 10 | |
SLEEP_SECONDS=10 | |
# Maximum wait time for Job to become INACTIVE (in seconds) | |
MAX_WAIT_SECONDS=3600 | |
## Command Line Args ################################################ | |
# Job ID | |
JOB_ID=$1 | |
# ORACLE_CDC_DICTIONARY_SOURCE | |
ORACLE_CDC_DICTIONARY_SOURCE=$2 | |
function usage() { | |
echo " | |
Error: wrong number of arguments or incorrect arguments | |
Usage: $0 <JOB_ID> <ORACLE_CDC_DICTIONARY_SOURCE> | |
The ORACLE_CDC_DICTIONARY_SOURCE argument must have one of these values: | |
DICT_FROM_ONLINE_CATALOG | |
DICT_FROM_REDO_LOGS | |
Example: $0 3d314420-66ec-46f5-90dd-e3cb7c7e9678:schbrooks DICT_FROM_ONLINE_CATALOG | |
" | |
exit -1 | |
} | |
if [ "$#" -ne 2 ]; then | |
usage | |
elif [ "$2" != "DICT_FROM_ONLINE_CATALOG" ] && [ "$2" != "DICT_FROM_REDO_LOGS" ]; then | |
usage | |
fi | |
## Get a Control Hub Auth Token ################################## | |
curl -X POST $SCH_BASE_URL/security/public-rest/v1/authentication/login \ | |
--header "Content-Type:application/json" --header "X-Requested-By:SDC" \ | |
-d "{\"userName\":\"$SCH_USER\", \"password\": \"$SCH_PASSWORD\"}" \ | |
-c cookie.txt | |
# Extract the auth token from the response | |
AUTH_TOKEN=$(cat cookie.txt | grep SSO | rev | grep -o '^\S*' | rev) | |
# Delete cookie | |
rm -rf cookie.txt | |
echo "" | |
JOB_STATUS="" | |
## Get the current status of the Job ############################# | |
CURRENT_STATUS_URL=$SCH_BASE_URL/jobrunner/rest/v1/job/$JOB_ID/currentStatus | |
RESPONSE=`curl -s -X GET $CURRENT_STATUS_URL \ | |
--header "X-SS-User-Auth-Token:$AUTH_TOKEN" \ | |
--header "Content-Type:application/json" \ | |
--header "X-Requested-By:SDC" \ | |
--header "X-SS-REST-CALL:true" \ | |
| jq '.status'` | |
# Check the status returned by Control Hub | |
if [ "$RESPONSE" = "\"INACTIVE\"" ]; then | |
JOB_STATUS="INACTIVE" | |
echo "`date`: Job $JOB_ID currently INACTIVE and will be started" | |
elif [ "$RESPONSE" = "\"ACTIVE\"" ]; then | |
JOB_STATUS="ACTIVE" | |
echo "`date`: Job $JOB_ID currently ACTIVE and will be stopped" | |
else | |
echo "`date`: Job $JOB_ID has an status other than \"ACTIVE\" or \"INACTIVE\"." | |
echo "`date`: Quitting without trying to restart Job" | |
exit -1 | |
fi | |
## Stop the Job (unless its status is already INACTIVE) ########################## | |
if [ "$JOB_STATUS" = "ACTIVE" ]; then | |
echo "`date`: Trying to stop Job $JOB_ID..." | |
STOP_JOB_URL=$SCH_BASE_URL/jobrunner/rest/v1/job/$JOB_ID/stop | |
RESPONSE=`curl -s -X POST $STOP_JOB_URL \ | |
--header "X-SS-User-Auth-Token:$AUTH_TOKEN" \ | |
--header "Content-Type:application/json" \ | |
--header "X-Requested-By:SDC" \ | |
--header "X-SS-REST-CALL:true" \ | |
-d '{}' \ | |
| jq '.status'` | |
# Check the status returned by Control Hub | |
if [ "$RESPONSE" = "\"DEACTIVATING\"" ]; then | |
echo "`date`: Job $JOB_ID DEACTIVATED" | |
else | |
echo "`date`: Error stopping Job $JOB_ID" | |
echo "`date`: Error message: "$RESPONSE | |
echo "`date`: Quitting without trying to start Job $JOB_ID" | |
exit -1 | |
fi | |
echo "`date`: Waiting for DEACTIVATED Job to transition to INACTIVE status..." | |
# Keep track of how long we have waited for Job to become INACTIVE | |
WAIT_SECONDS=0 | |
# Wait for stopped Job to transition to INACTIVE status | |
while [ ! "$JOB_STATUS" = "\"INACTIVE\"" ] | |
do | |
sleep $SLEEP_SECONDS | |
WAIT_SECONDS=$((WAIT_SECONDS+SLEEP_SECONDS)) | |
# Exit if we have waited too long | |
if [ "$WAIT_SECONDS" -gt "$MAX_WAIT_SECONDS" ]; then | |
echo "`date`: Error stopping Job $JOB_ID" | |
echo "`date`: Exceeded timeout of $MAX_WAIT_SECONDS seconds waiting for Job to transition to INACTIVE" | |
echo "`date`: Quitting without trying to start Job $JOB_ID" | |
exit -1 | |
fi | |
# Get Job Status from Control Hub | |
JOB_STATUS=`curl -s -X GET $CURRENT_STATUS_URL \ | |
--header "X-SS-User-Auth-Token:$AUTH_TOKEN" \ | |
--header "Content-Type:application/json" \ | |
--header "X-Requested-By:SDC" \ | |
--header "X-SS-REST-CALL:true" \ | |
| jq '.status'` | |
done | |
echo "`date`: Job $JOB_ID has transitioned to INACTIVE status" | |
fi | |
## Start the Job ########################################################## | |
JOB_PARAM_STRING="{\"ORACLE_CDC_DICTIONARY_SOURCE\":\"${ORACLE_CDC_DICTIONARY_SOURCE}\"}" | |
echo "`date`: Trying to start Job $JOB_ID with ORACLE_CDC_DICTIONARY_SOURCE = ${ORACLE_CDC_DICTIONARY_SOURCE}" | |
START_JOB_URL=$SCH_BASE_URL/jobrunner/rest/v1/job/$JOB_ID/start | |
RESPONSE=`curl -s -X POST $START_JOB_URL \ | |
--header "X-SS-User-Auth-Token:$AUTH_TOKEN" \ | |
--header "Content-Type:application/json" \ | |
--header "X-Requested-By:SDC" \ | |
--header "X-SS-REST-CALL:true" \ | |
-d ${JOB_PARAM_STRING} \ | |
| jq '.status'` | |
# Check the Job status returned by Control Hub | |
if [ "$RESPONSE" = "\"ACTIVATING\"" ]; then | |
echo "`date`: Job $JOB_ID successfully ACTIVATED" | |
exit 0 | |
else | |
echo "`date`: Error activating Job $JOB_ID" | |
exit -1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment