#!/bin/sh

openshift_tuned_socket=/var/lib/tuned/openshift-tuned.sock
export SYSTEMD_IGNORE_CHROOT=1

start() {
  # Tuned can take ~20s to reload/start when "ulimit -Sn == 1048576".
  # See:
  # - https://github.com/redhat-performance/tuned/issues/146
  # - https://www.python.org/dev/peps/pep-0446/#closing-all-open-file-descriptors
  # - http://bugs.python.org/issue1663329
  ulimit -Sn 1024	# workaround for the issue above

  openshift-tuned \
    -v=1 \
    ${OCP_NODE_NAME}
}

stop() {
  local timeout=10	# wait $timeout [s] for a reply via the socket
  local response=$(echo stop | socat -t$timeout - UNIX-CONNECT:$openshift_tuned_socket 2>/dev/null)

  if [ "$response" != "ok" ]; then
    # provide a failure message in the event log
    echo "openshift-tuned stop response: $response" 1>&2
    return 1
  fi
}

$@
