github.com/apache/beam/sdks/v2@v2.48.2/python/scripts/run_job_server.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_job_server.sh (start|stop) [options]
    21  Options:
    22    --group_id [unique id for stop services later]
    23    --job_port [port for job endpoint, default 8099]
    24    --artifact_port [port for artifact service, default 8098]
    25    --job_server_jar [path to job server jar]
    26  END
    27  
    28  JOB_PORT=8099
    29  ARTIFACT_PORT=8098
    30  
    31  while [[ $# -gt 0 ]]; do
    32    key="$1"
    33    case $key in
    34      --group_id)
    35        GROUP_ID="$2"
    36        shift
    37        shift
    38        ;;
    39      --job_port)
    40        JOB_PORT="$2"
    41        shift
    42        shift
    43        ;;
    44      --artifact_port)
    45        ARTIFACT_PORT="$2"
    46        shift
    47        shift
    48        ;;
    49      --job_server_jar)
    50        JOB_SERVER_JAR="$2"
    51        shift
    52        shift
    53        ;;
    54      --additional_args)
    55        ADDITIONAL_ARGS="$2"
    56        shift
    57        shift
    58        ;;
    59      start)
    60        STARTSTOP="$1"
    61        shift
    62        ;;
    63      stop)
    64        STARTSTOP="$1"
    65        shift
    66        ;;
    67      *)
    68        echo "Unknown option: $1"
    69        echo "$USAGE"
    70        exit 1
    71        ;;
    72    esac
    73  done
    74  
    75  FILE_BASE="beam-job-server"
    76  if [ -n "$GROUP_ID" ]; then
    77    FILE_BASE="$FILE_BASE-$GROUP_ID"
    78  fi
    79  
    80  TEMP_DIR=/tmp
    81  pid=$TEMP_DIR/$FILE_BASE.pid
    82  lock=$TEMP_DIR/$FILE_BASE.lock
    83  
    84  # Check whether flock exists since some OS distributions (like MacOS)
    85  # don't have it by default
    86  command -v flock >/dev/null 2>&1
    87  CHECK_FLOCK=$?
    88  
    89  if [[ $CHECK_FLOCK -eq 0 ]]; then
    90    exec 200>$lock
    91    if ! flock -n 200; then
    92      echo "script already running."
    93      exit 0
    94    fi
    95  fi
    96  
    97  case $STARTSTOP in
    98    start)
    99      if [ -f "$pid" ]; then
   100        echo "services already running."
   101        exit 0
   102      fi
   103  
   104      echo "Launching job server @ $JOB_PORT ..."
   105      java -jar $JOB_SERVER_JAR --job-port=$JOB_PORT --artifact-port=$ARTIFACT_PORT --expansion-port=0 $ADDITIONAL_ARGS >$TEMP_DIR/$FILE_BASE.log 2>&1 </dev/null &
   106      mypid=$!
   107      if kill -0 $mypid >/dev/null 2>&1; then
   108        echo $mypid >> $pid
   109      else
   110        echo "Can't start job server."
   111      fi
   112      ;;
   113    stop)
   114      if [ -f "$pid" ]; then
   115        while read stop_pid; do
   116          if kill -0 $stop_pid >/dev/null 2>&1; then
   117            echo "Stopping job server pid: $stop_pid."
   118            kill $stop_pid
   119          else
   120            echo "Skipping invalid pid: $stop_pid."
   121          fi
   122        done < $pid
   123        rm $pid
   124      fi
   125      ;;
   126  esac
   127  
   128  if [[ $CHECK_FLOCK -eq 0 ]]; then
   129    flock -u 200
   130  fi