fix double callback for proc.on 'error' and proc.on 'close'

This commit is contained in:
Brian Gough
2015-02-27 16:07:02 +00:00
parent 916b4cb40b
commit 3a4dd9df50

View File

@@ -2,6 +2,7 @@ fs = require "fs"
Path = require "path" Path = require "path"
spawn = require("child_process").spawn spawn = require("child_process").spawn
logger = require "logger-sharelatex" logger = require "logger-sharelatex"
_ = require "underscore"
module.exports = OutputFileOptimiser = module.exports = OutputFileOptimiser =
@@ -22,12 +23,15 @@ module.exports = OutputFileOptimiser =
stdout = "" stdout = ""
proc.stdout.on "data", (chunk) -> proc.stdout.on "data", (chunk) ->
stdout += chunk.toString() stdout += chunk.toString()
proc.on "error", callback callback = _.once(callback) # avoid double call back for error and close event
proc.on "error", (err) ->
logger.warn {err, args}, "qpdf failed"
callback(null) # ignore the error
proc.on "close", (code) -> proc.on "close", (code) ->
if code != 0 if code != 0
logger.warn {directory, code}, "qpdf returned error" logger.warn {code, args}, "qpdf returned error"
return callback null return callback(null) # ignore the error
fs.rename tmpOutput, dst, (err) -> fs.rename tmpOutput, dst, (err) ->
if err? if err?
logger.warn {tmpOutput, dst}, "failed to rename output of qpdf command" logger.warn {tmpOutput, dst}, "failed to rename output of qpdf command"
callback err callback(null) # ignore the error