我正在处理一个使用计时器触发器并与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时间触发器函数与服务总线代码在部署时被删除以及如何纠正此问题。
感谢您的帮助!
1条答案
按热度按时间6uxekuva1#
在使用你的代码时,我在部署过程中遇到了下面的错误:
我对你的代码做了一些修改,在package.json中添加了
"@azure/service-bus": "^7.9.2"
,在host.json中的"version": "[4.*, 5.0.0)"
重新部署了函数。package.json:
host.json:
timerTrigger
:输出:
本地:
Azure: