0

I'm a beginner and I have configured a lambda function with a timeout of 10 minutes and I want to run a certain function inside my lambda function with 30 second time interval. For this I used a 'setInterval()' function and my code look like this, and when I test it Function 1 executes but the function 2 which has to execute every 30 seconds doesn't work. How to fix this?

import { default as fetch, Request } from 'node-fetch';

const GRAPHQL_ENDPOINT = process.env....;
const GRAPHQL_API_KEY = process.env....;


/**
 * @type {import('@types/aws-lambda').APIGatewayProxyHandler}
 */
export const handler = async (event) => {
  console.log(`EVENT: ${JSON.stringify(event)}`);

  
  let statusCode = 200;
  let body;
  let response;
  
  
  async function Func2(){
    
    try {
     // function 2 this is called in every 30 seconds, and this is a async function calling a graphql endpoint
  };
    }catch(error){
      statusCode = 400;
    body = {
      errors: [
        {
          status: response.status,
          message: error.message,
          stack: error.stack
        }
      ]
    };
   }
  }
  
  try {
    
    // Function 1, this is an async function calling a graphql endpoint
    
    setInterval(() => Func2(), 30000);
    
  } catch (error) {
    statusCode = 400;
    body = {
      errors: [
        {
          status: response.status,
          message: error.message,
          stack: error.stack
        }
      ]
    };
    return {
    statusCode,
    body: JSON.stringify(body)
  };
 }
  
   return {
     statusCode,
     body: JSON.stringify(body)
   };
 };
hewa
  • 43
  • 7
  • 1
    What does "doesn't work" mean? What did you expect to happen, and what actually happened? – fedonev Jan 11 '23 at 08:02
  • @fedonev I want to run my 'Func2()' with a time interval of 30 seconds for 10 minutes (which is the timeout of my lambda) but when I test the lambda it doesn't run for 10 minutes as I expected instead it returns and exit after a short period of time – hewa Jan 11 '23 at 08:21
  • Your `Func2` changes some variables, but you don't use these variables afterwards – Konrad Jan 11 '23 at 08:31
  • @Konrad Do you mean whether I should check these variables before returning the function? – hewa Jan 11 '23 at 08:39
  • 1
    @hewa, this answer might help you:) https://stackoverflow.com/a/42183439/12326605 – Arpit Jain Jan 11 '23 at 13:51

0 Answers0