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

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

问题描述

我正在使用 Azure 函数开发聊天机器人.我想从文件中加载 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 run code on startup 的副本.但是这个问题是针对 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')
}

要运行这段代码,我只需要这样做

To run this code I just have to do

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

在我的任何功能中.只要一个功能就可以了.由于在部署代码时会加载所有函数依赖项,只要此行在其中一个函数中,start.js 就会运行并初始化所有全局/单例变量或其他任何变量您希望它在 func 启动时执行.我制作了一个名为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 类.虽然通常将某些东西设为全局变量是不好的做法,但在这种情况下,我认为将这 2 个类设为全局并没有任何害处,因此它们可用于所有函数.

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 <function requires start.js> 中包含 that 函数<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天全站免登陆