3 Commits

5 changed files with 56 additions and 32 deletions

View File

@@ -11,6 +11,8 @@ RUN set -eux && \
echo "Installing tools" && \ echo "Installing tools" && \
apt-get install -y -qq \ apt-get install -y -qq \
curl unzip cron ca-certificates logrotate dos2unix && \ curl unzip cron ca-certificates logrotate dos2unix && \
echo "Remove exim" && \
apt-get remove -y -qq exim4 exim4-base exim4-daemon-light && \
echo "Cleaning up" && \ echo "Cleaning up" && \
apt-get --yes autoremove --purge && \ apt-get --yes autoremove --purge && \
apt-get clean --yes && \ apt-get clean --yes && \
@@ -19,9 +21,6 @@ RUN set -eux && \
rm --recursive --force --verbose /var/tmp/* && \ rm --recursive --force --verbose /var/tmp/* && \
rm --recursive --force --verbose /var/cache/apt/archives/* && \ rm --recursive --force --verbose /var/cache/apt/archives/* && \
truncate --size 0 /var/log/*log truncate --size 0 /var/log/*log
COPY ./docker/cron-bun-log /etc/logrotate.d/
COPY ./docker/Crontab /etc/cron.d/
RUN chmod 0644 /etc/cron.d/Crontab
# install BunJs # install BunJs
RUN curl -fsSL https://bun.com/install | bash RUN curl -fsSL https://bun.com/install | bash
@@ -43,16 +42,22 @@ RUN cd /temp/prod && bun install --frozen-lockfile --production
# then copy all (non-ignored) project files into the image # then copy all (non-ignored) project files into the image
FROM base AS prerelease FROM base AS prerelease
COPY --from=install /temp/dev/node_modules node_modules COPY --from=install /temp/dev/node_modules node_modules
COPY . ./ COPY . .
# [optional] tests & build # [optional] tests & build
# copy production dependencies and source code into final image # copy production dependencies and source code into final image
FROM base AS release FROM base AS release
ENV NODE_ENV=production ENV NODE_ENV=production
COPY --from=install /temp/prod/node_modules node_modules COPY --from=install /temp/prod/node_modules node_modules
COPY . ./ # COPY ./docker/cron-bun-log /etc/logrotate.d/
COPY ./docker/docker-entrypoint.sh /opt/app/docker-entrypoint.sh COPY ./docker/Crontab /etc/cron.d/
#COPY --from=prerelease .entrypoint.sh . RUN chmod 0644 /etc/cron.d/Crontab && \
RUN dos2unix /opt/app/docker-entrypoint.sh && \ chown root:root /etc/cron.d/Crontab
chmod +x /opt/app/docker-entrypoint.sh COPY . .
ENTRYPOINT [ "/opt/app/docker-entrypoint.sh" ] RUN dos2unix \
/opt/app/docker/docker-entrypoint.sh \
/opt/app/run-task.sh \
/etc/cron.d/Crontab
RUN chmod +x /opt/app/docker/docker-entrypoint.sh && \
chmod +x /opt/app/run-task.sh
ENTRYPOINT [ "/opt/app/docker/docker-entrypoint.sh" ]

View File

@@ -3,7 +3,6 @@ services:
build: . build: .
volumes: volumes:
- ./data/db:/opt/app/data/db - ./data/db:/opt/app/data/db
- ./data/app/log:/var/log
env_file: env_file:
- path: ./.env - path: ./.env
required: true required: true
@@ -28,8 +27,8 @@ services:
#- 8880:8000 #- 8880:8000
healthcheck: healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/status"] test: ["CMD", "curl", "-f", "http://localhost:8000/status"]
interval: 10s interval: 5s
timeout: 5s timeout: 3s
retries: 5 retries: 5
# networks: # networks:
# default: # default:

View File

@@ -1,2 +1,5 @@
0 8 * * * root /opt/app/run-task.sh --today SHELL=/bin/bash
*/15 * * * * root /opt/app/run-task.sh MAILTO=""
0 8 * * * root . /etc/cron-env.sh && /opt/app/run-task.sh --today >> /proc/1/fd/1 2>&1
*/15 * * * * root . /etc/cron-env.sh && /opt/app/run-task.sh >> /proc/1/fd/1 2>&1
* * * * * root echo "cron test ran at $(date)" >> /proc/1/fd/1 2>&1

View File

@@ -1,15 +1,33 @@
#!/bin/bash #!/bin/bash
bun run ./src/app.ts --today
echo "ntfy_on=$ntfy_on" >> /etc/environment # Create or overwrite the cron env file
echo "ntfy_username=$ntfy_username" >> /etc/environment cat /dev/null > /etc/cron-env.sh
echo "ntfy_password=$ntfy_password" >> /etc/environment chmod 600 /etc/cron-env.sh
echo "ntfy_host=$ntfy_host" >> /etc/environment chmod +x /etc/cron-env.sh
echo "ntfy_topic=$ntfy_topic" >> /etc/environment # Write the Env Vars into a file for cron. happens during runtime of the container and not build.
echo "dc_on=$dc_on" >> /etc/environment # List your environment variables here
echo "dc_webhook=$dc_webhook" >> /etc/environment env_vars=(
echo "dc_botname=$dc_botname" >> /etc/environment ntfy_on
echo "dc_avatar_url=$dc_avatar_url" >> /etc/environment ntfy_username
ntfy_password
ntfy_host
ntfy_topic
dc_on
dc_webhook
dc_botname
dc_avatar_url
)
for var in "${env_vars[@]}"; do
val="${!var}"
if [ -n "$val" ]; then
# Safely export the variable with proper quoting
printf 'export %s=%q\n' "$var" "$val" >> /etc/cron-env.sh
fi
done
export PATH="/root/.bun/bin:$PATH"
bun run /opt/app/src/app.ts --today
# Start cron in foreground # Start cron in foreground
exec cron -f exec cron -f

View File

@@ -1,13 +1,10 @@
#!/bin/bash #!/bin/bash
set -e # set -e
set -o allexport
. /etc/environment || echo "[WARN] Failed to load env" >> /proc/1/fd/2
set +o allexport
export PATH="/root/.bun/bin:$PATH" export PATH="/root/.bun/bin:$PATH"
cd /opt/app set -o allexport
. /etc/cron-env.sh || echo "[WARN] Failed to load env" >> /proc/1/fd/2
set +o allexport
log_info() { log_info() {
echo "[INFO] $(date) $1" >> /proc/1/fd/1 echo "[INFO] $(date) $1" >> /proc/1/fd/1
@@ -19,6 +16,8 @@ log_error() {
log_info "Starting task with args: $*" log_info "Starting task with args: $*"
cd /opt/app
if bun run ./src/app.ts "$@" >> /proc/1/fd/1 2>> /proc/1/fd/2; then if bun run ./src/app.ts "$@" >> /proc/1/fd/1 2>> /proc/1/fd/2; then
log_info "Task completed successfully." log_info "Task completed successfully."
else else