Skip to content

Commit

Permalink
fix sync using last sync time from server start
Browse files Browse the repository at this point in the history
  • Loading branch information
FreekBes committed Oct 8, 2024
1 parent c21faa2 commit 895803b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
3 changes: 0 additions & 3 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@ 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 .sync-timestamp file
export const LAST_SYNC_TIMESTAMP: number = fs.existsSync('.sync-timestamp') ? parseInt(fs.readFileSync('.sync-timestamp', 'utf8')) : 0;
19 changes: 16 additions & 3 deletions src/sync/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ export const syncDataCB = async function(api: Fast42, syncDate: Date, lastSyncDa
await fetchMultiple42ApiPagesCallback(api, path, params, callback);
}

const getLastSyncTimestamp = async function(): Promise<Date> {
return new Promise((resolve, reject) => {
fs.readFile('.sync-timestamp', 'utf8', (err, data) => {
if (err) {
// return reject(err);
return resolve(new Date(0));
}
return resolve(new Date(parseInt(data)));
});
});
}

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
Expand All @@ -173,13 +185,14 @@ export const syncWithIntra = async function(api: Fast42): Promise<void> {
const now = new Date();

console.info(`Starting Intra synchronization at ${now.toISOString()}...`);
const lastSync = await getLastSyncTimestamp();

await initCodamQuiz();
await initCodamCoalitionFixedTypes();
await syncProjects(api, now);
await syncUsers(api, now);
await syncProjects(api, lastSync, now);
await syncUsers(api, lastSync, now);
await syncBlocs(api, now); // also syncs coalitions
await syncCoalitionUsers(api, now);
await syncCoalitionUsers(api, lastSync, now);
await cleanupDB(api);

await saveSyncTimestamp(now);
Expand Down
6 changes: 1 addition & 5 deletions src/sync/coalitions_users.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Fast42 from '@codam/fast42';
import { prisma, syncData } from './base';
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 @@ -68,10 +67,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_SYNC_TIMESTAMP);

export const syncCoalitionUsers = async function(api: Fast42, syncSince: Date, syncDate: Date): Promise<void> {
// Fetch all of our coalition ids
const coalitionIds = await getCoalitionIds(prisma);

Expand Down
7 changes: 2 additions & 5 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_SYNC_TIMESTAMP } from '../env';
import { CURSUS_ID } from '../env';

export const syncProject = async function(project: any): Promise<void> {
try {
Expand Down Expand Up @@ -33,10 +33,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_SYNC_TIMESTAMP);

export const syncProjects = async function(api: Fast42, syncSince: Date, syncDate: Date): Promise<void> {
// Fetch all projects from the API updated since the last shutdown
const projects = await syncData(api, syncDate, syncSince, `/cursus/${CURSUS_ID}/projects`, {});

Expand Down
7 changes: 2 additions & 5 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_SYNC_TIMESTAMP } from '../env';
import { CAMPUS_ID } 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 @@ -58,10 +58,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_SYNC_TIMESTAMP);

export const syncUsers = async function(api: Fast42, syncSince: Date, syncDate: Date): Promise<void> {
// 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 895803b

Please sign in to comment.