Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue with nodemon #186

Closed
kenenisa opened this issue Feb 6, 2022 · 2 comments
Closed

issue with nodemon #186

kenenisa opened this issue Feb 6, 2022 · 2 comments

Comments

@kenenisa
Copy link

kenenisa commented Feb 6, 2022

Nodemon works perfectly but when I include const LocalSession = require('telegraf-session-local') it keeps failing to restart like: [nodemon] restarting due to changes... [nodemon] starting 'node index.js' again and again.

I'm using nodemon 2.0.15 and the latest telegraf-session-local version

@TemaSM
Copy link
Contributor

TemaSM commented Feb 7, 2022

Hi @kenenisa

That's an expected behavior of nodemon, because it starts watching changes of files: [nodemon] watching extensions: js,mjs,json.
If you look into simple example:

bot.use((new LocalSession({ database: 'example_db.json' })).middleware())

- Here is database option which defines the path for database file of sessions, for example example_db.json.
telegraf-sessin-local uses underlying library called lowdb to store and manipulate database data.
When you start your bot, lowdb creates new file descriptor for database-file and touches it (look onto file modification date-time just after starting nodemon or node), which simply fires nodemon event like 'file was modified' and nodemon performing restart of your js file, which leads to looped behavior:
--(1)-- Start js file and watch for changes by nodemon --(2)--> JS file started by node --(3)--> Bot started and touched database file --(4)--> Nodemon detected change of json file --(5)--> Nodemon re-executes js file --> (1)

To fix this, please add config for nodemon to ignore json-database file, for example by adding new block into your package.json:

"nodemonConfig": {
  "ignore": ["sessions.json", "example_db.json"]
}

sessions.json - default filename of sessions' database in telegraf-session-local:

* @param {String} [options.database] - Name or path to database file `default:` `'sessions.json'`

@kenenisa
Copy link
Author

Hi @kenenisa

That's an expected behavior of nodemon, because it starts watching changes of files: [nodemon] watching extensions: js,mjs,json. If you look into simple example:

bot.use((new LocalSession({ database: 'example_db.json' })).middleware())

  • Here is database option which defines the path for database file of sessions, for example example_db.json.
    telegraf-sessin-local uses underlying library called lowdb to store and manipulate database data.
    When you start your bot, lowdb creates new file descriptor for database-file and touches it (look onto file modification date-time just after starting nodemon or node), which simply fires nodemon event like 'file was modified' and nodemon performing restart of your js file, which leads to looped behavior:
    --(1)-- Start js file and watch for changes by nodemon --(2)--> JS file started by node --(3)--> Bot started and touched database file --(4)--> Nodemon detected change of json file --(5)--> Nodemon re-executes js file --> (1)
    To fix this, please add config for nodemon to ignore json-database file, for example by adding new block into your package.json:
{
  "...": "... other standard package.json values",
  "nodemonConfig": {
    "ignore": ["sessions.json", "example_db.json"]
  }
}

sessions.json - default filename of sessions' database in telegraf-session-local:

* @param {String} [options.database] - Name or path to database file `default:` `'sessions.json'`

The issue is resolved. It's now working as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants