|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2019 Cortex Labs, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -eou pipefail |
| 18 | + |
| 19 | +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. >/dev/null && pwd)" |
| 20 | +CORTEX="$ROOT/bin/cortex" |
| 21 | + |
| 22 | +for example in $ROOT/examples/*/app.yaml; do |
| 23 | + timer=1200 |
| 24 | + example_base_dir=$(dirname "${example}") |
| 25 | + retry="false" |
| 26 | + |
| 27 | + cd $example_base_dir |
| 28 | + echo "Deploying $example_base_dir" |
| 29 | + $CORTEX refresh |
| 30 | + |
| 31 | + api_names="$($CORTEX get api | sed '1,2d' | sed '/^$/d' | tr -s ' ' | cut -f 1 -d " ")" |
| 32 | + sample="$(find . -name "*.json")" |
| 33 | + |
| 34 | + while true; do |
| 35 | + current_status="$($CORTEX status)" |
| 36 | + echo "$current_status" |
| 37 | + |
| 38 | + error_count="$(echo $current_status | { grep "error" || test $? = 1; } | wc -l)" |
| 39 | + # accomodate transient error `error: failed to connect to operator...` |
| 40 | + if [ $error_count -gt "0" ] && [[ ! $current_status =~ ^error\:\ failed\ to\ connect\ to\ the\ operator.* ]]; then |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + |
| 44 | + ready_count="$($CORTEX get api | sed '1,2d' | sed '/^$/d' | { grep "ready" || test $? = 1; } | wc -l)" |
| 45 | + total_count="$($CORTEX get api | sed '1,2d' | sed '/^$/d' | wc -l)" |
| 46 | + |
| 47 | + sleep 15 # account for API startup delay |
| 48 | + |
| 49 | + if [ "$ready_count" == "$total_count" ] && [ $total_count -ne "0" ]; then |
| 50 | + for api_name in $api_names; do |
| 51 | + echo "Running cx predict $api_name $sample" |
| 52 | + result="$($CORTEX predict $api_name $sample)" |
| 53 | + prediction_exit_code=$? |
| 54 | + echo "$result" |
| 55 | + if [ $prediction_exit_code -ne 0 ]; then |
| 56 | + # accomodate transient error `error: failed to connect to operator...` |
| 57 | + # handle `error: api ... is updating` error caused when the API status is set to `ready` but it actually isn't |
| 58 | + if [[ $result =~ ^error\:\ failed\ to\ connect\ to\ the\ operator.* ]] || [[ $result =~ ^error\:\ api.*is\ updating$ ]]; then |
| 59 | + echo "retrying prediction..." |
| 60 | + $retry="true" |
| 61 | + break # skip request predictions from the remaining APIs and try again |
| 62 | + else |
| 63 | + echo "prediction failed" |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | + fi |
| 67 | + done |
| 68 | + |
| 69 | + if [ "$retry" == "false" ]; then |
| 70 | + break # successfully got predictions from all APIs for this example, move into the next |
| 71 | + else |
| 72 | + retry="false" |
| 73 | + fi |
| 74 | + fi |
| 75 | + |
| 76 | + timer=$((timer-15)) |
| 77 | + echo "Running $example_base_dir. $timer seconds left before timing out." |
| 78 | + if [ $timer -lt "0" ]; then |
| 79 | + echo "timed out!" |
| 80 | + exit 1 |
| 81 | + fi |
| 82 | + done |
| 83 | + |
| 84 | + $CORTEX delete |
| 85 | +done |
| 86 | + |
| 87 | +echo "Ran all examples successfully." |
0 commit comments