Firebase的云功能:完成较长的流程而不会影响最大超时 [英] Cloud Functions for Firebase: completing long processes without touching maximum timeout

查看:123
本文介绍了Firebase的云功能:完成较长的流程而不会影响最大超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将视频上传到Firebase存储后,我必须将它们从webm转码为mp4. 我在这里有一个可以正常工作的代码演示,但是如果上传的视频太大,firebase功能将超时转换完成之前,对我负责.我知道可以增加该函数的超时限制,但这似乎很混乱,因为我无法确认,该过程所花费的时间少于超时限制.

I have to transcode videos from webm to mp4 when they're uploaded to firebase storage. I have a code demo here that works, but if the uploaded video is too large, firebase functions will time out on me before the conversion is finished. I know it's possible to increase the timeout limit for the function, but that seems messy, since I can't ever confirm the process will take less time than the timeout limit.

是否有某种方法可以在不增加最大超时限制的情况下停止Firebase超时?

Is there some way to stop firebase from timing out without just increasing the maximum timeout limit?

如果没有,是否有一种方法可以完成耗时的过程(例如视频转换),同时仍然让每个过程都使用firebase函数触发器开始?

If not, is there a way to complete time consuming processes (like video conversion) while still having each process start using firebase function triggers?

如果甚至不真正使用Firebase函数完成耗时的过程,是否有某种方法可以在不影响质量的情况下加快fluent-ffmpeg的转换? (我意识到这部分问题要问很多.如果绝对需要,我计划降低质量,因为将webms转换为mp4的原因是针对IOS设备)

If even completing time consuming processes using firebase functions isn't something that really exists, is there some way to speed up the conversion of fluent-ffmpeg without touching the quality that much? (I realize this part is a lot to ask. I plan on lowering the quality if I absolutely have to, as the reason webms are being converted to mp4 is for IOS devices)

作为参考,这是我提到的演示的主要部分.如我之前所说,完整的代码可以是在此处看到,但是复制的这段代码是创建Promise的部分,该部分可确保代码转换完成.完整的代码只有70几行,因此如果需要的话,应该相对容易些.

For reference, here's the main portion of the demo I mentioned. As I said before, the full code can be seen here, but this section of the code copied over is the part that creates the Promise that makes sure the transcoding finishes. The full code is only 70 something lines, so it should be relatively easy to go through if needed.

const functions = require('firebase-functions');
const mkdirp = require('mkdirp-promise');
const gcs = require('@google-cloud/storage')();
const Promise = require('bluebird');
const ffmpeg = require('fluent-ffmpeg');
const ffmpeg_static = require('ffmpeg-static');

(这里有很多文本解析代码,随后是onChange事件中的下一段代码)

(There's a bunch of text parsing code here, followed by this next chunk of code inside an onChange event)

function promisifyCommand (command) {
    return new Promise( (cb) => {
        command
        .on( 'end',   ()      => { cb(null)  } )
        .on( 'error', (error) => { cb(error) } )
        .run();
    })
}
return mkdirp(tempLocalDir).then(() => {
    console.log('Directory Created')
    //Download item from bucket
    const bucket = gcs.bucket(object.bucket);
    return bucket.file(filePath).download({destination: tempLocalFile}).then(() => {
      console.log('file downloaded to convert. Location:', tempLocalFile)
      cmd = ffmpeg({source:tempLocalFile})
               .setFfmpegPath(ffmpeg_static.path)
               .inputFormat(fileExtension)
               .output(tempLocalMP4File)
      cmd = promisifyCommand(cmd)
      return cmd.then(() => {
        //Getting here takes forever, because video transcoding takes forever!
        console.log('mp4 created at ', tempLocalMP4File)
        return bucket.upload(tempLocalMP4File, {
            destination: MP4FilePath
        }).then(() => {
          console.log('mp4 uploaded at', filePath);
        });
      })
    });
  });

推荐答案

Firebase的Cloud Functions不适用于(并且不受支持)长时间运行的任务(这些任务可能超过最大超时).使用 only 云功能执行非常繁重的计算操作的唯一真正机会是找到一种方法,将工作分解为多个函数调用,然后将所有工作的结果合并为最终产品.对于像视频转码这样的事情,这听起来像是一个非常困难的任务.

Cloud Functions for Firebase is not well suited (and not supported) for long-running tasks that can go beyond the maximum timeout. Your only real chance at using only Cloud Functions to perform very heavy compute operations is to find a way to split up the work into multiple function invocations, then join the results of all that work into a final product. For something like video transcoding, that sounds like a very difficult task.

相反,请考虑在 App Engine 计算引擎.

这篇关于Firebase的云功能:完成较长的流程而不会影响最大超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆