Skip to content

Commit c207961

Browse files
committed
Add better logging to the run-development script
1 parent ff24de1 commit c207961

File tree

3 files changed

+104
-28
lines changed

3 files changed

+104
-28
lines changed

docker/app/Dockerfile

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ RUN (echo "https://github.com/philomena-dev/prebuilt-ffmpeg/raw/master"; cat /et
55
&& cp /tmp/repositories /etc/apk/repositories \
66
&& apk update --allow-untrusted \
77
&& apk add --allow-untrusted \
8+
bash \
89
inotify-tools \
910
build-base \
1011
git \
@@ -39,10 +40,15 @@ RUN git clone --depth 1 https://github.com/philomena-dev/mediatools /tmp/mediato
3940
&& cd /tmp/mediatools \
4041
&& make -j$(nproc) install
4142

42-
COPY docker/app/run-development /usr/local/bin/run-development
43-
COPY docker/app/run-test /usr/local/bin/run-test
44-
COPY docker/app/safe-rsvg-convert /usr/local/bin/safe-rsvg-convert
45-
COPY docker/app/purge-cache /usr/local/bin/purge-cache
46-
ENV PATH=$PATH:/root/.cargo/bin
43+
COPY scripts/lib.sh /var/philomena/scripts/
44+
45+
COPY \
46+
docker/app/run-development \
47+
docker/app/run-test \
48+
docker/app/safe-rsvg-convert \
49+
docker/app/purge-cache \
50+
/var/philomena/docker/app/
51+
52+
ENV PATH=$PATH:/root/.cargo/bin:/var/philomena/docker/app
4753
EXPOSE 5173
4854
CMD run-development

docker/app/run-development

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,70 @@
1-
#!/usr/bin/env sh
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
. "$(dirname "${BASH_SOURCE[0]}")/../../scripts/lib.sh"
26

37
# Create S3 dirs
4-
mkdir -p /srv/philomena/priv/static/system/images/thumbs
5-
mkdir -p /srv/philomena/priv/s3/philomena
6-
ln -sf /srv/philomena/priv/static/system/images/thumbs /srv/philomena/priv/s3/philomena/images
7-
ln -sf /srv/philomena/priv/static/system/images /srv/philomena/priv/s3/philomena/adverts
8-
ln -sf /srv/philomena/priv/static/system/images /srv/philomena/priv/s3/philomena/avatars
9-
ln -sf /srv/philomena/priv/static/system/images /srv/philomena/priv/s3/philomena/badges
10-
ln -sf /srv/philomena/priv/static/system/images /srv/philomena/priv/s3/philomena/tags
8+
step mkdir -p /srv/philomena/priv/static/system/images/thumbs
9+
step mkdir -p /srv/philomena/priv/s3/philomena
10+
step ln -sf /srv/philomena/priv/static/system/images/thumbs /srv/philomena/priv/s3/philomena/images
11+
step ln -sf /srv/philomena/priv/static/system/images /srv/philomena/priv/s3/philomena/adverts
12+
step ln -sf /srv/philomena/priv/static/system/images /srv/philomena/priv/s3/philomena/avatars
13+
step ln -sf /srv/philomena/priv/static/system/images /srv/philomena/priv/s3/philomena/badges
14+
step ln -sf /srv/philomena/priv/static/system/images /srv/philomena/priv/s3/philomena/tags
1115

1216
# For compatibility with musl libc
1317
export CARGO_FEATURE_DISABLE_INITIAL_EXEC_TLS=1
1418
export CARGO_HOME=/srv/philomena/.cargo
1519

1620
background() {
21+
# Log the execution with (bg-jobs) prefix to differentiate it from the main process
22+
export TASK="bg-jobs"
23+
1724
while :; do
18-
mix run -e 'Philomena.Release.update_channels()' > /dev/null
19-
mix run -e 'Philomena.Release.verify_artist_links()' > /dev/null
20-
mix run -e 'Philomena.Release.update_stats()' > /dev/null
21-
mix run -e 'Philomena.Release.clean_moderation_logs()' > /dev/null
22-
mix run -e 'Philomena.Release.generate_autocomplete()' > /dev/null
25+
step mix run -e 'Philomena.Release.update_channels()' > /dev/null
26+
step mix run -e 'Philomena.Release.verify_artist_links()' > /dev/null
27+
step mix run -e 'Philomena.Release.update_stats()' > /dev/null
28+
step mix run -e 'Philomena.Release.clean_moderation_logs()' > /dev/null
29+
step mix run -e 'Philomena.Release.generate_autocomplete()' > /dev/null
2330

2431
sleep 300
2532
done
2633
}
2734

2835
# Always install assets
2936
(
30-
cd /srv/philomena/assets
31-
npm install
37+
step cd /srv/philomena/assets
38+
step npm install
3239
)
3340

3441
# Always install mix dependencies
35-
(cd /srv/philomena && mix deps.get)
42+
(
43+
step cd /srv/philomena
44+
step mix deps.get
45+
)
3646

3747
# Sleep to allow OpenSearch to finish initializing
3848
# if it's not done doing whatever it does yet
39-
echo -n "Waiting for OpenSearch"
49+
info "Waiting for OpenSearch"
4050

41-
until wget --no-check-certificate -qO - http://opensearch:9200; do
42-
echo -n "."
51+
until step wget --no-check-certificate -qO - http://opensearch:9200; do
4352
sleep 2
4453
done
4554

46-
echo
47-
4855
# Try to create the database if it doesn't exist yet
49-
createdb -h postgres -U postgres philomena_dev && mix ecto.setup_dev && mix reindex_all
56+
if step createdb -h postgres -U postgres philomena_dev; then
57+
step mix ecto.setup_dev
58+
step mix reindex_all
59+
fi
5060

5161
# Explicitly compile deps to avoid racing
52-
mix compile
62+
step mix compile
5363

5464
# Run background jobs
5565
background &
5666

67+
info "Starting the server"
68+
5769
# Run the application
5870
START_WORKER=true exec mix phx.server

scripts/lib.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env bash
2+
# This file is meant to be sourced by other scripts, not executed directly.
3+
# It contains a bunch of helper functions for writing bash scripts.
4+
5+
# This output stream is used by subshells to send their output to the root process stdout.
6+
global_stdout=3
7+
eval "exec $global_stdout>&1"
8+
9+
# Log a message at the info level
10+
function info {
11+
local message=$1
12+
13+
echo -e "\033[32;1m[INFO]\033[0m \033[0;32m$message\033[0m" >&2
14+
}
15+
16+
# Log the command and execute it
17+
function step {
18+
local cmd="$1"
19+
if [[ $cmd == exec ]]; then
20+
cmd=("${@:2}")
21+
else
22+
cmd=("${@:1}")
23+
fi
24+
25+
local task="${TASK:-}"
26+
27+
if [[ -n $task ]]; then
28+
task="($task) "
29+
fi
30+
31+
colorized_cmd=$(colorize_command "${cmd[@]}")
32+
33+
echo >&$global_stdout -e "\033[32;1m$task❱\033[0m $colorized_cmd" >&2
34+
35+
"$@"
36+
}
37+
38+
# Returns a command with syntax highlighting
39+
function colorize_command {
40+
local program=$1
41+
shift
42+
43+
local args=()
44+
for arg in "$@"; do
45+
if [[ $arg =~ ^- ]]; then
46+
args+=("\033[34;1m${arg}\033[0m")
47+
else
48+
args+=("\033[0;33m${arg}\033[0m")
49+
fi
50+
done
51+
52+
# On old versions of bash, for example 4.2.46 if the `args` array is empty,
53+
# then an `unbound variable` is thrown.
54+
#
55+
# Luckily, we don't pass commands without positional arguments to this function,
56+
# and we use bash >= v5. If this ever becomes a problem, you know the why.
57+
echo -e "\033[1;32m${program}\033[0m ${args[*]}"
58+
}

0 commit comments

Comments
 (0)