Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Not polling updates #74

Closed
phun-ky opened this issue Nov 29, 2017 · 8 comments
Closed

Not polling updates #74

phun-ky opened this issue Nov 29, 2017 · 8 comments

Comments

@phun-ky
Copy link

phun-ky commented Nov 29, 2017

With this code:

'use strict';
/*eslint no-console:0 quotes:0*/

import launchDarkly from 'ldclient-node';
import Bunyan from 'bunyan';
import winston from 'winston';
const logger = new Bunyan({ name: 'Server' });
const ldclient = launchDarkly.init('<my key>', {
  logger: new winston.Logger({
    level: 'debug',
    transports: [new winston.transports.Console()]
  }),
  stream: false,
  timeout: 3
});

ldclient.once('ready', function() {
  console.log('ldclient ready');
  ldclient.variation('<myflag>', { key: '[email protected]' }, false, function(err, showFeature) {
    console.log(err);
    if (showFeature) {
      console.log('SHOW FEATURE', showFeature);
    } else {
      console.log('HIDE FEATURE', showFeature);
    }
  });
});

ldclient.once('error', function(err) {
  console.log(err);
});

I only get the console.log for SHOW/HIDE FEATURE once, even though I see the polling log flood. I am enabling/disabling the feature flag in the dashboard, nothing happens..

@apucacao
Copy link
Contributor

Hi @phun-ky ,

Unless I am mistaken, that code only calls ldclient.variation once. Have you tried wrapping the call to ldclient.variation in setTimeout?

The default polling interval is 1 second, so changes you make on your dashboard should take at ~1s to show up in your client.

Let me know if that helps.

@apucacao
Copy link
Contributor

Also, I'm curious why you decided to use polling mode here. We generally recommend streaming instead of polling since it offers many benefits like instant updates of your flag configurations in your app.

Happy to answer any questions as well :)

@phun-ky
Copy link
Author

phun-ky commented Nov 29, 2017 via email

@apucacao
Copy link
Contributor

When you get a chance, could share the code you tried with streaming?

I think the reason polling didn't output updates is because variation is only called once. But I would like to understand why streaming wasn't working for you, so that you can start using that mode.

@phun-ky
Copy link
Author

phun-ky commented Nov 29, 2017 via email

@apucacao
Copy link
Contributor

apucacao commented Dec 1, 2017

I mentioned that because in your original comment you said, "I only get the console.log for SHOW/HIDE FEATURE once…".

Were you seeing errors in the logs when using streaming?

@phun-ky
Copy link
Author

phun-ky commented Dec 13, 2017

Sorry for the late reply, I've managed to do what I want with this code (this code is to fetch flags and decide server routes on my node backend:

/**
 * The event handler for featuerToggle
 * @type {FeatureFlagEventsEmitter}
 */
export const featureFlagEvents = new FeatureFlagEventsEmitter();

logger.info(
  `Attempting to initialize Launch Darkly initialized with sdk key: ${process.env.FEATURE_TOGGLE_LAUNCH_DARKLY_SDK_KEY}`
);

if (process.env.FEATURE_TOGGLE_LAUNCH_DARKLY_SDK_KEY) {
  featureToggleClient = require('ldclient-node').init(process.env.FEATURE_TOGGLE_LAUNCH_DARKLY_SDK_KEY, {
    timeout: 3,
    stream: false
  });

  featureToggleClient.once('ready', function() {
    logger.info(`Launch Darkly initialized with sdk key: ${process.env.FEATURE_TOGGLE_LAUNCH_DARKLY_SDK_KEY}`);
    setInterval(() => {
      featureToggleInterval = featureToggleClient.all_flags({ key: '[email protected]' }, processFlags);
    }, 30000);
  });
}

In the first versions of this code, stream: true was set, but that stopped working somehow, so I had to swtich to stream:false, and then it worked.

Which is weird because this code (that is used in an API route) works with streaming:

const featureToggleClient = require('ldclient-node').init(process.env.FEATURE_TOGGLE_LAUNCH_DARKLY_SDK_KEY, {
  // this is seconds..
  timeout: 3
});

const fetchAllFlags = () => {
  return new Promise((resolve, reject) => {
    featureToggleClient.once('ready', function() {
      featureToggleClient.all_flags({ key: '[email protected]' }, (err, flagMap) => {
        if (err) {
          reject(err);
        }
        resolve(processFlagsWithoutEmit(err, flagMap));
      });
    });
  });
};

fetchAllFlags()
  .then(flags => {
    logger.info('Feature flags fetched!');
    featureToggleClient.close();
    return res.status(200).json(flags);
  })
  .catch(err => {
    logger.warning(`Error fetching feature flags, returning default flags! ${JSON.stringify(err)}`);
    featureToggleClient.close();
    return res.status(200).json(flags);
  });

@apucacao
Copy link
Contributor

Hi @phun-ky ,

I'm sorry for the late response here.

Are you still unable to use streaming in both of your use cases? When you try streaming mode, do you get any errors from the SDK?

Also, as a side note, we've released a newer version of the SDK with a few improvements, one of which is clearer errors.

eli-darkly added a commit that referenced this issue May 10, 2018
add variation index to feature events and summary counters
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants