I'm working on an Azure Function that uses a timer trigger and interacts with Azure Service Bus. The function initially deploys successfully when it contains only the timer trigger code.
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
});
**However, as soon as I add code related to Azure Service Bus,and then deploy, deployment seems to be successful, but the deployed function disappears from the Azure portal, and the redeployed version doesn't appear. **
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
}
}
Do I need any function.json ? if yes then where to put along with other json files or into src/function/.. and how does it look like ? Or the problem lies somewhere else?
I'd appreciate any insights into why the Azure time trigger Function with Service Bus code is getting deleted upon deployment and how to rectify this issue.
Thank you for your assistance!
While using your code I faced an error below during deployment :-
I made some changes to your code and added
"@azure/service-bus": "^7.9.2"in package.json, and"version": "[4.*, 5.0.0)"in host.json redeployed the function.package.json:
host.json:
timerTrigger:Output:
Local:
Azure: