Skip to content

Commit

Permalink
fix: handle EPIPE errors + logs
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelTaranto authored and joshmh committed Oct 21, 2019
1 parent 3a4b916 commit c16333a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
12 changes: 10 additions & 2 deletions bin/lamassu-machine
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var Brain = require('../lib/brain')
var Configuration = require('../lib/configuration.js')

var bailed = false
var brain = null

function bail (err) {
if (bailed) return
Expand All @@ -30,12 +31,19 @@ function startBrain () {

var config = Configuration.loadConfig(commandLine)

var brain = new Brain(config)
brain = new Brain(config)
brain.on('error', bail)
brain.run()
}

process.on('uncaughtException', bail)
process.on('uncaughtException', err => {
if (err.code === 'EPIPE') {
if (brain) brain.epipeLog()
console.trace(err)
return
}
bail(err)
})
process.on('unhandledRejection', console.log)
process.on('exit', function () { console.log('lamassu-machine exiting') })

Expand Down
6 changes: 6 additions & 0 deletions lib/brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ Brain.prototype.run = function run () {
return this.checkWifiStatus()
}

Brain.prototype.epipeLog = function epipeLog () {
if (this.trader) {
this.trader.epipeLog()
}
}

Brain.prototype._executeCallbackAfterASufficientIdlePeriod =
function _executeCallbackAfterASufficientIdlePeriod (callback) {
const self = this
Expand Down
9 changes: 9 additions & 0 deletions lib/trader.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const LOGS_SYNC_INTERVAL = 60 * 1000

let serialNumber = 0
let networkDownCount = 0
let epipeLog = null
let epipePoll = null
const pid = uuid.v4()

// TODO: need to pass global options to request
Expand Down Expand Up @@ -63,6 +65,7 @@ util.inherits(Trader, EventEmitter)
*/
Trader.prototype.syncLogs = function syncLogs () {
// Get last seen timestamp from server
epipeLog = new Date()
this.request({ path: '/logs', method: 'get', noRetry: true })
.then(data => data.body)
// Delete log files that are two or more days old
Expand Down Expand Up @@ -130,7 +133,13 @@ Trader.prototype.verifyTransaction = function verifyTransaction (idRec) {
}).catch(err => console.log(err))
}

Trader.prototype.epipeLog = function epipeLog () {
console.log(`EPIPE: Log last try: ${epipeLog}`)
console.log(`EPIPE: Poll last try: ${epipePoll}`)
}

Trader.prototype.poll = function poll () {
epipePoll = new Date()
const stateRec = this.state

const path = '/poll?state=' + stateRec.state + '&idle=' + stateRec.isIdle + '&pid=' + pid + '&sn=' + serialNumber.toString()
Expand Down

0 comments on commit c16333a

Please sign in to comment.