Genesys Cloud - Developer Community!

 View Only

Sign Up

Expand all | Collapse all

Disabling live reload config does not unwatch config file

  • 1.  Disabling live reload config does not unwatch config file

    Posted 05-21-2025 05:45
    Edited by Johan Cantens 05-21-2025 06:09

    I'm using the purecloud platform sdk javascript client, and am trying to disable the live config reload.

    In the README two ways to do this are documented.
    1. Set `client.config.live_reload_config` to false
    2. Set `live_reload_config` to false inside `.genesyscloudjavascript\config`

    Method 1


    If I disable it via **method 1**, it is already too late because live reload is already configured inside the `Configuration` constructor:

    const client = new platformClient.ApiClientClass(); // <=== Live reload already triggered here
    client.config.live_reload_config = false; 

    Relevant source code is here.
    This is problematic because live reload calls `fs.watchFile` and will never call `fs.unwatchFile` which eventually can lead to these kind of errors: 
    (node:12724) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 change listeners added to [StatWatcher]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit

    try {
    const fs = require('fs');
    fs.watchFile(this.configPath, { persistent: false }, (eventType, filename) => {
     this.updateConfigFromFile();
     if (!this.live_reload_config) {
      fs.unwatchFile(this.configPath); // <=== Never called
     }
    });
    }

    Method 2

    If I disable it via **method 2**, it works but **only** if I use the config file in the default location. If I use a different location (which should work via `client.config.setConfigPath(/*location*/)`), it does not work because again the default file is already being watched in the constructor of `Configuration`.

    So if I set `live_reload_config` to false in the non-default config file. The file will be watched and immediately unwatched, **which is correct**:

    fs.watchFile(this.configPath, { persistent: false }, (eventType, filename) => {
    this.updateConfigFromFile();
    if (!this.live_reload_config) {
     fs.unwatchFile(this.configPath); // <=== New config file will be unwatched
    }
    });

    **BUT** the default file is still being watched (and will trigger updates if it changes?) and never unwatched, leading to the same problem of unclosed resources.


    Conclusion


    Is there any way to completely disable live reload config? I want to disable it because I don't use it, and leads to unclosed resources because fs.unwatch is not called.


    #PlatformSDK

    ------------------------------
    Johan Cantens
    Independant Genesys Consultant & Data Architect
    ------------------------------



  • 2.  RE: Disabling live reload config does not unwatch config file

    Posted 05-21-2025 13:21

    Hello,

    Are you trying to load the config from a file and not have auto/live reload?

    Or no config file and no reload?

    Regards,



    ------------------------------
    Jerome Saint-Marc
    Senior Development Support Engineer
    ------------------------------



  • 3.  RE: Disabling live reload config does not unwatch config file

    Posted 05-22-2025 02:27

    Hi,

    No config file and no reload. 

    But in the current implementation of the library the default config file is automatically watched in the constructor of Configuration.

    Currently I can only disable auto reload after Configuration is initialized.

     

    So I should be able to either:

    - Specify earlier that I don't want to auto reload

    - Or the file already being watched with fs.watchFile should be unwatched/closed when I disable auto reload so there are no unused open listeners in the node process.

    Regards,



    ------------------------------
    Johan Cantens
    Independant Genesys Consultant & Data Architect
    ------------------------------



  • 4.  RE: Disabling live reload config does not unwatch config file

    Posted 05-22-2025 02:50

    If it helps, this is my untested code changes that I think should fix the problem.

    If you want I could test it properly and create a pull request.

    diff --git a/build/src/purecloud-platform-client-v2/configuration.js b/build/src/purecloud-platform-client-v2/configuration.js
    index 36fe69d6..3c77cd80 100644
    --- a/build/src/purecloud-platform-client-v2/configuration.js
    +++ b/build/src/purecloud-platform-client-v2/configuration.js
    @@ -29,7 +29,7 @@ class Configuration {
       }
       this.refresh_access_token = true;
       this.refresh_token_wait_max = 10;
    -  this.live_reload_config = true;
    +  this._live_reload_config = true;
       this.host;
       this.environment;
       this.basePath;
    @@ -39,6 +39,7 @@ class Configuration {
       this.logger = new Logger();
       this.setEnvironment();
       this.liveLoadConfig();
    +  this.listener = undefined;
      }
     
      liveLoadConfig() {
    @@ -51,11 +52,19 @@ class Configuration {
        if (this.live_reload_config && this.live_reload_config === true) {
         try {
          const fs = require('fs');
    -     fs.watchFile(this.configPath, { persistent: false }, (eventType, filename) => {
    +
    +     // This could happen if config path changed via setConfigPath()
    +     // Unsubscribe to previous listener so we aren't listening to multiple files
    +     if (this.listener) {
    +       this.listener.close();
    +     }
    +
    +     this.listener = fs.watchFile(this.configPath, { persistent: false }, (eventType, filename) => {
           this.updateConfigFromFile();
    -      if (!this.live_reload_config) {
    -       fs.unwatchFile(this.configPath);
    -      }
    +      // No longer needed because will unsubscribe in setter of live_reload_config
    +      // if (!this.live_reload_config) {
    +      //  fs.unwatchFile(this.configPath);
    +      // }
          });
         } catch (err) {
          // do nothing
    @@ -67,6 +76,18 @@ class Configuration {
       this.configPath = '';
      }
     
    + set live_reload_config(enabled) {
    +   this._live_reload_config = enabled;
    +
    +   if (!enabled && this.listener) {
    +     this.listener.close();
    +   }
    + }
    +
    + get live_reload_config() {
    +  return this._live_reload_config;
    + }
    +
      setConfigPath(path) {
       if (path && path !== this.configPath) {
        this.configPath = path;
     



    ------------------------------
    Johan Cantens
    Independant Genesys Consultant & Data Architect
    ------------------------------



  • 5.  RE: Disabling live reload config does not unwatch config file

    Posted 05-22-2025 08:26

    Thank you. I'll bring this to the attention of the SDK team and will update this post when I have some news.

    Regards,



    ------------------------------
    Jerome Saint-Marc
    Senior Development Support Engineer
    ------------------------------



  • 6.  RE: Disabling live reload config does not unwatch config file

    Posted 06-10-2025 06:54

    Hi Jerome

    Do you have an update?

    Regards, Johan



    ------------------------------
    Johan Cantens
    Independant Genesys Consultant & Data Architect
    ------------------------------



  • 7.  RE: Disabling live reload config does not unwatch config file

    Posted 06-10-2025 09:57

    Hello,

    Not yet. We are working on a round of features and issues. We should take a look at this one in the next round.

    Regards,



    ------------------------------
    Jerome Saint-Marc
    Senior Development Support Engineer
    ------------------------------



  • 8.  RE: Disabling live reload config does not unwatch config file

    Posted 06-10-2025 10:15
    Hello Jerome

    Thank you for the update! is it possible to get a date ? Because this has quit some impact for us.

    kind regards,
    Johan


    Johan Cantens
    Senior Application Developer

     Tiensesteenweg 168, bus 401 blok C, 3800 Sint-Truiden





  • 9.  RE: Disabling live reload config does not unwatch config file

    Posted 06-10-2025 11:29

    I hope we can start having a look at it some time next week.



    ------------------------------
    Jerome Saint-Marc
    Senior Development Support Engineer
    ------------------------------



  • 10.  RE: Disabling live reload config does not unwatch config file

    Posted 06-23-2025 05:07

    Hello,

    We have made a fix in the last SDK version - Javascript SDK 225.0.0

    The file should now be properly "unwatched" - when live_reload_config is set to false or when the config path is null/undefined/empty string.

    Regards,



    ------------------------------
    Jerome Saint-Marc
    Senior Development Support Engineer
    ------------------------------



  • 11.  RE: Disabling live reload config does not unwatch config file

    Posted 06-11-2025 06:47

    I don't think this exists: 

    this.listener.close();


    ------------------------------
    Jerome Saint-Marc
    Senior Development Support Engineer
    ------------------------------