2 Commits

Author SHA1 Message Date
f9a1919d08 replaced the old Crontab with commands with a script to execute instead. Better logging for docker logs <container name>.
because cron does not execute script with env vars, they get dumped into a file and set for the runtime of the  script run-taks.sh
2025-10-22 16:28:25 +02:00
96e9e79aeb replaced CMD in the docker file with ENTRYPOINT and docker-entrypoint.sh 2025-10-22 16:26:04 +02:00
4 changed files with 56 additions and 15 deletions

View File

@@ -10,7 +10,7 @@ RUN set -eux && \
apt-get upgrade -y -qq && \ apt-get upgrade -y -qq && \
echo "Installing tools" && \ echo "Installing tools" && \
apt-get install -y -qq \ apt-get install -y -qq \
curl unzip cron ca-certificates logrotate && \ curl unzip cron ca-certificates logrotate dos2unix && \
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 +19,14 @@ 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
ENV PATH="/root/.bun/bin:$PATH" ENV PATH="/root/.bun/bin:$PATH"
RUN ln -s /root/.bun/bin/bun /usr/local/bin/bun
# install dependencies into temp directory # install dependencies into temp directory
# this will cache them and speed up future builds # this will cache them and speed up future builds
FROM base AS install FROM base AS install
@@ -34,26 +39,20 @@ RUN mkdir -p /temp/prod
COPY package.json bun.lock /temp/prod/ COPY package.json bun.lock /temp/prod/
RUN cd /temp/prod && bun install --frozen-lockfile --production RUN cd /temp/prod && bun install --frozen-lockfile --production
COPY ./docker/Crontab /etc/cron.d/
RUN chmod 0644 /etc/cron.d/Crontab
COPY ./docker/cron-bun-log /etc/logrotate.d/
RUN mkdir /var/log/cron && touch /var/log/cron.log
# copy node_modules from temp directory # copy node_modules from temp directory
# 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
ENV NODE_ENV=production
# 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
COPY --from=install /temp/prod/node_modules node_modules COPY --from=install /temp/prod/node_modules node_modules
COPY --from=prerelease /opt/app/package.json .
#COPY --from=prerelease .entrypoint.sh .
COPY . ./ COPY . ./
RUN mkdir /var/log/cron && touch /var/log/cron.log COPY ./docker/docker-entrypoint.sh /opt/app/docker-entrypoint.sh
VOLUME /opt/app/data/db #COPY --from=prerelease .entrypoint.sh .
# VOLUME /var/log/cron RUN dos2unix /opt/app/docker-entrypoint.sh && \
CMD bun run ./src/app.ts --today && cron && tail -f /var/log/cron.log chmod +x /opt/app/docker-entrypoint.sh
ENTRYPOINT [ "/opt/app/docker-entrypoint.sh" ]

View File

@@ -1,2 +1,2 @@
8 * * * * bun run ./src/app.ts --today > /var/log/cron.log 2>&1 0 8 * * * root /opt/app/run-task.sh --today
*/15 * * * * bun run ./src/app.ts > /var/log/cron.log 2>&1 */15 * * * * root /opt/app/run-task.sh

View File

@@ -0,0 +1,15 @@
#!/bin/bash
bun run ./src/app.ts --today
echo "ntfy_on=$ntfy_on" >> /etc/environment
echo "ntfy_username=$ntfy_username" >> /etc/environment
echo "ntfy_password=$ntfy_password" >> /etc/environment
echo "ntfy_host=$ntfy_host" >> /etc/environment
echo "ntfy_topic=$ntfy_topic" >> /etc/environment
echo "dc_on=$dc_on" >> /etc/environment
echo "dc_webhook=$dc_webhook" >> /etc/environment
echo "dc_botname=$dc_botname" >> /etc/environment
echo "dc_avatar_url=$dc_avatar_url" >> /etc/environment
# Start cron in foreground
exec cron -f

27
run-task.sh Normal file
View File

@@ -0,0 +1,27 @@
#!/bin/bash
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"
cd /opt/app
log_info() {
echo "[INFO] $(date) $1" >> /proc/1/fd/1
}
log_error() {
echo "[ERROR] $(date) $1" >> /proc/1/fd/2
}
log_info "Starting task with args: $*"
if bun run ./src/app.ts "$@" >> /proc/1/fd/1 2>> /proc/1/fd/2; then
log_info "Task completed successfully."
else
log_error "Task failed!"
exit 1
fi