Cloud Functions for Firebase:在不触及最大超时的情况下完成长流程
Cloud Functions for Firebase: completing long processes without touching maximum timeout
当视频上传到 firebase 存储时,我必须将视频从 webm 转码为 mp4。 I have a code demo here that works,但如果上传的视频太大,我的 firebase 函数会在转换完成前超时。我知道可以增加该函数的超时限制,但这看起来很麻烦,因为我永远无法 确认 该过程将花费比超时限制更少的时间。
有什么方法可以在不增加最大超时限制的情况下阻止 firebase 超时吗?
如果没有,有没有办法在完成耗时的过程(如视频转换)的同时仍然让每个过程开始使用 firebase 函数触发器?
如果即使使用 firebase 函数完成耗时的过程也不是真正存在的东西,是否有某种方法可以在不影响质量的情况下加快 fluent-ffmpeg 的转换速度? (我意识到这部分有很多问题。如果绝对必要,我计划降低质量,因为将 webms 转换为 mp4 的原因是 IOS 设备)
作为参考,这里是我提到的演示的主要部分。正如我之前所说,完整的代码可以是 seen here,但是复制过来的这部分代码是创建确保转码完成的 Promise 的部分。完整的代码只有70多行,如果需要的话应该比较容易看懂。
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 事件中的下一段代码)
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);
});
})
});
});
Cloud Functions for Firebase 不太适合(也不支持)可能超过最大超时的长时间 运行 任务。使用 only Cloud Functions 执行非常繁重的计算操作的唯一真正机会是找到一种方法将工作拆分为多个函数调用,然后将所有工作的结果加入一个最终产品。对于像视频转码这样的事情,这听起来是一个非常困难的任务。
相反,考虑在 App Engine or Compute Engine.
中使用函数触发长 运行 任务
作为对试图弄清楚如何通过转码视频或其他一些长过程的随机匿名人员的跟进,这是我给出的相同代码示例的一个版本,而不是将 http 请求发送到 google 对文件进行转码的应用引擎进程。目前尚无相关文档,但查看 Firebase/functions/index.js 代码和 app.js 代码可能会帮助您解决问题。
https://github.com/Scew5145/GCSConvertDemo
祝你好运。
当视频上传到 firebase 存储时,我必须将视频从 webm 转码为 mp4。 I have a code demo here that works,但如果上传的视频太大,我的 firebase 函数会在转换完成前超时。我知道可以增加该函数的超时限制,但这看起来很麻烦,因为我永远无法 确认 该过程将花费比超时限制更少的时间。
有什么方法可以在不增加最大超时限制的情况下阻止 firebase 超时吗?
如果没有,有没有办法在完成耗时的过程(如视频转换)的同时仍然让每个过程开始使用 firebase 函数触发器?
如果即使使用 firebase 函数完成耗时的过程也不是真正存在的东西,是否有某种方法可以在不影响质量的情况下加快 fluent-ffmpeg 的转换速度? (我意识到这部分有很多问题。如果绝对必要,我计划降低质量,因为将 webms 转换为 mp4 的原因是 IOS 设备)
作为参考,这里是我提到的演示的主要部分。正如我之前所说,完整的代码可以是 seen here,但是复制过来的这部分代码是创建确保转码完成的 Promise 的部分。完整的代码只有70多行,如果需要的话应该比较容易看懂。
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 事件中的下一段代码)
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);
});
})
});
});
Cloud Functions for Firebase 不太适合(也不支持)可能超过最大超时的长时间 运行 任务。使用 only Cloud Functions 执行非常繁重的计算操作的唯一真正机会是找到一种方法将工作拆分为多个函数调用,然后将所有工作的结果加入一个最终产品。对于像视频转码这样的事情,这听起来是一个非常困难的任务。
相反,考虑在 App Engine or Compute Engine.
中使用函数触发长 运行 任务作为对试图弄清楚如何通过转码视频或其他一些长过程的随机匿名人员的跟进,这是我给出的相同代码示例的一个版本,而不是将 http 请求发送到 google 对文件进行转码的应用引擎进程。目前尚无相关文档,但查看 Firebase/functions/index.js 代码和 app.js 代码可能会帮助您解决问题。
https://github.com/Scew5145/GCSConvertDemo
祝你好运。