Skip to content

Commit

Permalink
feat: recognice if the backend is broken
Browse files Browse the repository at this point in the history
  • Loading branch information
AdriDevelopsThings committed Dec 6, 2023
1 parent 8620c84 commit 8505147
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/autoFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { DateTime } from 'luxon'
import { info, debug } from './logger.js'
import { rabbit } from './rabbit/rabbit.js'
import staticConfig from './staticConfig.js'
import fetch from 'node-fetch'
import heartbeat from './heartbeat.js'

let fetchedHour: number | null = null

export const autoFetch = () => {
if (process.env.HEARTBEAT_URL)
fetch(process.env.HEARTBEAT_URL)
heartbeat()
const hour = DateTime.now().hour
if (fetchedHour != hour) {
fetchedHour = hour
Expand Down
25 changes: 25 additions & 0 deletions src/heartbeat.ts
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

0 comments on commit 8505147

Please sign in to comment.