Morfinismo
Morfinismo

Reputation: 5243

Google Cloud Run is returning error 503 - Service Unavailable

For some reason that I have not been able to spot, cloud run always returns 503 - Unavailable. No matter what I do. This works locally so I have no idea what could be the problem. All the other endpoints of the app work. Everything operates normally, but this request, for some reason, only works on localhost but when deployed to GCR, it doesn't works.

What I'm trying to do is upload a file to google drive. Here is the logic of the request:

router.post("/upload", async(req, res)=>{
  const payload = req.body;
  const resource = {
    name: payload.fileName || "Picture",
    driveId: process.env.DRIVE_ID
  };
  const uploadedFile = await uploadToTeamDrive(payload.imgData, resource).catch(error=>{
    console.error(`Error uploading file to team drive: ${error.toString()}`);
    gapiError(res, error);
  });
  if(!uploadedFile){ return; }
  res.status(201).send({success: true});
});

const uploadToTeamDrive = async(base64Img, resource)=>{
  const base64String = base64Img.split(",")[1];
  const imgData = Readable.from(Buffer.from(base64String, "base64"));
  const mimeType = base64Img.split(",")[0].split(";")[0].split(":")[1];
  const scopes = ["https://www.googleapis.com/auth/drive"];
  const client = await getClient(scopes);
  const service = google.drive({version: "v3", auth: client});
  const request = await service.files.create({
    media: {
      body: imgData,
      mimeType: mimeType
    },
    requestBody: resource,
    fields: "id,webViewLink,name,thumbnailLink,iconLink",
    supportsAllDrives: true
  });
  const file = request.data;
  return file;
}

This is what the logs from cloud run say:

Default
2021-02-05 13:01:20.945 CSTevents.js:187
Default
2021-02-05 13:01:20.945 CST throw er; // Unhandled 'error' event
Default
2021-02-05 13:01:20.945 CST ^
Default
2021-02-05 13:01:20.945 CST
Default
2021-02-05 13:01:20.945 CSTTypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type number
Default
2021-02-05 13:01:20.945 CST at validChunk (_stream_writable.js:268:10)
Default
2021-02-05 13:01:20.945 CST at ProgressStream.Writable.write (_stream_writable.js:303:21)
Default
2021-02-05 13:01:20.945 CST at Readable.ondata (_stream_readable.js:727:22)
Default
2021-02-05 13:01:20.945 CST at Readable.emit (events.js:210:5)
Default
2021-02-05 13:01:20.945 CST at addChunk (_stream_readable.js:309:12)
Default
2021-02-05 13:01:20.945 CST at readableAddChunk (_stream_readable.js:290:11)
Default
2021-02-05 13:01:20.945 CST at Readable.push (_stream_readable.js:224:10)
Default
2021-02-05 13:01:20.945 CST at next (internal/streams/from.js:34:27)
Default
2021-02-05 13:01:20.945 CST at processTicksAndRejections (internal/process/task_queues.js:93:5)
Default
2021-02-05 13:01:20.945 CSTEmitted 'error' event on Readable instance at:
Default
2021-02-05 13:01:20.945 CST at emitErrorNT (internal/streams/destroy.js:92:8)
Default
2021-02-05 13:01:20.945 CST at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
Default
2021-02-05 13:01:20.945 CST at processTicksAndRejections (internal/process/task_queues.js:80:21) {
2021-02-05 13:01:20.945 CST code: 'ERR_INVALID_ARG_TYPE'
Default
2021-02-05 13:01:20.945 CST}
2021-02-05 13:01:20.959 CSTPOST503661 B53 msChrome 88 https://xxx-yyy-test-75i2ghnk3q-uc.a.run.app/images/upload
The request failed because either the HTTP response was malformed or connection to the instance had an error.
Default
2021-02-05 13:01:20.961 CSTnpm ERR! code ELIFECYCLE
Default
2021-02-05 13:01:20.961 CSTnpm ERR! errno 1
Default
2021-02-05 13:01:20.962 CSTnpm ERR! [email protected] startapp: `node server/server.js`
Default
2021-02-05 13:01:20.962 CSTnpm ERR! Exit status 1
Default
2021-02-05 13:01:20.962 CSTnpm ERR!
Default
2021-02-05 13:01:20.963 CSTnpm ERR! Failed at the [email protected] startapp script.
Default
2021-02-05 13:01:20.963 CSTnpm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Default
2021-02-05 13:01:22.078 CST
Default
2021-02-05 13:01:22.078 CST> [email protected] startapp /usr/src/app
Default
2021-02-05 13:01:22.078 CST> node server/server.js
Default
2021-02-05 13:01:22.078 CST
Default
2021-02-05 13:01:23.240 CSTListening on port: 8080

I have followed all the suggestions in the troubleshooting documentation but nothing seems to give me a hint. There is nothing more on the logs and I already reviewed the GCR limitations and can't seem to find any problem.

Upvotes: 0

Views: 2097

Answers (1)

Morfinismo
Morfinismo

Reputation: 5243

Thanks to the suggestion by @guillaumeblaquiere I was able to troubleshoot the container locally. I realized that for some reason, the line that was causing the issue was this one:

const imgData = Readable.from(Buffer.from(base64String, "base64"));

That line of code works on windows, which is my development environment, but it doesn't work on linux, the container environment. Thanks to this post I change that line of code to:

const theBuff = Buffer.from(base64String, "base64");
const imgData = new PassThrough();
imgData.end(theBuff);

The above seems to play nice with linux and now the error is gone. In conclusion, this was not a google cloud run problem, but an environment code issue. I still have to investigate why the error. If someone knows, please provide some enlightenment.

Upvotes: 1

Related Questions