Skip to content

Commit

Permalink
switch to sync timestamp and sync every 10 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
FreekBes committed Oct 7, 2024
1 parent 3f8eb64 commit 7e08953
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.env
.shutdown-timestamp
.sync-timestamp
database/*
build/*
dist/*
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
12 changes: 0 additions & 12 deletions src/handlers/shutdown.ts

This file was deleted.

14 changes: 4 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
10 changes: 10 additions & 0 deletions src/sync/base.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs';
import { PrismaClient } from "@prisma/client";
import Fast42 from "@codam/fast42";
import { initCodamQuiz } from "./quiz";
Expand Down Expand Up @@ -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<void> {
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<void> {
const now = new Date();

Expand All @@ -174,5 +182,7 @@ export const syncWithIntra = async function(api: Fast42): Promise<void> {
await syncCoalitionUsers(api, now);
await cleanupDB(api);

await saveSyncTimestamp(now);

console.info(`Intra synchronization completed at ${new Date().toISOString()}.`);
};
4 changes: 2 additions & 2 deletions src/sync/coalitions_users.ts
Original file line number Diff line number Diff line change
@@ -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 !
Expand Down Expand Up @@ -70,7 +70,7 @@ export const syncCoalitionUser = async function(coalitionUser: any): Promise<voi

export const syncCoalitionUsers = async function(api: Fast42, syncDate: Date): Promise<void> {
// 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);
Expand Down
4 changes: 2 additions & 2 deletions src/sync/projects.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
try {
Expand Down Expand Up @@ -35,7 +35,7 @@ export const syncProject = async function(project: any): Promise<void> {

export const syncProjects = async function(api: Fast42, syncDate: Date): Promise<void> {
// 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`, {});
Expand Down
4 changes: 2 additions & 2 deletions src/sync/users.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
Expand Down Expand Up @@ -60,7 +60,7 @@ export const syncUser = async function(user: any): Promise<void> {

export const syncUsers = async function(api: Fast42, syncDate: Date): Promise<void> {
// 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`, {});
Expand Down

0 comments on commit 7e08953

Please sign in to comment.