0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-23 16:37:52 +01:00
posthog/bin/plugin-server
Brett Hoerner 5b5d0d43a3
chore(plugin-server): make it easier to run multiple plugin-server instances locally (#17456)
* chore(plugin-server): allow customizing the HTTP server port

* chore(plugin-server): add NO_WATCH mode for development

* fix: http-server test
2023-09-15 08:55:46 -06:00

74 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
while test $# -gt 0; do
case "$1" in
-h | --help)
echo "USAGE:"
echo " bin/plugin-server [FLAGS]"
echo " "
echo "FLAGS:"
echo " -h, --help Print this help information."
echo " --no-restart-loop Run without restart loop. Recommended when deferring resiliency to e.g. docker-compose."
exit 0
;;
--no-restart-loop)
NO_RESTART_LOOP='true'
shift
;;
*)
break
;;
esac
done
export BASE_DIR=$(dirname $(dirname "$PWD/${0#./}"))
export KAFKA_HOSTS=${KAFKA_HOSTS:-'kafka:9092'}
if [[ -n $INJECT_EC2_CLIENT_RACK ]]; then
# To avoid cross-AZ Kafka traffic, set KAFKA_CLIENT_RACK from the EC2 metadata endpoint.
# TODO: switch to the downwards API when https://github.com/kubernetes/kubernetes/issues/40610 is released
TOKEN=$(curl --max-time 0.1 -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
export KAFKA_CLIENT_RACK=$(curl --max-time 0.1 -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/placement/availability-zone-id)
fi
./bin/migrate-check
cd plugin-server
if [[ -n $DEBUG ]]; then
echo "🧐 Verifying installed packages..."
pnpm i --frozen-lockfile
fi
if [ $? -ne 0 ]; then
echo "💥 Verification failed!"
exit 1
fi
if [[ -n $DEBUG ]]; then
if [[ -n $NO_WATCH ]]; then
cmd="pnpm start:devNoWatch"
else
cmd="pnpm start:dev"
fi
else
cmd="node dist/index.js"
fi
if [[ -n $NO_RESTART_LOOP ]]; then
echo "▶️ Starting plugin server..."
trap 'kill -TERM $child 2>/dev/null; while kill -0 $child 2>/dev/null; do sleep 1; done' EXIT
$cmd &
child=$!
wait $child
else
echo "🔁 Starting plugin server in a resiliency loop..."
while true; do
$cmd
echo "💥 Plugin server crashed!"
echo "⌛️ Waiting 2 seconds before restarting..."
sleep 2
done
fi