如何在Mac上本地运行计时器触发的Azure函数? [英] How to run Timer-triggered Azure Functions locally on Mac?

查看:63
本文介绍了如何在Mac上本地运行计时器触发的Azure函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to execute a timer-triggered function in my local development environment (Node, OS X) but it seems to require some changes to the HTTP-triggered functions setup I have.

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

Here's the code related to the timer function so far:

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

/cron-job/function.json defines a timer input binding scheduled to run every minute. It also has a reference to the code entry point (compiled from 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": ""
  }
}

当我尝试启动功能应用程序时:

When I try to start the function app:

 ~/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 时,出现另一个错误:

When I add AzureWebJobsStorage setting to the local.settings.json I get another error:

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.

推荐答案

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

After some research I came up with a working setup which I figured I should share.

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

The issues with my original setup were:

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

  1. Not having "AzureWebJobsStorage": "UseDevelopmentStorage=true" in the local.settings.json. I already had a HTTP triggered function up and running but it appears timer trigger requires that setting. For local development when using a storage emulator, UseDevelopmentStorage=true shortcut can be used.

未安装存储模拟器.在Windows上,它似乎是Microsoft Azure SDK的一部分,并且/或者可以作为独立工具安装.但不适用于Mac和Linux.但是,有一个开放源代码可供选择: Azurite ,它将转到

Not having Storage Emulator installed. It seems that on Windows it's part of the Microsoft Azure SDK and/or it can be installed as a standalone tool. It's not available for Mac and Linux though. However there's an open-source alternative available: Azurite, which is going to replace the Storage Emulator.

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

As a reference, I've created a Typescript starter repository which can be extended for writing your own Azure Timer-triggered functions: azure-timer-function-starter-typescript

这篇关于如何在Mac上本地运行计时器触发的Azure函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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