502 Proxy Error Node.js when using rackspace

103 views Asked by At

Using pkgcloud to upload file to Rackspace Cloud files causing Request Timeout and then 502 error happens makes server unusable (stopped) but not crash.

My Code :

function processImage(req, reply) {
  const options = {}
  let imageName = null
  let imageURL = null
  req.multipart(handler, done, options)

  // upload to rackspace here
  function done() {
    const readStream = fs.createReadStream(`${imgsPath}/${imageName}`)
    const writeStream = rackspace.upload({
      container: 'image-bearbrand-container',
      remote: imageName
    })
    writeStream.on('error', function(err) {
      reply.code(400).send({ err })
    })
    writeStream.on('success', function() {
      rimraf.sync(`${imgsPath}/${imageName}`) // delete current image because it now uploaded to rackspace
      reply.code(200).send({ imageURI: `${imageCdnUrl}${imageName}` })
    })
    readStream.pipe(writeStream)
  }
  // saving image to disk first
  function handler(field, file, filename, encoding, mimetype) {
    if (mime.includes(mimetype)) {
      imageName = newFilename('jpg')
      const imageStream = fs.createWriteStream(`${imgsPath}/${imageName}`)
      pump(file, imageStream, err => {
        if (err) throw Error(err.message)
      })
    } else {
      reply.code(200).send({
        message:
          'Ssst! your file is malicious! We keep your secret do not worry'
      })
    }
  }
}

So here my code was waiting for upload image on Rackspace ended then send the response the size of file image are around 0.5mb to 2mb, it may takes 2 minutes using rackspace and then request timeout then my server become stalled and cannot be accessed. is this because the stream never ended? Should I do timeout on stream and make it ends if request was timeout? how to do that then?

For framework I'm using fastify.

0

There are 0 answers