Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trap sigterm to exit container faster #206

Closed
wants to merge 1 commit into from
Closed

Conversation

edaniszewski
Copy link
Contributor

fixes #205

Copy link
Contributor

@marcoceppi marcoceppi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We think there is a better way to do this, let me pull up some of my old run.sh files

@codecov
Copy link

codecov bot commented Sep 27, 2018

Codecov Report

Merging #206 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #206   +/-   ##
=======================================
  Coverage   82.76%   82.76%           
=======================================
  Files          44       44           
  Lines        1300     1300           
=======================================
  Hits         1076     1076           
  Misses        224      224

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c67ccad...3dcd5fb. Read the comment docs.

@edaniszewski
Copy link
Contributor Author

@marcoceppi I hope there is. This felt a little janky to me, but it worked, so its got that going for it.

@marcoceppi
Copy link
Contributor

It is embarrassing how annoying this is in Docker. I've used this in a lot of projects; just carrying it around from code to code.

#!/bin/bash

child=1

_handle_term() {
  echo "Caught SIGTERM, forwarding to pid $child"
  kill -TERM "$child" 2>/dev/null
}

_handle_usr2() {
  echo "Caught SIGUSR2, forwarding to pid $child"
  kill -USR2 "$child" 2>/dev/null
}

_handle_quit() {
  echo "Caught SIGQUIT, forwarding to pid $child"
  kill -QUIT "$child" 2>/dev/null
}

_handle_int() {
  exec_after_terminate=bash
  echo "Caught SIGINT, forwarding to pid $child"
  kill -INT "$child" 2>/dev/null
}

trap _handle_term SIGTERM
trap _handle_quit SIGQUIT
trap _handle_usr2 SIGUSR2
trap _handle_int SIGINT

wrap() {
  declare desc="Proxy signals to the child process. Invoke shell on SIGINT"
  echo "Starting the child process, watching for SIGINT"

  echo "\$ $@ &"
  $@ &
  child=$!

  echo "Child process started has pid $child"
  wait $child
  wait $child
  echo "Child process $child exited with status $?"

  if [ -n "$exec_after_terminate" ]
  then
    echo "Running exec_after_terminate: '$exec_after_terminate'"
    exec $exec_after_terminate
  fi
}

get-command() {
  declare desc="Get the command mapping for a command word"
  declare type="$1"

  echo "$1"
}

run-commands() {
  declare desc="Run command words"
  declare type="$1"

  if [ "$(eval echo $(get-command "$1"))" = "$1" ]; then
    # if the first command is the same as the first argument, then the command
    # was not found (or it was identical to the command)
    echo "Command word '$1' not found, running the whole string '$@' as a single command"

    cmd="$CMD_PREFIX$(eval echo $@)"
    exec $cmd
  else
    # loop through the command words and handle appropriately
    until [ -z "$1" ]
    do
      cmd="$CMD_PREFIX$(eval echo $(get-command "$1"))"

      if [ "$#" -gt "1" ]; then
        echo "Only one command word at a time"
        exit 1
      else
        echo "entrypoint: Command word is '"$1"', running '$cmd'"

        if [ "$$" = "1" ]
        then
          wrap "$cmd"
        else
          exec $cmd
        fi
      fi

      shift
    done
  fi
}

main() {
  declare desc="Kick off the entrypoint script"
  declare type="$@"

  if [ $# -eq 0 ]; then
    echo "No command words specified. Exiting"
  else
    run-commands "$@"
  fi
}

main "$@"

My Dockerfile ends up like this

ENTRYPOINT ["/code/run.sh", "python3"]
CMD ["app.py"]

@edaniszewski
Copy link
Contributor Author

closing this in preference of #207 (which also simplifies the image build process)

@edaniszewski edaniszewski deleted the trap-sigterm branch October 1, 2018 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

catch sigint/sigterm signal so container can terminate in a timely fashion
2 participants