firebase-functions icon indicating copy to clipboard operation
firebase-functions copied to clipboard

Firestore trigger (background function) crashes when uploading youtube video with googleapis

Open agbizzzle opened this issue 6 years ago • 3 comments

Related issues

[REQUIRED] Version info

node: 10, 12 (tested on both)

firebase-functions: 3.11.0

firebase-tools: 8.10.0

firebase-admin: 9.2.0

[REQUIRED] Test case

I have a firestore trigger that attempts to upload a video using the googleapis (version 61.0.0). Each time the API's video insert is called the firebase function terminates with a 'crash' status. I added log statements both before and after the video insert call to verify it is the insert that crashes.

Note: I created an HTTP callable function with the same logic and that worked leading me to believe it's something to do with background functions. Tested on both a firebase function node 10 & 12 environment.

import { google } from "googleapis";

const bucket_name = '';
const object_name = '';
const client_id = '';
const client_secret = '';
const youtube_oauth_refresh_token = '';
const YOUTUBE_SCOPES = [
  'https://www.googleapis.com/auth/youtube.upload',
  'https://www.googleapis.com/auth/youtube',
  'https://www.googleapis.com/auth/youtube.readonly'
].join(',');

const getYoutubeClient = () => {
  const youtubeUploadAuth = new google.auth.OAuth2({
    clientId: client_id,
    clientSecret: client_secret
  });

  youtubeUploadAuth.setCredentials({
    refresh_token: youtube_oauth_refresh_token,
    scope: YOUTUBE_SCOPES
  });

  youtubeApi = google.youtube({ auth: youtubeUploadAuth, version: 'v3' })
  google.options({ auth: youtubeUploadAuth });
  return youtubeApi;
}

const youtubeApi = getYoutubeClient();
logger.info('Initialized youtube client'); // this outputs in the console log

const response = await youtubeApi.videos.insert({ // crashes on this line
  part: ['snippet', 'status'],
  notifySubscribers: false,
  requestBody: {
    snippet: {
      title: 'video title',
      description: 'video description',
      tags: ['tag 1']
    },
    status: {
      privacyStatus: 'private',
      publicStatsViewable: false
    }
  },
  media: {
    body: admin.storage().bucket(bucket_name).file(object_name).createReadStream()
  }
});

[REQUIRED] Steps to reproduce

  1. Create a firestore trigger function for a document update (reproducible with onCreate and onWrite) that attempts to upload a video id.
  2. Trigger the function

[REQUIRED] Expected behavior

The video in the specified bucket path should be uploaded

[REQUIRED] Actual behavior

Function fails with a 'crash' status

Function execution took 4060 ms, finished with status: 'crash'

Were you able to successfully deploy your functions?

Yes, with no errors

agbizzzle avatar Oct 17 '20 19:10 agbizzzle

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

google-oss-bot avatar Oct 17 '20 19:10 google-oss-bot

Hi @agbizzzle. Sorry we are late on responding to this issue.

I can make few guesses on why this might be happening (e.g. background function might be taking up more RAM - try beefing up your function instance?), but can't debug it further without more info. If there are ways for you to share more debug logs (e.g. catch the error that's being thrown), that'd be very helpful.

Let us know!

taeold avatar May 19 '21 17:05 taeold

Marking the issue as closed. Please re-open if you continue have problems.

taeold avatar May 26 '21 17:05 taeold