nmarus / node-ews

A simple JSON wrapper for the Exchange Web Services (EWS) SOAP API.
MIT License
116 stars 52 forks source link

Memory leak problems #154

Open wifono opened 1 year ago

wifono commented 1 year ago

Hello. I'm developing app, where I fetch events from Outlook with EWS/Graph ... With MS Graph, everything works well, but when I use EWS, memory and heap usage are increasing, and then I got error that heap is out of memory....

I tried to solve this problem with this solution, but my problem is not solved.... My problem is, that my app fetches events from calendars every 10 seconds. When I disable cron, everything works well and memory and heap are at low numbers ... But I need that cron to do his job....

This is part of my code that is responsible for fetching events....

` if (settings.ews === '1') { const roomService = new MeetingRoomsManagmentService({ Model: app.get('mssqlClient'), name: 'meeting_rooms' }) const rooms = await roomService.find() const roomEmails = rooms.map((room) => room.mroomEmail)

    const configService = new EwsConfigService({
      Model: app.get('mssqlClient'),
      name: 'ews_config'
    })

    const config = await configService.find()

    const currentDate = new Date()
    const currentDayOfWeek = currentDate.getDay()
    const currentDayOfMonth = currentDate.getDate()
    const startOfWeek = new Date(currentDate)
    startOfWeek.setDate(currentDayOfMonth - currentDayOfWeek + 1)
    startOfWeek.setHours(2, 0, 0, 0)
    const endOfWeek = new Date(currentDate)
    endOfWeek.setDate(currentDayOfMonth + (6 - currentDayOfWeek))
    endOfWeek.setHours(25, 59, 59, 999)

    const ewsConfig = {
      username: config[0].mail,
      host: 'https://outlook.office365.com/',
      auth: 'bearer',
      token: globalAccessToken,
      temp: '/tmp'
    }

    const ews = new EWS(ewsConfig)
    const ewsFunction = 'FindItem'
    const ewsArgs = {
      attributes: {
        Traversal: 'Shallow'
      },
      ItemShape: {
        BaseShape: 'Default'
      },
      ParentFolderIds: {
        DistinguishedFolderId: {
          attributes: {
            Id: 'calendar'
          }
        }
      },
      CalendarView: {
        attributes: {
          StartDate: startOfWeek.toISOString(),
          EndDate: endOfWeek.toISOString()
        }
      }
    }
    const formattedEvents = []

    for (let i = 0; i < roomEmails.length; i++) {
      const email = roomEmails[i]
      const ewsSoapHeader = {
        't:ExchangeImpersonation': {
          't:ConnectingSID': {
            't:SmtpAddress': email
          }
        }
      }

      try {
        const result = await ews.run(ewsFunction, ewsArgs, ewsSoapHeader)
        const rootFolder = result.ResponseMessages.FindItemResponseMessage.RootFolder

        if (rootFolder && rootFolder.Items && rootFolder.Items.CalendarItem) {
          const calendarEvents = Array.isArray(rootFolder.Items.CalendarItem)
            ? rootFolder.Items.CalendarItem
            : [rootFolder.Items.CalendarItem]

          for (const event of calendarEvents) {
            if (!event || !event.ItemId) {
              continue
            }

            const formattedEvent = {
              event_id: event.ItemId.attributes.Id,
              changeKey: event.ItemId.attributes.ChangeKey,
              event_subject: event.Subject,
              event_start: dateTime(event.Start).toString(),
              event_end: dateTime(event.End).toString(),
              event_location: event.Location,
              event_organizer: email,
              event_organizer_name: event.Organizer.Mailbox.Name
            }

            formattedEvents.push(formattedEvent)
          }
        }
      } catch (error) {
        console.error('Error fetching calendar events for email:', email, error)
      }
    }

    return formattedEvents
  }`