gpt4 book ai didi

azure - 如何在 Mac 上本地运行计时器触发的 Azure Functions?

转载 作者:行者123 更新时间:2023-12-02 07:56:35 25 4
gpt4 key购买 nike

我想在本地开发环境(Node、OS X)中执行计时器触发函数,但似乎需要对我的 HTTP 触发函数设置进行一些更改。

这是到目前为止与计时器功能相关的代码:

/cron-job/function.json 定义计划每分钟运行的计时器输入绑定(bind)。它还具有对代码入口点的引用(从 Typescript 编译):

{
"bindings": [
{
"type": "timerTrigger",
"direction": "in",
"name": "timer",
"schedule": "0 */1 * * * *"
}
],
"scriptFile": "../dist/cron-job/index.js"
}

/cron-job/index.ts

import { AzureFunction, Context } from '@azure/functions'

const timerTrigger: AzureFunction = async function (
context: Context,
timer: any,
) {
console.log('context', context)
console.log('timer', timer)

// move on with async calls...
}

export default timerTrigger

/local.settings.json

{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "node",
"AzureWebJobsStorage": ""
}
}

当我尝试启动函数应用程序时:

 ~/Projects/test-api (dev) $ func start --verbose

我收到错误:

Missing value for AzureWebJobsStorage in local.settings.json. This is required for all triggers other than httptrigger, kafkatrigger. You can run 'func azure functionapp fetch-app-settings <functionAppName>' or specify a connection string in local.settings.json.

当我将 AzureWebJobsStorage 设置添加到 local.settings.json 时,我收到另一个错误:

The listener for function 'Functions.cron-job' was unable to start.
The listener for function 'Functions.cron-job' was unable to start. Microsoft.Azure.Storage.Common: Connection refused. System.Net.Http: Connection refused. System.Private.CoreLib: Connection refused.

最佳答案

经过一番研究,我想出了一个我认为应该分享的工作设置。

我原来的设置存在的问题是:

  1. local.settings.json 中没有 "AzureWebJobsStorage": "UseDevelopmentStorage=true"。我已经启动并运行了 HTTP 触发功能,但计时器触发器似乎需要该设置。对于使用存储模拟器时的本地开发,UseDevelopmentStorage=true shortcut可以使用。

  2. 未安装存储模拟器。在 Windows 上,它似乎是 Microsoft Azure SDK 的一部分和/或可以作为独立工具安装。但它不适用于 Mac 和 Linux。但是,有一个开源替代方案可用:Azurite ,这将是 replace the Storage Emulator .

作为引用,我创建了一个 Typescript 入门存储库,可以对其进行扩展以编写您自己的 Azure 计时器触发函数:azure-timer-function-starter-typescript

关于azure - 如何在 Mac 上本地运行计时器触发的 Azure Functions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64492603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com