jamespfennell / path-train-gtfs-realtime

GTFS Realtime for the PATH train in New York and New Jersey
MIT License
12 stars 3 forks source link

GTFS Realtime for the PATH Train

This repository hosts a simple Go application that reads PATH train realtime data from Matt Razza's public API and outputs a feed of the data in the GTFS Realtime format. Some important notes:

Running the application

The application is an HTTP server with the GTFS Realtime feed available at the /gtfsrt path.

There are 2 options for the data source to use for PATH arrival times:

  1. The path-data API (default), which fetches the data that the RidePATH app uses.
  2. The PANYNJ JSON API, which powers the PATH train schedules web page.

In the background, the program periodically retrieves data from the selected API and updates the feed. By default, this update occurs every 5 seconds for the path-data API and every 15 seconds for the PANYNJ JSON API.

There are a couple flags that can be passed to the binary:

Running using Docker

The CI process (using Github actions) builds a Docker image and stores it at the jamespfennell/path-train-gtfs-realtime:latest tag on Docker Hub. You can also build the Docker image locally by running docker build . in the root of the repo.

It is generally simplest to run the application using Docker. The only thing you need to do is port forward the HTTP server's port outside of the container. This is a functioning Docker compose configuration that does this:

version: '3.5'

services:
  path-train-gtfs-realtime:
    image: jamespfennell/path-train-gtfs-realtime:latest
    port: 8080:9001
    restart: always

Running using go run

When doing dev work it is generally necessary to run the application on "bare metal", which you can do simply with go run cmd/pathgtfsrt.go.

The source gRPC API and the GTFS Realtime format are both built on proto files. Getting these proto files and compiling them to go files is a bit of a pain, so they're kept in source control. To regenerate them, it's probably just simplest to use the Docker build process.

Error handling and exit codes

A number of errors can prevent the application from running 100% correctly, with the main source of errors being network failures when hitting the source API. At start-up, the application downloads static and realtime data from the API; if this fails, the application will exit.

After start-up, any further errors encountered are handled gracefully, and the server will not exit until interrupted. If, during a particular update, the realtime data for a specific stop cannot be retrieved, or is malformed, then the previously retrieved data will be used.

Monitoring

The application exports metrics in Prometheus format on the /metrics endpoint. See cmd/pathgtfsrt.go for the metric definitions.

Licence notes