I have an azure function that I am running daily, but for the past few days I noticed that my function is returning old results for example
Result when function ran at 8/16/2023 10:08AM CST (The file name should be the yesterday’s date and current time in PST)
Result when function ran at 8/16/2023 12:17PM CST (notice file name is exactly the same, the current time didn’t change at all)
Result when code ran at 8/17/2023 7:00AM CST
Result when code ran at 8/17/2023 10:15AM CST (exact same result as 7:00AM run)
This problem will fix itself when I make a push on github without changing anything, the code will run perfectly for one iteration, but for the next run it will again start returning results from an older run.
In the code I use node.js’s library luxon to get the current date and time and the code uses it to run certain functions
const { DateTime } = require("luxon");
const { BlobServiceClient } = require("@azure/storage-blob");
const XLSX = require("xlsx");
const start_time = DateTime.now()
.set({ hour: 0, minute: 0, second: 0, millisecond: 0 })
.minus({ days: 1 });
const end_time = DateTime.now()
.set({ hour: 23, minute: 59, second: 59, millisecond: 999 })
.minus({ days: 1 });
const time_now = DateTime.now();
async function uploadFileToBlobStorage(filename, json) {
const connectionString = process.env.blobStorageConnectionString;
const blobServiceClient =
BlobServiceClient.fromConnectionString(connectionString);
const containerName = process.env.blobContainer;
const containerClient = blobServiceClient.getContainerClient(containerName);
const worksheet = XLSX.utils.json_to_sheet(json);
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
// Write workbook to a binary buffer
const excelBuffer = XLSX.write(workbook, {
type: "buffer",
bookType: "xlsx",
});
const blobName = `${time_now
.minus({ days: 1 })
.toFormat("yyyy-LL-dd-hh-mm-ss")}_${filename}.xlsx`;
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
await blockBlobClient.uploadData(excelBuffer, {
blobHTTPHeaders: {
blobContentType:
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
},
});
Could anyone tell me why the function is not returning accurate results.
P.S. -> It works perfectly every time locally.
2
Answers
I have fixed it. Turn's out I was initializing time outside
module.exports
, because of this Azure function wasn't reinitializing time every time it ran, moving the time initialization insidemodule.exports
fixed my issueI made some changes to your code and the blob uploaded successfully to my storage account container.
Code:
.env :
Output:
The Timer Trigger function runs successfully and blob uploaded successfully like below,
The blob uploaded to storage account at Azure Portal as below,
Then, I deployed the above code to my function app and it deployed successfully as below,
I run the function in function app at Azure Portal and it runs successfuly like below,