mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
update timed loader to use class composition
This commit is contained in:
54
src/loaders/TimedApiBasedRepositoryLoader.ts
Normal file
54
src/loaders/TimedApiBasedRepositoryLoader.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { RepositoryLoader } from "./RepositoryLoader";
|
||||
|
||||
// Ideas to break this into smaller pieces in the future:
|
||||
// Have one repository data loader running for each supported system
|
||||
// Each data loader independently updates data based on frequency of usage
|
||||
|
||||
// Notes on this: we only need to reload ETA data frequently
|
||||
// Other data can be reloaded periodically
|
||||
// Detailed list:
|
||||
// - ETA: reload frequently or switch to write-through approach
|
||||
// - Shuttles: reload every minute
|
||||
// - Routes: reload every few minutes
|
||||
// - Stops: reload every few minutes
|
||||
// - OrderedStops: reload every few minutes
|
||||
// - Systems: reload once a day
|
||||
|
||||
export class TimedApiBasedRepositoryLoader {
|
||||
private shouldBeRunning: boolean = false;
|
||||
private timer: any;
|
||||
|
||||
readonly timeout = 10000;
|
||||
|
||||
constructor(
|
||||
public loader: RepositoryLoader,
|
||||
) {
|
||||
this.startFetchDataAndUpdate = this.startFetchDataAndUpdate.bind(this);
|
||||
}
|
||||
|
||||
public async start() {
|
||||
if (this.shouldBeRunning) {
|
||||
console.warn("DataLoader timer is already running");
|
||||
return;
|
||||
}
|
||||
|
||||
this.shouldBeRunning = true;
|
||||
await this.startFetchDataAndUpdate();
|
||||
}
|
||||
|
||||
public stop() {
|
||||
this.shouldBeRunning = false;
|
||||
}
|
||||
|
||||
private async startFetchDataAndUpdate() {
|
||||
if (!this.shouldBeRunning) return;
|
||||
|
||||
try {
|
||||
await this.loader.fetchAndUpdateAll();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
this.timer = setTimeout(this.startFetchDataAndUpdate, this.timeout);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user