Skip to content

Commit

Permalink
monitor: interval, stats logging
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Jul 30, 2019
1 parent bf7eb0e commit 5c35b65
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ const createApi = (hafas, config, attachMiddleware) => {
if ('docsLink' in config) assertNonEmptyString(config, 'docsLink')

const api = express()
api.locals.logger = pino()
api.locals.logger = pino({
level: process.env.LOGGING_LEVEL || 'info'
})

if (config.cors) {
const createCors = require('./cors')
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
"express": "^4.16.2",
"express-pino-logger": "^4.0.0",
"hafas-gtfs-rt-feed": "^0.2.0",
"hafas-monitor-trips": "^2.1.1",
"hafas-monitor-trips-server": "^1.0.0",
"hsts": "^2.1.0",
"length-prefixed-stream": "^2.0.0",
"lodash.throttle": "^4.1.1",
"markdown-it": "^9.0.1",
"nocache": "^2.0.0",
"parse-messy-time": "^2.1.0",
Expand All @@ -51,7 +53,7 @@
"devDependencies": {
"axios": "^0.19.0",
"get-port": "^5.0.0",
"hafas-client": "^4.0.1",
"hafas-client": "^4.5.1",
"pino-pretty": "^3.2.0",
"serve-static": "^1.13.2",
"tap-min": "^2.0.0",
Expand Down
14 changes: 13 additions & 1 deletion routes/events.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use strict'

const createServer = require('hafas-monitor-trips-server')
const createMonitor = require('hafas-monitor-trips')
const SSE = require('ssestream')
const createGtfsRtFeed = require('hafas-gtfs-rt-feed/writer')
const encodeChunks = require('length-prefixed-stream/encode')
const accepts = require('accepts')
const throttle = require('lodash.throttle')
const {parseNumber} = require('../lib/parse')

const INTERVAL = parseInt(process.env.MONITOR_INTERVAL || 60) * 1000
const EVENT_STREAM = 'text/event-stream'
const GTFS_RT = 'application/x.length-prefixed-gtfs-rt'

Expand Down Expand Up @@ -56,7 +59,11 @@ const gtfsRtWriter = (req, res, evNames) => {
}

const createEventsRoute = (hafas, config) => {
const monitor = createServer(hafas)
const monitor = createServer(hafas, {
createMonitor: (hafas, bbox) => {
return createMonitor(hafas, bbox, INTERVAL)
}
})

const eventsRoute = (req, res, next) => {
const {logger} = req.app.locals
Expand Down Expand Up @@ -99,8 +106,13 @@ const createEventsRoute = (hafas, config) => {
})

logger.info({bbox}, 'starting monitor')
const logInfos = throttle((stats) => {
logger.info({...stats, bbox})
}, 10 * 1000)
sub.on('stats', logInfos)
res.once('close', () => {
logger.info({bbox}, 'stopping monitor')
sub.removeListener('stats', logInfos)
sub.destroy()
})

Expand Down

0 comments on commit 5c35b65

Please sign in to comment.