5 Commits

Author SHA1 Message Date
5546d5511e replaced /etc/environment with /etc/cron-env.sh to work with cron 2025-10-22 19:10:04 +02:00
4e21b1372f added windows style new line clean up to all script files and linux resources. 2025-10-22 19:09:15 +02:00
f33324e9f8 docker-compose.yml cleanup 2025-10-22 19:07:33 +02:00
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
5 changed files with 85 additions and 20 deletions

View File

@@ -10,7 +10,9 @@ RUN set -eux && \
apt-get upgrade -y -qq && \
echo "Installing tools" && \
apt-get install -y -qq \
curl unzip cron ca-certificates logrotate && \
curl unzip cron ca-certificates logrotate dos2unix && \
echo "Remove exim" && \
apt-get remove -y -qq exim4 exim4-base exim4-daemon-light && \
echo "Cleaning up" && \
apt-get --yes autoremove --purge && \
apt-get clean --yes && \
@@ -19,9 +21,11 @@ RUN set -eux && \
rm --recursive --force --verbose /var/tmp/* && \
rm --recursive --force --verbose /var/cache/apt/archives/* && \
truncate --size 0 /var/log/*log
# install BunJs
RUN curl -fsSL https://bun.com/install | bash
ENV PATH="/root/.bun/bin:$PATH"
RUN ln -s /root/.bun/bin/bun /usr/local/bin/bun
# install dependencies into temp directory
# this will cache them and speed up future builds
FROM base AS install
@@ -34,26 +38,26 @@ RUN mkdir -p /temp/prod
COPY package.json bun.lock /temp/prod/
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
# then copy all (non-ignored) project files into the image
FROM base AS prerelease
COPY --from=install /temp/dev/node_modules node_modules
COPY . ./
COPY . .
# [optional] tests & build
ENV NODE_ENV=production
# copy production dependencies and source code into final image
FROM base AS release
ENV NODE_ENV=production
COPY --from=install /temp/prod/node_modules node_modules
COPY --from=prerelease /opt/app/package.json .
#COPY --from=prerelease .entrypoint.sh .
COPY . ./
RUN mkdir /var/log/cron && touch /var/log/cron.log
VOLUME /opt/app/data/db
# VOLUME /var/log/cron
CMD bun run ./src/app.ts --today && cron && tail -f /var/log/cron.log
# COPY ./docker/cron-bun-log /etc/logrotate.d/
COPY ./docker/Crontab /etc/cron.d/
RUN chmod 0644 /etc/cron.d/Crontab && \
chown root:root /etc/cron.d/Crontab
COPY . .
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: .
volumes:
- ./data/db:/opt/app/data/db
- ./data/app/log:/var/log
env_file:
- path: ./.env
required: true
@@ -28,8 +27,8 @@ services:
#- 8880:8000
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/status"]
interval: 10s
timeout: 5s
interval: 5s
timeout: 3s
retries: 5
# networks:
# default:

View File

@@ -1,2 +1,5 @@
8 * * * * bun run ./src/app.ts --today > /var/log/cron.log 2>&1
*/15 * * * * bun run ./src/app.ts > /var/log/cron.log 2>&1
SHELL=/bin/bash
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

@@ -0,0 +1,33 @@
#!/bin/bash
# Create or overwrite the cron env file
cat /dev/null > /etc/cron-env.sh
chmod 600 /etc/cron-env.sh
chmod +x /etc/cron-env.sh
# Write the Env Vars into a file for cron. happens during runtime of the container and not build.
# List your environment variables here
env_vars=(
ntfy_on
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
exec cron -f

26
run-task.sh Normal file
View File

@@ -0,0 +1,26 @@
#!/bin/bash
# set -e
export PATH="/root/.bun/bin:$PATH"
set -o allexport
. /etc/cron-env.sh || echo "[WARN] Failed to load env" >> /proc/1/fd/2
set +o allexport
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: $*"
cd /opt/app
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