github.com/apache/beam/sdks/v2@v2.48.2/python/scripts/run_expansion_services.sh (about)

     1  #!/bin/bash
     2  #
     3  #    Licensed to the Apache Software Foundation (ASF) under one or more
     4  #    contributor license agreements.  See the NOTICE file distributed with
     5  #    this work for additional information regarding copyright ownership.
     6  #    The ASF licenses this file to You under the Apache License, Version 2.0
     7  #    (the "License"); you may not use this file except in compliance with
     8  #    the License.  You may obtain a copy of the License at
     9  #
    10  #       http://www.apache.org/licenses/LICENSE-2.0
    11  #
    12  #    Unless required by applicable law or agreed to in writing, software
    13  #    distributed under the License is distributed on an "AS IS" BASIS,
    14  #    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  #    See the License for the specific language governing permissions and
    16  #    limitations under the License.
    17  #
    18  
    19  read -r -d '' USAGE <<END
    20  Usage: run_expansion_services.sh (start|stop) [options]
    21  Options:
    22    --group_id [unique id for stop services later]
    23    --java_expansion_service_jar [path to expansion service jar]
    24    --python_virtualenv_dir [path to virtualenv root dir]
    25    --python_expansion_service_module [name of expansion service module]
    26    --java_port [number]
    27    --python_port [number]
    28  END
    29  
    30  while [[ $# -gt 0 ]]; do
    31    key="$1"
    32    case $key in
    33      --group_id)
    34        GROUP_ID="$2"
    35        shift
    36        shift
    37        ;;
    38      --java_expansion_service_jar)
    39        JAVA_EXPANSION_SERVICE_JAR="$2"
    40        shift
    41        shift
    42        ;;
    43      --java_expansion_service_allowlist_file)
    44        JAVA_EXPANSION_SERVICE_ALLOWLIST_FILE="$2"
    45        shift
    46        shift
    47        ;;
    48      --python_expansion_service_allowlist_glob)
    49        PYTHON_EXPANSION_SERVICE_ALLOWLIST_GLOB="$2"
    50        shift
    51        shift
    52        ;;
    53      --python_virtualenv_dir)
    54        PYTHON_VIRTUALENV_DIR="$2"
    55        shift
    56        shift
    57        ;;
    58      --python_expansion_service_module)
    59        PYTHON_EXPANSION_SERVICE_MODULE="$2"
    60        shift
    61        shift
    62        ;;
    63      --java_port)
    64        JAVA_PORT="$2"
    65        shift
    66        shift
    67        ;;
    68      --python_port)
    69        PYTHON_PORT="$2"
    70        shift
    71        shift
    72        ;;
    73      start)
    74        STARTSTOP="$1"
    75        shift
    76        ;;
    77      stop)
    78        STARTSTOP="$1"
    79        shift
    80        ;;
    81      *)
    82        echo "Unknown option: $1"
    83        echo "$USAGE"
    84        exit 1
    85        ;;
    86    esac
    87  done
    88  
    89  FILE_BASE="beam-expansion-service"
    90  if [ -n "$GROUP_ID" ]; then
    91    FILE_BASE="$FILE_BASE-$GROUP_ID"
    92  fi
    93  
    94  TEMP_DIR=/tmp
    95  pid=$TEMP_DIR/$FILE_BASE.pid
    96  lock=$TEMP_DIR/$FILE_BASE.lock
    97  
    98  # Check whether flock exists since some OS distributions (like MacOS)
    99  # don't have it by default
   100  command -v flock >/dev/null 2>&1
   101  CHECK_FLOCK=$?
   102  
   103  if [[ $CHECK_FLOCK -eq 0 ]]; then
   104    exec 200>$lock
   105    if ! flock -n 200; then
   106      echo "script already running."
   107      exit 0
   108    fi
   109  fi
   110  
   111  case $STARTSTOP in
   112    start)
   113      if [ -f "$pid" ]; then
   114        echo "services already running."
   115        exit 0
   116      fi
   117  
   118      echo "Launching Java expansion service @ $JAVA_PORT"
   119      java -jar $JAVA_EXPANSION_SERVICE_JAR $JAVA_PORT --javaClassLookupAllowlistFile="$JAVA_EXPANSION_SERVICE_ALLOWLIST_FILE" >$TEMP_DIR/$FILE_BASE-java.log 2>&1 </dev/null &
   120      mypid=$!
   121      if kill -0 $mypid >/dev/null 2>&1; then
   122        echo $mypid >> $pid
   123      else
   124        echo "Can't start Java expansion service."
   125      fi
   126  
   127      echo "Launching Python expansion service @ $PYTHON_PORT"
   128      source $PYTHON_VIRTUALENV_DIR/bin/activate
   129      python -m $PYTHON_EXPANSION_SERVICE_MODULE -p $PYTHON_PORT --fully_qualified_name_glob "$PYTHON_EXPANSION_SERVICE_ALLOWLIST_GLOB" >$TEMP_DIR/$FILE_BASE-python.log 2>&1 </dev/null &
   130      mypid=$!
   131      if kill -0 $mypid >/dev/null 2>&1; then
   132        echo $mypid >> $pid
   133      else
   134        echo "Can't start Python expansion service."
   135      fi
   136      ;;
   137    stop)
   138      if [ -f "$pid" ]; then
   139        while read stop_pid; do
   140          if kill -0 $stop_pid >/dev/null 2>&1; then
   141            echo "Stopping expansion service pid: $stop_pid."
   142            kill $stop_pid
   143          else
   144            echo "Skipping invalid pid: $stop_pid."
   145          fi
   146        done < $pid
   147        rm $pid
   148      fi
   149      ;;
   150  esac
   151  
   152  if [[ $CHECK_FLOCK -eq 0 ]]; then
   153    flock -u 200
   154  fi