typescript Azure TimerTrigger函数与服务总线代码部署问题

gopyfrb3  于 2023-10-22  发布在  TypeScript
关注(0)|答案(1)|浏览(125)

我正在处理一个使用计时器触发器并与Azure服务总线交互的Azure函数。当函数只包含计时器触发器代码时,它最初部署成功。

import { app, InvocationContext, Timer } from "@azure/functions";

export async function timerTrigger5(myTimer: Timer, context: InvocationContext): Promise<void> {
    context.log('Timer function processed request.');
}

app.timer('timerTrigger5', {
    schedule: '0 */5 * * * *',
    handler: timerTrigger5
});

但是,一旦我添加与Azure服务总线相关的代码,然后部署,部署似乎是成功的,但部署的功能从Azure门户中消失,并且重新部署的版本不会出现。

import { app, InvocationContext, Timer } from "@azure/functions";
import { ServiceBusClient } from "@azure/service-bus";

export async function timerTrigger5(
  myTimer: Timer,
  context: InvocationContext
): Promise<void> {
  // Service Bus configuration
  const serviceBusConnectionString = "Endpoint=.......";
  const queueName = "xyz";

  // Array of data
  const FX_EXCHANGE_CURRENCIES = [
    "USD",
    "GBP",
    "EUR",
    // ... other currencies
  ];

  // Initialize Service Bus client and sender
  const serviceBusClient = new ServiceBusClient(serviceBusConnectionString);
  const sender = serviceBusClient.createSender(queueName);

  // Loop through data and send to Service Bus
  for (const currency of FX_EXCHANGE_CURRENCIES) {
    const message = {
      body: currency,
    };
    await sender.sendMessages(message);

    context.log(`Sent ${currency} to the Service Bus Queue.`);
  }

  await sender.close();
  await serviceBusClient.close();

  // Log timestamp
  const timeStamp = new Date().toISOString();

  if (myTimer.isPastDue) {
    context.log("Timer function is running late!");
  }

  context.log("Timer trigger function ran!", timeStamp);
}

app.timer("timerTrigger5", {
  schedule: "0 */5 * * * *",
  handler: timerTrigger5,
});

host.json

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.*, 4.0.0)"
  }
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=dev...;AccountKey=zoty...;EndpointSuffix=core.windows.net",
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "outDir": "dist",
    "rootDir": ".",
    "sourceMap": true,
    "strict": false,
    "esModuleInterop": true
  }
}

我需要function.json吗?如果是,那么将沿着其他json文件放在何处,或者放在src/function/中。看起来怎么样?或者问题出在别的地方?
我希望了解为什么Azure时间触发器函数与服务总线代码在部署时被删除以及如何纠正此问题。
感谢您的帮助!

6uxekuva

6uxekuva1#

在使用你的代码时,我在部署过程中遇到了下面的错误:

我对你的代码做了一些修改,在package.json中添加了"@azure/service-bus": "^7.9.2",在host.json中的"version": "[4.*, 5.0.0)"重新部署了函数。

package.json:

"dependencies": {
    "@azure/functions": "^4.0.0",
    "@azure/service-bus": "^7.9.2",
    "azure-functions": "^1.0.2"
  }

host.json:

"extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[4.*, 5.0.0)"
  }

timerTrigger

const serviceBusConnectionString = '';
const topicName = ' ';

export async function timerTrigger1(myTimer: Timer, context: InvocationContext): Promise<void> {
    try {
        context.log('Timer function processed request.');

        // Create a ServiceBusClient using the connection string
        const serviceBusClient = new ServiceBusClient(serviceBusConnectionString);

        // Create a sender for the topic
        const sender = serviceBusClient.createSender(topicName);

        try {
            // Create a message to send
            const message = {
                body: 'Your message content here',
                label: 'MyLabel', // You can specify a label if needed
            };

            // Send the message
            await sender.sendMessages(message);
            context.log('Message sent to Service Bus topic:', message.body);
        } finally {
            // Close the sender and the client when done
            await sender.close();
            await serviceBusClient.close();
        }
    } catch (error) {
        if (context && context.log) {
            context.log('An error occurred:', error);
        }
        // Use myTimer to signal a failure
        myTimer.isPastDue = true;
    }
}

app.timer('timerTrigger1', {
    schedule: '0 */5 * * * *',
    handler: timerTrigger1,
});

输出:
本地:

Azure:

相关问题