Move cron jobs into GitHub actions (#1006)

This commit is contained in:
Alex Johansson 2021-10-25 17:16:42 +02:00 committed by GitHub
parent 8d6fec79d3
commit a9df3b9ad0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 116 additions and 58 deletions

View file

@ -0,0 +1,20 @@
name: Cron - bookingReminder
on:
# "Scheduled workflows run on the latest commit on the default or base branch."
# — https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#schedule
schedule:
# Runs “At minute 0, 15, 30, and 45.” (see https://crontab.guru)
- cron: "0,15,30,45 * * * *"
jobs:
cron:
runs-on: ubuntu-latest
steps:
- name: cURL request
if: ${{ secrets.APP_URL && secrets.CRON_API_KEY }}
run: |
curl ${{ secrets.APP_URL }}/api/cron/bookingReminder \
-X POST
-H 'content-type: application/json' \
-H 'authorization: ${{ secrets.CRON_API_KEY }}' \
--fail

View file

@ -0,0 +1,20 @@
name: Cron - downgradeUsers
on:
# "Scheduled workflows run on the latest commit on the default or base branch."
# — https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#schedule
schedule:
# Runs “At minute 0, 15, 30, and 45.” (see https://crontab.guru)
- cron: "0,15,30,45 * * * *"
jobs:
cron:
runs-on: ubuntu-latest
steps:
- name: cURL request
if: ${{ secrets.APP_URL && secrets.CRON_API_KEY }}
run: |
curl ${{ secrets.APP_URL }}/api/cron/downgradeUsers \
-X POST
-H 'content-type: application/json' \
-H 'authorization: ${{ secrets.CRON_API_KEY }}' \
--fail

View file

@ -7,12 +7,16 @@ import EventOrganizerRequestReminderMail from "@lib/emails/EventOrganizerRequest
import prisma from "@lib/prisma";
export default async function handler(req: NextApiRequest, res: NextApiResponse): Promise<void> {
const apiKey = req.query.apiKey;
if (process.env.CRON_API_KEY != apiKey) {
return res.status(401).json({ message: "Not authenticated" });
const apiKey = req.headers.authorization || req.query.apiKey;
if (process.env.CRON_API_KEY !== apiKey) {
res.status(401).json({ message: "Not authenticated" });
return;
}
if (req.method !== "POST") {
res.status(405).json({ message: "Invalid method" });
return;
}
if (req.method == "POST") {
const reminderIntervalMinutes = [48 * 60, 24 * 60, 3 * 60];
let notificationsSent = 0;
for (const interval of reminderIntervalMinutes) {
@ -49,13 +53,23 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
});
for (const booking of bookings.filter((b) => !reminders.some((r) => r.referenceId == b.id))) {
const { user } = booking;
const name = user?.name || user?.username;
if (!user || !name || !user.timeZone) {
console.error(`Booking ${booking.id} is missing required properties for booking reminder`, { user });
continue;
}
const evt: CalendarEvent = {
type: booking.title,
title: booking.title,
description: booking.description,
description: booking.description || undefined,
startTime: booking.startTime.toISOString(),
endTime: booking.endTime.toISOString(),
organizer: { email: booking.user.email, name: booking.user.name, timeZone: booking.user.timeZone },
organizer: {
email: user.email,
name,
timeZone: user.timeZone,
},
attendees: booking.attendees,
};
@ -72,4 +86,3 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
res.status(200).json({ notificationsSent });
}
}

View file

@ -6,9 +6,14 @@ import prisma from "@lib/prisma";
const TRIAL_LIMIT_DAYS = 14;
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const apiKey = req.query.apiKey;
const apiKey = req.headers.authorization || req.query.apiKey;
if (process.env.CRON_API_KEY !== apiKey) {
return res.status(401).json({ message: "Not authenticated" });
res.status(401).json({ message: "Not authenticated" });
return;
}
if (req.method !== "POST") {
res.status(405).json({ message: "Invalid method" });
return;
}
await prisma.user.updateMany({