skip to Main Content

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)

enter image description here

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)
enter image description here

Result when code ran at 8/17/2023 7:00AM CST
enter image description here

Result when code ran at 8/17/2023 10:15AM CST (exact same result as 7:00AM run)
enter image description here

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


  1. Chosen as BEST ANSWER

    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 inside module.exports fixed my issue

    module.exports = async function (context, req) {
      try {
        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();
        .
        .
        .
        .
    

  2. I made some changes to your code and the blob uploaded successfully to my storage account container.

    Code:

    const { DateTime } = require("luxon");
    const { BlobServiceClient } = require("@azure/storage-blob");
    const XLSX = require("xlsx");
    
    module.exports = async function (context, myTimer) {
      try {
        console.log("blobStorageConnectionString:", process.env.blobStorageConnectionString);
        console.log("blobContainer:", process.env.blobContainer);
    
        const filename = "example";
    
        const jsonData = [
          { name: "Alice", age: 25 },
          { name: "Bob", age: 30 }
        ];
    
        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();
    
        await uploadFileToBlobStorage(filename, jsonData, time_now);
    
        context.log("Function executed successfully.");
      } catch (error) {
        context.log.error("An error occurred:", error.message);
      }
    };
    
    async function uploadFileToBlobStorage(filename, json, time_now) {
      try {
        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");
    
        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",
          },
        });
    
        console.log("File uploaded successfully:", blobName);
      } catch (error) {
        console.error("An error occurred:", error.message);
      }
    }
    
    

    .env :

    blobStorageConnectionString="<Connec_string>"
    blobContainer="<container_name>"
    
    

    Output:

    The Timer Trigger function runs successfully and blob uploaded successfully like below,

    enter image description here

    The blob uploaded to storage account at Azure Portal as below,

    enter image description here

    Then, I deployed the above code to my function app and it deployed successfully as below,

    enter image description here

    I run the function in function app at Azure Portal and it runs successfuly like below,

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search