简体   繁体   English

如何通过带有firebase功能的FTP下载文件?

[英]How to download a file through FTP with a firebase function?

QUESTION:题:

Unfortunately, my function finishes execution within seconds instead of executing in full.不幸的是,我的函数在几秒钟内完成执行,而不是完全执行。 This is apparently due to the fact that listeners are declared to stream the data: they are not promises I can await to my knowledge.这显然是因为监听器被声明为流式传输数据:据我所知,它们不是我可以等待的承诺。

How may I have my firebase function execute in full ?如何让我的 firebase 功能完全执行?


CODE:代码:

exports.fifteenMinutesData = functions
.runWith(runtimeOpts)
.pubsub
.schedule('*/15 * * * *')
.timeZone('Etc/UTC')
.onRun((context) => {
    return (async() => {
        try {

            const Client = require('ftp');
            const c = new Client();
            
            c.connect({
                host: "...",
                user: "..."
            });
            
            c.on('ready', async function () {
                c.get('text.txt', async function (err, stream) {
                    if (err)
                        throw err;
                    var content = '';
                    stream.on('data', function (chunk) {
                        content += chunk.toString();
                    });
                    stream.on('end', function () {
                        (async () => {

                            try {
                                let data = content;

                                //etc....
                            }
                            catch(err) {
                                console.log("ERR: "+err);
                            }
                        })()
                    })
                })
            })
        }
        catch(err) {
            console.log("ERR: "+err)
        }
    })()
});

You will need to promisify the result so the module is aware the value is asynchronous.您需要承诺结果,以便模块知道该值是异步的。 Currently, your callback is not informing the module of anything so the execution exits immediately, you will want a format like目前,您的回调未通知模块任何内容,因此执行会立即退出,您将需要类似的格式

exports.fifteenMinutesData = functions
.runWith(runtimeOpts)
.pubsub
.schedule('*/15 * * * *')
.timeZone('Etc/UTC')
.onRun((context) => new Promise((resolve, reject) => 
{

});

Where you call resolve(data);你在哪里调用resolve(data); for the success path and reject(err);对于成功路径和reject(err); for all error execution paths.对于所有错误执行路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM