aws-sdk-js: Failure to scan DynamoDB table in `setInterval` callback

Hello,

I’m experiencing a bizarre issue where I am unable to scan a DynamoDB table from the callback of setInterval. Please see the snippet below:

import AWS from 'aws-sdk';

const activeHosts = {};


// Setup DynamoDB.
const docClient = new AWS.DynamoDB.DocumentClient({
  region: 'us-west-1',
  dynamoDbCrc32: false,
});


function getActiveHosts(cb) {
  const TableName = 'Hosts';
  docClient.scan({ TableName }, (err, data) => {
    if (data.Items) {
      cb(data.Items);
    } else {
      cb([]);
    }
  });
}


function setActiveHosts() {
  getActiveHosts((hosts) => {
    hosts.forEach(({ Host, Config }) => {
      const hostConfigMap = {};
      hostConfigMap[Host] = Config;
      Object.assign(activeHosts, hostConfigMap);
    });
  });
}


// Poll the active hosts configuration periodically, i.e. every 30 seconds.
setInterval(setActiveHosts, 1000 * 30);

If I call setActiveHosts directly after its definition it returns the expected data, but when it’s called from the setInterval callback, I get an empty object.

What could possibly be going on here?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 20 (8 by maintainers)

Most upvoted comments

It’s not configuration that’s exposed to the SDK; any readable stream can be transitioned to flowing mode by attaching a listener to its data event. I’ll look into how we can better document incompatibilities with libraries that coerce streams from paused to flowing mode.