azure - 如何在 Mac 上本地运行计时器触发的 Azure Functions?
问题描述
我想在我的本地开发环境(Node、OS X)中执行一个计时器触发的函数,但它似乎需要对我拥有的 HTTP 触发的函数设置进行一些更改。
这是到目前为止与计时器功能相关的代码:
/cron-job/function.json
定义一个定时器输入绑定,每分钟运行一次。它还引用了代码入口点(从 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.
解决方案
经过一些研究,我想出了一个我认为应该分享的工作设置。
我原来设置的问题是:
没有
"AzureWebJobsStorage": "UseDevelopmentStorage=true"
在local.settings.json
. 我已经启动并运行了一个 HTTP 触发功能,但似乎计时器触发器需要该设置。对于使用存储模拟器的本地开发,可以使用UseDevelopmentStorage=true
快捷方式。未安装存储模拟器。在 Windows 上,它似乎是 Microsoft Azure SDK 的一部分和/或它可以作为独立工具安装。但它不适用于 Mac 和 Linux。但是,有一个可用的开源替代方案:Azurite,它将取代 Storage Emulator。
作为参考,我创建了一个 Typescript 启动器存储库,可以扩展它以编写您自己的 Azure 计时器触发函数:azure-timer-function-starter-typescript
推荐阅读
- c++ - 用long long 溢出pow 函数?
- azure - 使用 azure 的安全 Web API 返回登录页面
- download - 如何在 .Net Core 中提供使用 OpenXML SDK 创建的可下载 docx 文件
- opc - Eclipse milo - OPCUA - 将服务器(值/节点)更改通知客户端以触发刷新的最佳实践是什么?
- javascript - 将对象添加到另一个对象
- django - Django 使用 next 重定向到登录 url,而不使用方法装饰器
- python - 如何设置 appache2 的 WSGI 以使用 python 3.7?
- android - 如何测试具有由 android 作为单元测试生成的接口依赖关系的 ViewModel 类
- javascript - 在 Vue.JS 中动态创建锚标签
- c# - 如何在 Excel VSTO C# 中获取已删除范围的值