diff --git a/.gitignore b/.gitignore index 254c38b..ec1a9b1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ .env -.shutdown-timestamp +.sync-timestamp database/* build/* dist/* diff --git a/README.md b/README.md index 517a259..0f2f9f7 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ If you're a student and want to contribute to this project, talk to the staff ab 5. Run `npm run start` to start the server and seed the initial database ### Reseed the database -1. Delete the *.shutdown-timestamp* file in the root directory +1. Delete the *.sync-timestamp* file in the root directory 2. Run `npm run build && npm run start` ### Add initial questionnaire questions diff --git a/src/env.ts b/src/env.ts index 4c50d22..97e27b7 100644 --- a/src/env.ts +++ b/src/env.ts @@ -11,5 +11,5 @@ export const CURSUS_ID: number = parseInt(process.env.INTRA_CURSUS_ID!); // Not defined in .env but in regular shell environment variables export const DEV_DAYS_LIMIT: number = process.env.DEV_DAYS_LIMIT ? parseInt(process.env.DEV_DAYS_LIMIT) : 365; -// Read from the .shutdown-timestamp file -export const LAST_SHUTDOWN_TIMESTAMP: number = fs.existsSync('.shutdown-timestamp') ? parseInt(fs.readFileSync('.shutdown-timestamp', 'utf8')) : 0; +// Read from the .sync-timestamp file +export const LAST_SYNC_TIMESTAMP: number = fs.existsSync('.sync-timestamp') ? parseInt(fs.readFileSync('.sync-timestamp', 'utf8')) : 0; diff --git a/src/handlers/shutdown.ts b/src/handlers/shutdown.ts deleted file mode 100644 index 0fe5e1c..0000000 --- a/src/handlers/shutdown.ts +++ /dev/null @@ -1,12 +0,0 @@ -import fs from 'fs'; - -// Server shutdown handler -export const handleServerShutdown = async function(): Promise { - console.warn('Server is shutting down.'); - console.log('Saving timestamp of shutdown to ./.shutdown-timestamp...'); - // Save to current folder in .shutdown-timestamp file - fs.writeFileSync('.shutdown-timestamp', Date.now().toString()); - console.log('Timestamp saved to ./.shutdown-timestamp'); - // Gracefully exit - process.exit(0); -}; diff --git a/src/main.ts b/src/main.ts index 282006e..7db7e45 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,7 +4,6 @@ dotenv.config({ path: '.env', debug: true }); // Imports for the server import express from 'express'; -import { handleServerShutdown } from './handlers/shutdown'; // Imports for the database connection import { PrismaClient } from "@prisma/client"; @@ -76,17 +75,12 @@ const main = async () => { // Start the Express server app.listen(4000, async () => { console.log('Server is running on http://localhost:4000'); - - // Make sure to save the timestamp of when the server exits (to synchronize with missed events) - process.on('exit', handleServerShutdown); - process.on('SIGINT', handleServerShutdown); - process.on('SIGTERM', handleServerShutdown); - process.on('uncaughtException', function(err, origin) { - console.error(`Caught exception: ${err}\nException origin: ${origin}`); - handleServerShutdown(); - }); }); + // Schedule the Intra synchronization to run every 10 minutes + setInterval(async () => { + await syncWithIntra(api!); + }, 10 * 60 * 1000); }; main(); // is async because of API synchronization diff --git a/src/sync/base.ts b/src/sync/base.ts index 89c1be1..1f47078 100644 --- a/src/sync/base.ts +++ b/src/sync/base.ts @@ -1,3 +1,4 @@ +import fs from 'fs'; import { PrismaClient } from "@prisma/client"; import Fast42 from "@codam/fast42"; import { initCodamQuiz } from "./quiz"; @@ -161,6 +162,13 @@ export const syncDataCB = async function(api: Fast42, syncDate: Date, lastSyncDa await fetchMultiple42ApiPagesCallback(api, path, params, callback); } +const saveSyncTimestamp = async function(timestamp: Date): Promise { + console.log('Saving timestamp of synchronization to ./.sync-timestamp...'); + // Save to current folder in .sync-timestamp file + fs.writeFileSync('.sync-timestamp', timestamp.toString()); + console.log('Timestamp saved to ./.sync-timestamp'); +} + export const syncWithIntra = async function(api: Fast42): Promise { const now = new Date(); @@ -174,5 +182,7 @@ export const syncWithIntra = async function(api: Fast42): Promise { await syncCoalitionUsers(api, now); await cleanupDB(api); + await saveSyncTimestamp(now); + console.info(`Intra synchronization completed at ${new Date().toISOString()}.`); }; diff --git a/src/sync/coalitions_users.ts b/src/sync/coalitions_users.ts index 9861799..72b74b3 100644 --- a/src/sync/coalitions_users.ts +++ b/src/sync/coalitions_users.ts @@ -1,6 +1,6 @@ import Fast42 from '@codam/fast42'; import { prisma, syncData } from './base'; -import { LAST_SHUTDOWN_TIMESTAMP } from '../env'; +import { LAST_SYNC_TIMESTAMP } from '../env'; import { getCoalitionIds } from '../utils'; // User object can be an object returned by /v2/users/:id or the user object in /v2/cursus_users/:id ! @@ -70,7 +70,7 @@ export const syncCoalitionUser = async function(coalitionUser: any): Promise { // Fetch the time of the last shutdown - const syncSince = new Date(LAST_SHUTDOWN_TIMESTAMP); + const syncSince = new Date(LAST_SYNC_TIMESTAMP); // Fetch all of our coalition ids const coalitionIds = await getCoalitionIds(prisma); diff --git a/src/sync/projects.ts b/src/sync/projects.ts index 659e78a..5337fee 100644 --- a/src/sync/projects.ts +++ b/src/sync/projects.ts @@ -1,6 +1,6 @@ import Fast42 from '@codam/fast42'; import { prisma, syncData } from './base'; -import { CURSUS_ID, LAST_SHUTDOWN_TIMESTAMP } from '../env'; +import { CURSUS_ID, LAST_SYNC_TIMESTAMP } from '../env'; export const syncProject = async function(project: any): Promise { try { @@ -35,7 +35,7 @@ export const syncProject = async function(project: any): Promise { export const syncProjects = async function(api: Fast42, syncDate: Date): Promise { // Fetch the time of the last shutdown - const syncSince = new Date(LAST_SHUTDOWN_TIMESTAMP); + const syncSince = new Date(LAST_SYNC_TIMESTAMP); // Fetch all projects from the API updated since the last shutdown const projects = await syncData(api, syncDate, syncSince, `/cursus/${CURSUS_ID}/projects`, {}); diff --git a/src/sync/users.ts b/src/sync/users.ts index b98ea84..79b2a72 100644 --- a/src/sync/users.ts +++ b/src/sync/users.ts @@ -1,6 +1,6 @@ import Fast42 from '@codam/fast42'; import { prisma, syncData } from './base'; -import { CAMPUS_ID, LAST_SHUTDOWN_TIMESTAMP } from '../env'; +import { CAMPUS_ID, LAST_SYNC_TIMESTAMP } from '../env'; // User object can be an object returned by /v2/users/:id or the user object in /v2/cursus_users/:id ! export const syncUser = async function(user: any): Promise { @@ -60,7 +60,7 @@ export const syncUser = async function(user: any): Promise { export const syncUsers = async function(api: Fast42, syncDate: Date): Promise { // Fetch the time of the last shutdown - const syncSince = new Date(LAST_SHUTDOWN_TIMESTAMP); + const syncSince = new Date(LAST_SYNC_TIMESTAMP); // Fetch all users from the API updated since the last shutdown const users = await syncData(api, syncDate, syncSince, `/campus/${CAMPUS_ID}/users`, {});