Skip to content

Commit

Permalink
chore: Refactor schedule service to improve startup test scheduling a…
Browse files Browse the repository at this point in the history
…nd logging
  • Loading branch information
Victor Lopez committed Oct 23, 2024
1 parent 65a09a1 commit 719d2bf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ export class AppComponent {
this.refreshHistory.bind(this)
);
this.refreshHistory();
if (this.storage.get('schoolId')) {
setInterval(() => {
this.scheduleService.initiate();
}, 60000);
}
setInterval(() => {
this.scheduleService.initiate();
}, 60000);
}

openSecond() {
Expand Down
32 changes: 26 additions & 6 deletions src/app/services/schedule.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export class ScheduleService {
private readonly RETRY_DELAY = 15 * 60 * 1000; // 15 minutes in milliseconds
private readonly STARTUP_TEST_DELAY = 15 * 60 * 1000; // 15 minutes in milliseconds
private readonly STARTUP_TEST_KEY = 'lastStartupTest';
private readonly STARTUP_TEST_SCHEDULED_KEY = 'startupTestScheduled';
private readonly TEN_MINUTE = 60 * 1000 * 10; // 10 minutes in milliseconds

constructor(
private storageService: StorageService,
Expand All @@ -34,8 +36,12 @@ export class ScheduleService {
async initiate() {
console.log('ScheduleService initiate called');
try {
if (!this.storageService.get('schoolId')) {
return console.log('No schoolId found, skipping schedule service');
}

this.scheduleStartupTestIfNeeded();
await this.watch();
await this.scheduleStartupTestIfNeeded();
} catch (error) {
console.error('Error during ScheduleService initiation:', error);
}
Expand Down Expand Up @@ -84,9 +90,9 @@ export class ScheduleService {
);
const nextDaySlotA = this.getSlotTimes(tomorrow).slotA;

if (lastMeasurementTime < slotA) {
if (lastMeasurementTime < slotA && now.getTime() < slotB) {
return this.createSlotSemaphore(slotA, 'A');
} else if (lastMeasurementTime < slotB) {
} else if (lastMeasurementTime < slotB && now.getTime() < slotC) {
return this.createSlotSemaphore(slotB, 'B');
} else if (lastMeasurementTime < slotC) {
return this.createSlotSemaphore(slotC, 'C');
Expand Down Expand Up @@ -146,6 +152,11 @@ export class ScheduleService {
await this.rescheduleFailedMeasurement(scheduleSemaphore);
return;
}
if (currentTime > scheduleSemaphore.end) {
console.log('Slot ended, Skiping measurement.');
await this.setSemaphore({});
return;
}

try {
console.log('Running test...');
Expand Down Expand Up @@ -261,7 +272,7 @@ export class ScheduleService {
}

// Keeping these methods to maintain compatibility with existing code
initializeScheduleInitializers() {}
initializeScheduleInitializers() { }
async scheduleInitializers(type: string) {
return this.createIntervalSemaphore(Date.now(), 24 * 60 * 60 * 1000);
}
Expand All @@ -280,6 +291,15 @@ export class ScheduleService {
const lastStartupTest = await this.storageService.get(
this.STARTUP_TEST_KEY
);
const startupTestScheduled = await this.storageService.get(
this.STARTUP_TEST_SCHEDULED_KEY
);
console.log(`Startup test scheduled for: ${startupTestScheduled}`);
if (parseInt(startupTestScheduled, 10) + this.TEN_MINUTE > Date.now()) {
console.log('Startup test already scheduled, skipping.');
return;
}

const now = new Date();
const today = new Date(
now.getFullYear(),
Expand All @@ -291,9 +311,9 @@ export class ScheduleService {
if (!lastStartupTest || parseInt(lastStartupTest, 10) < today) {
const startupDelay = Math.floor(Math.random() * this.STARTUP_TEST_DELAY);
const scheduledTime = new Date(Date.now() + startupDelay);

setTimeout(() => this.runStartupTest(), startupDelay);
this.storageService.set(this.STARTUP_TEST_SCHEDULED_KEY, scheduledTime.getTime().toString());
console.log(`Scheduling startup test for ${scheduledTime.toISOString()}`);
setTimeout(() => this.runStartupTest(), startupDelay);
} else {
console.log('Startup test already run today, skipping.');
}
Expand Down

0 comments on commit 719d2bf

Please sign in to comment.