added comments

This commit is contained in:
Brian Gough
2015-05-21 11:33:13 +01:00
parent 3c97b98fc4
commit ff5b203ecf

View File

@@ -20,8 +20,9 @@ module.exports = UrlFetcher =
logger.log url:url, filePath: filePath, "started downloading url to cache" logger.log url:url, filePath: filePath, "started downloading url to cache"
urlStream = request.get({url: url, timeout: oneMinute}) urlStream = request.get({url: url, timeout: oneMinute})
urlStream.pause() urlStream.pause() # stop data flowing until we are ready
# attach handlers before setting up pipes
urlStream.on "error", (error) -> urlStream.on "error", (error) ->
logger.error err: error, url:url, filePath: filePath, "error downloading url" logger.error err: error, url:url, filePath: filePath, "error downloading url"
callbackOnce(error or new Error("Something went wrong downloading the URL #{url}")) callbackOnce(error or new Error("Something went wrong downloading the URL #{url}"))
@@ -33,6 +34,7 @@ module.exports = UrlFetcher =
if res.statusCode >= 200 and res.statusCode < 300 if res.statusCode >= 200 and res.statusCode < 300
fileStream = fs.createWriteStream(filePath) fileStream = fs.createWriteStream(filePath)
# attach handlers before setting up pipes
fileStream.on 'error', (error) -> fileStream.on 'error', (error) ->
logger.error err: error, url:url, filePath: filePath, "error writing file into cache" logger.error err: error, url:url, filePath: filePath, "error writing file into cache"
fs.unlink filePath, (err) -> fs.unlink filePath, (err) ->
@@ -48,7 +50,7 @@ module.exports = UrlFetcher =
logger.log url:url, filePath: filePath, "piping into filestream" logger.log url:url, filePath: filePath, "piping into filestream"
urlStream.pipe(fileStream) urlStream.pipe(fileStream)
urlStream.resume() urlStream.resume() # now we are ready to handle the data
else else
logger.error statusCode: res.statusCode, url:url, filePath: filePath, "unexpected status code downloading url to cache" logger.error statusCode: res.statusCode, url:url, filePath: filePath, "unexpected status code downloading url to cache"
# https://nodejs.org/api/http.html#http_class_http_clientrequest # https://nodejs.org/api/http.html#http_class_http_clientrequest