How to run Timer-triggered Azure Functions locally on Mac?

夙愿已清 提交于 2020-12-15 04:30:17

问题


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 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

I get an error:

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.

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.

回答1:


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

The issues with my original setup were:

  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.

  2. 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.

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



来源:https://stackoverflow.com/questions/64492603/how-to-run-timer-triggered-azure-functions-locally-on-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!