Azure功能在启动Node时运行代码 [英] Azure function run code on startup for Node

查看:84
本文介绍了Azure功能在启动Node时运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Azure函数开发Chatbot.我想从文件加载Chatbot的一些对话.我正在寻找一种在功能应用程序以某些功能回调开始之前加载这些对话数据的方法.有没有一种方法可以在启动功能应用程序后仅加载一次对话数据?

I am developing Chatbot using Azure functions. I want to load the some of the conversations for Chatbot from a file. I am looking for a way to load these conversation data before the function app starts with some function callback. Is there a way load the conversation data only once when the function app is started?

这个问题实际上是 Azure Function在启动时运行代码的重复.但是这个问题是针对C#的,我想在NodeJS中做同样的事情

This question is actually a duplicate of Azure Function run code on startup. But this question is asked for C# and I wanted a way to do the same thing in NodeJS

推荐答案

经过一个星期的混乱,我得到了一个可行的解决方案.

After like a week of messing around I got a working solution.

首先介绍一下上下文:当前的问题是,为Node JS Azure Functions运行自定义代码@ App Start.该问题目前正在此处进行讨论,并且几乎已经开放5年了,而且似乎什么都没去.

First some context: The question at hand, running custom code @ App Start for Node JS Azure Functions. The issue is currently being discussed here and has been open for almost 5 years, and doesn't seem to be going anywhere.

截止到目前,Azure Functions的热身"触发功能,请在此处找到 AZ Funcs Warm向上触发.但是,此触发器仅按比例运行.因此,您的应用的第一个初始实例将不会运行预热"程序.代码.

As of now there is an Azure Functions "warmup" trigger feature, found here AZ Funcs Warm Up Trigger. However this trigger only runs on-scale. So the first, initial instance of your App won't run the "warmup" code.

解决方案:

我创建了一个 start.js 文件,并将以下代码放在其中

I created a start.js file and put the following code in there

const ErrorHandler = require('./Classes/ErrorHandler');
const Validator = require('./Classes/Validator');
const delay = require('delay');

let flag = false;

module.exports = async () =>
{
    console.log('Initializing Globals')

    global.ErrorHandler = ErrorHandler;
    global.Validator = Validator;

    //this is just to test if it will work with async funcs
    const wait = await delay(5000)

    //add additional logic...
    //await db.connect(); etc // initialize a db connection

    console.log('Done Waiting')
}

我只需要运行这段代码

require('../start')();

在我的任何功能中.只需一项功能就可以了.由于部署代码时会加载所有函数依赖项,因此只要此行位于其中一个函数中, start.js 将运行并初始化所有全局/单变量或其他任何变量您希望它在功能启动时执行.我做了一个文字函数,叫做"startWarmUp".它只是一个计时器触发的功能,每天运行一次.

in any of my functions. Just one function is fine. Since all of the function dependencies are loaded when you deploy your code, as long as this line is in one of the functions, start.js will run and initialize all of your global/singleton variables or whatever else you want it to do on func start. I made a literal function called "startWarmUp" and it is just a timer triggered function that runs once a day.

我的用例是几乎每个函数都依赖ErrorHandler和Validator类.尽管通常将某些东西设为全局变量是一种不好的做法,但在这种情况下,我认为将这两个类设置为全局变量没有任何危害,因此它们可以在所有函数中使用.

My use case is that almost every function relies on ErrorHandler and Validator class. And though generally making something a global variable is bad practice, in this case I didn't see any harm in making these 2 classes global so they're available in all of the functions.

侧面说明:在本地进行开发时,您必须在 func start --functions<需要start.js的函数>中包含那个函数.< other funcs> ,以使启动代码实际运行.

Side Note: when developing locally you will have to include that function in your func start --functions <function requiring start.js> <other funcs> in order to have that startup code actually run.

这篇关于Azure功能在启动Node时运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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