微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Mac上本地运行计时器触发的Azure函数?

如何解决如何在Mac上本地运行计时器触发的Azure函数?

我想在本地开发环境(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.

解决方法

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

我的原始设置存在以下问题:

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

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

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

,

要在 Mac 上本地运行 Azure 计时器功能,您可以在 local.settings.json 中为 AzureWebJobsStorage 提供存储帐户连接字符串。设置"AzureWebJobsStorage": "<storage account connection string>"

您可以从 Azure 门户获取存储帐户连接字符串。在 Azure 门户中创建存储帐户。转到存储帐户访问密钥并复制连接字符串。

在 Windows 上,设置 "AzureWebJobsStorage": "UseDevelopmentStorage=true""AzureWebJobsStorage": "<storage account connection string>" 都可以。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。