Compare commits
6 Commits
fix/docker
...
5546d5511e
| Author | SHA1 | Date | |
|---|---|---|---|
| 5546d5511e | |||
| 4e21b1372f | |||
| f33324e9f8 | |||
| f9a1919d08 | |||
| 96e9e79aeb | |||
| 1729332373 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -37,3 +37,4 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
|
||||
*.db
|
||||
*.sqlite
|
||||
data
|
||||
2
Crontab
2
Crontab
@@ -1,2 +0,0 @@
|
||||
1 * * * * bun run ./src/app.ts --today > /dev/null 2>&1
|
||||
0 * * * * bun run ./src/app.ts > /dev/null 2>&1
|
||||
62
Dockerfile
62
Dockerfile
@@ -1,12 +1,31 @@
|
||||
FROM debian:12 AS base
|
||||
ARG BUILD_DATE
|
||||
ARG VERSION
|
||||
LABEL build_version="77th_eventcalendarntfy ${VERSION}, Build-date:- ${BUILD_DATE}"
|
||||
LABEL maintainer="chiko <chiko@xcsone.de>"
|
||||
WORKDIR /opt/app
|
||||
RUN apt-get update && \
|
||||
# apt-get install -y curl unzip cron ca-certificates python3 python3-pip && \
|
||||
apt-get install -y curl unzip cron ca-certificates python3 python3-pip && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
RUN set -eux && \
|
||||
echo "Updating APT" && \
|
||||
apt-get update -y -qq && \
|
||||
apt-get upgrade -y -qq && \
|
||||
echo "Installing tools" && \
|
||||
apt-get install -y -qq \
|
||||
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 && \
|
||||
rm --recursive --force --verbose /var/lib/apt/lists/* && \
|
||||
rm --recursive --force --verbose /tmp/* && \
|
||||
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
|
||||
@@ -19,33 +38,26 @@ RUN mkdir -p /temp/prod
|
||||
COPY package.json bun.lock /temp/prod/
|
||||
RUN cd /temp/prod && bun install --frozen-lockfile --production
|
||||
|
||||
# and install python dependencies
|
||||
# COPY ./requirements.txt .
|
||||
# RUN python3 -m pip install --break-system-packages -r requirements.txt
|
||||
# RUN python3 -m pip install -U python-dotenv
|
||||
|
||||
# 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
|
||||
WORKDIR /opt/app
|
||||
ENV NODE_ENV=production
|
||||
COPY --from=install /temp/prod/node_modules node_modules
|
||||
COPY --from=prerelease /opt/app/src/app.ts .
|
||||
COPY --from=prerelease /opt/app/package.json .
|
||||
#COPY --from=prerelease .entrypoint.sh .
|
||||
COPY Crontab /etc/cron.d/
|
||||
RUN chmod 0644 /etc/cron.d/Crontab
|
||||
COPY . ./
|
||||
# USER bun
|
||||
RUN touch /var/log/cron.log
|
||||
# RUN chmod +x entrypoint.sh
|
||||
# ENTRYPOINT ["./entrypoint.sh"]
|
||||
VOLUME /opt/app/data/db
|
||||
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" ]
|
||||
@@ -23,12 +23,12 @@ services:
|
||||
- ./data/apprise/config:/config
|
||||
- ./data/apprise/plugin:/plugin
|
||||
- ./data/apprise/attach:/attach
|
||||
ports:
|
||||
- 8000:8000
|
||||
# ports:
|
||||
#- 8880:8000
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://localhost:8000/status"]
|
||||
interval: 30s
|
||||
timeout: 10s
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 5
|
||||
# networks:
|
||||
# default:
|
||||
|
||||
5
docker/Crontab
Normal file
5
docker/Crontab
Normal file
@@ -0,0 +1,5 @@
|
||||
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
|
||||
8
docker/cron-bun-log
Normal file
8
docker/cron-bun-log
Normal file
@@ -0,0 +1,8 @@
|
||||
/var/log/cron.log {
|
||||
daily
|
||||
rotate 7
|
||||
compress
|
||||
missingok
|
||||
notifempty
|
||||
copytruncate
|
||||
}
|
||||
33
docker/docker-entrypoint.sh
Normal file
33
docker/docker-entrypoint.sh
Normal 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
|
||||
@@ -1,6 +0,0 @@
|
||||
#!/bin/bash
|
||||
crontab -l > mycron
|
||||
echo "0 8 * * * bun run ./src/app.ts --today > /dev/null 2>&1" >> mycron
|
||||
echo "0 * * * * bun run ./src/app.ts > /dev/null 2>&1" >> mycron
|
||||
crontab mycron
|
||||
rm mycron
|
||||
@@ -1,2 +0,0 @@
|
||||
apprise
|
||||
python-dotenv
|
||||
26
run-task.sh
Normal file
26
run-task.sh
Normal 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
|
||||
75
src/app.ts
75
src/app.ts
@@ -1,31 +1,25 @@
|
||||
import { TEventType } from "./component/event/event.types";
|
||||
import { db } from "./sql";
|
||||
import { Event, type TEventEntityNew, type TGetEventsOptions } from "./component/event/events";
|
||||
import { createPlaceholders, getTsNow, pad_l2 } from "./util";
|
||||
import { sendNotification } from "./sendNotification";
|
||||
import { createPlaceholders, pad_l2 } from "./util";
|
||||
|
||||
const argv = require('minimist')(process.argv.slice(2));
|
||||
console.dir(argv)
|
||||
|
||||
// const TS_TODAY = new Date();
|
||||
|
||||
function getTsNow() {
|
||||
const now = new Date();
|
||||
const rtn = {
|
||||
year: now.getFullYear(),
|
||||
month: now.getMonth() + 1,
|
||||
day: now.getDate(),
|
||||
minute: now.getMinutes(),
|
||||
seconds: now.getSeconds()
|
||||
}
|
||||
return rtn;
|
||||
}
|
||||
console.log("App started");
|
||||
console.dir({argv})
|
||||
|
||||
async function main ( ) {
|
||||
console.log("Excecuting main()");
|
||||
const TODAY = getTsNow();
|
||||
console.dir(TODAY);
|
||||
const events_currentMonth = await Event.fetch_events( TODAY.year, TODAY.month , -120 );
|
||||
console.log("events_currentMonth.length:" + events_currentMonth.length );
|
||||
const events_nextMonth = await Event.fetch_events( TODAY.year, TODAY.month + 1 , -120 );
|
||||
console.log("events_nextMonth.length:" + events_nextMonth.length );
|
||||
const events = [...events_currentMonth, ...events_nextMonth];
|
||||
console.log("events.length:" + events.length );
|
||||
|
||||
// const TS_TODAY = new Date();
|
||||
// Write to JSON File Section START
|
||||
// const data = JSON.stringify(events, null, 2);
|
||||
// const TS = `${TS_TODAY.getFullYear()}-${TS_TODAY.getMonth() + 1}-${TS_TODAY.getDate()}_${TS_TODAY.getHours()}-${TS_TODAY.getMinutes()}-${TS_TODAY.getSeconds()}`;
|
||||
@@ -33,16 +27,19 @@ async function main( ) {
|
||||
// Write to JSON File Section END
|
||||
|
||||
const allEventUids = events.map( event => { return event.uid; });
|
||||
console.dir(allEventUids );
|
||||
const placeholders = createPlaceholders( allEventUids );
|
||||
const getAllRelevantEventsQuery = db.query(
|
||||
`SELECT * FROM events WHERE uid IN (${placeholders}); `
|
||||
).as(Event );
|
||||
const AllRelevantEvents = getAllRelevantEventsQuery.all(...allEventUids);
|
||||
|
||||
console.log("AllRelevantEvents.length:" + AllRelevantEvents.length );
|
||||
const eventsToInsert: TEventEntityNew[] = [];
|
||||
for ( const ev of events ) {
|
||||
console.log("loop ev: " + [ ev.uid, ev.title, ev.date_at ].join( ", " ) );
|
||||
const found = AllRelevantEvents.find(event => event.uid === ev.uid);
|
||||
if ( found ) {
|
||||
console.log("loop ev found: " + [ found.uid, found.title, found.date_at ].join( ", " ) );
|
||||
if (
|
||||
found.title != ev.title ||
|
||||
found.description != ev.description ||
|
||||
@@ -55,29 +52,34 @@ async function main( ) {
|
||||
found.timezone != ev.timezone ||
|
||||
found.link != ev.link
|
||||
) {
|
||||
console.log("loop ev different (changed): " + [ ev.uid, ev.title, ev.date_at ].join( ", " ) );
|
||||
const newEventToInsert: TEventEntityNew = {... ev, notification: "changed"};
|
||||
eventsToInsert.push( newEventToInsert );
|
||||
}
|
||||
} else {
|
||||
console.log("loop ev added (new): " + [ ev.uid, ev.title, ev.date_at ].join( ", " ) );
|
||||
const newEventToInsert: TEventEntityNew = {... ev, notification: "new"};
|
||||
eventsToInsert.push( newEventToInsert );
|
||||
}
|
||||
}
|
||||
|
||||
console.dir(eventsToInsert)
|
||||
Event.insert( eventsToInsert, db);
|
||||
const options: TGetEventsOptions = {
|
||||
}
|
||||
const where: TGetEventsOptions = {}
|
||||
where.notification = ["new", "changed"]
|
||||
if ( argv.today ) {
|
||||
options.date = {
|
||||
where.date = {
|
||||
year: TODAY.year,
|
||||
month: TODAY.month,
|
||||
day: TODAY.day
|
||||
}
|
||||
} else {
|
||||
options.notification = ["new", "changed"]
|
||||
}
|
||||
const list_of_events = Event.get_events( options, db );
|
||||
const list_of_events = Event.get_events( where, db );
|
||||
console.dir({
|
||||
list_of_events,
|
||||
where
|
||||
});
|
||||
for ( const ev of list_of_events ) {
|
||||
console.log("loop list_of_events - ev: " + [ev.uid, ev.title, ev.date_at, "notification:" + ev.notification].join( ", " ) );
|
||||
const body = [
|
||||
`Title: ${ev.title}`,
|
||||
`Location: ${ev.location}`,
|
||||
@@ -87,6 +89,7 @@ async function main( ) {
|
||||
`By: ${ev.posted_by}`,
|
||||
`Link: ${ev.link}`,
|
||||
].join("\n");
|
||||
console.log("loop list_of_events - ev 'body': " + body );
|
||||
const notification_prefix = ( (event: Event) => {
|
||||
switch( event.notification) {
|
||||
case "new":
|
||||
@@ -113,28 +116,8 @@ async function main( ) {
|
||||
return false;
|
||||
})( ev );
|
||||
const title = `${today_prefix ? "TODAY " : ""}${notification_prefix ? notification_prefix + ": " : ""} ${ev.title} (${ TEventType[ ev.event_type ] })`;
|
||||
|
||||
await fetch("http://apprise:8000/notify", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({
|
||||
urls: [
|
||||
`ntfys://${process.env.ntfy_username}:${process.env.ntfy_password}@${process.env.ntfy_host}/${process.env.ntfy_topic}${ ev.link ? `?click=${ev.link}`: "?click=https://77th-jsoc.com/#/events" }`,
|
||||
`discord://${process.env.dc_webhook}?avatar_url=${process.env.dc_avatar_url}&botname=${process.env.dc_botname}`
|
||||
].join(","),
|
||||
title: title,
|
||||
body: body,
|
||||
format: "text"
|
||||
})
|
||||
});
|
||||
|
||||
// await sendNotification(
|
||||
// title,
|
||||
// body
|
||||
// // `${ev.link || "https://77th-jsoc.com/#/events"}`
|
||||
// );
|
||||
console.log("loop list_of_events - ev 'title': " + title );
|
||||
await sendNotification( title, body, ev.link ? ev.link : null);
|
||||
ev.set_notification("done", db);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,16 +1,26 @@
|
||||
import * as Bun from "bun";
|
||||
|
||||
export async function sendNotification(title: string, body: string, click?: string | null) {
|
||||
const command = [
|
||||
"python3",
|
||||
"./src/notification.py",
|
||||
`--title=${title}`,
|
||||
`--body=${body}`,
|
||||
];
|
||||
if ( click ) {
|
||||
command.push(`--click=${click}`);
|
||||
export async function sendNotification(title: string, body: string, link?: string | null) {
|
||||
console.dir({
|
||||
sendNotification: {
|
||||
title,
|
||||
body,
|
||||
link
|
||||
}
|
||||
const proc = Bun.spawn(command);
|
||||
const text = await proc.stdout.text();
|
||||
console.log("sendNotification: " + text);
|
||||
});
|
||||
const response = await fetch("http://apprise:8000/notify", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({
|
||||
urls: [
|
||||
`ntfys://${process.env.ntfy_username}:${process.env.ntfy_password}@${process.env.ntfy_host}/${process.env.ntfy_topic}${ link ? `?click=${link}`: "?click=https://77th-jsoc.com/#/events" }`,
|
||||
`discord://${process.env.dc_webhook}?avatar_url=${process.env.dc_avatar_url}&botname=${process.env.dc_botname}`
|
||||
].join(","),
|
||||
title: title,
|
||||
body: body,
|
||||
format: "text"
|
||||
})
|
||||
});
|
||||
const responseBody = await response.json();
|
||||
return responseBody;
|
||||
}
|
||||
16
src/sendNotificationPy.ts
Normal file
16
src/sendNotificationPy.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import * as Bun from "bun";
|
||||
|
||||
export async function sendNotificationPy(title: string, body: string, click?: string | null) {
|
||||
const command = [
|
||||
"python3",
|
||||
"./src/notification.py",
|
||||
`--title=${title}`,
|
||||
`--body=${body}`,
|
||||
];
|
||||
if ( click ) {
|
||||
command.push(`--click=${click}`);
|
||||
}
|
||||
const proc = Bun.spawn(command);
|
||||
const text = await proc.stdout.text();
|
||||
console.log("sendNotification: " + text);
|
||||
}
|
||||
12
src/util.ts
12
src/util.ts
@@ -27,3 +27,15 @@ export function pad_l2 ( _thing: string | number ): string {
|
||||
};
|
||||
return _thing.padStart(2, "0");
|
||||
}
|
||||
|
||||
export function getTsNow() {
|
||||
const now = new Date();
|
||||
const rtn = {
|
||||
year: now.getFullYear(),
|
||||
month: now.getMonth() + 1,
|
||||
day: now.getDate(),
|
||||
minute: now.getMinutes(),
|
||||
seconds: now.getSeconds()
|
||||
}
|
||||
return rtn;
|
||||
}
|
||||
Reference in New Issue
Block a user