-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: recognice if the backend is broken
- Loading branch information
1 parent
8620c84
commit 8505147
Showing
2 changed files
with
27 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { DateTime } from 'luxon' | ||
import fetch from 'node-fetch' | ||
import database from './database.js' | ||
|
||
const runHeartbeat = async () => { | ||
let heartbeatUrl = process.env.HEARTBEAT_URL | ||
const newestTrainTrip = await database('train_trip').whereNotNull('destination_station').first() | ||
|
||
if (newestTrainTrip) { | ||
const newestTrainTripHour = DateTime.now().diff(DateTime.fromJSDate(newestTrainTrip.timestamp)).as('hours') | ||
if (newestTrainTripHour > 6) { | ||
console.error('The newest train trip in the database is about 6 hours old. It seem\'s like the backend is broken. Try to restart.') | ||
console.error('Heartbeat disabled because the backend seems to broken') | ||
heartbeatUrl = undefined // disable heartbeat | ||
} | ||
} else { | ||
console.warn('No full train trip does yet exist in the database') | ||
} | ||
|
||
if (heartbeatUrl) { | ||
fetch(heartbeatUrl) | ||
} | ||
} | ||
|
||
export default runHeartbeat |