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

distribute method should not cached ThreadPool #24

Open
malacca opened this issue Nov 22, 2024 · 3 comments
Open

distribute method should not cached ThreadPool #24

malacca opened this issue Nov 22, 2024 · 3 comments

Comments

@malacca
Copy link

malacca commented Nov 22, 2024

the issue code

https://github.com/grammyjs/runner/blob/main/src/distribute.ts#L46

Reproduction code

bot.js

const { Bot, session } = require('grammy');
const {
  run,
  distribute,
  BotWorker,
  sequentialize,
} = require('@grammyjs/runner');
const {
  isMainThread,
  setEnvironmentData,
  getEnvironmentData,
} = require('node:worker_threads');

function getSessionKey(ctx) {
  return ctx.chat?.id.toString();
}

function initialSessionValue() {
  return {};
}

function ComposeBot(bot, inThread) {
  bot.on('msg', async (ctx) => {
    console.log(inThread, ctx);
  });
  return bot;
}

async function StartBot(token, { workerCount = 4, ...config } = {}) {
  const bot = new Bot(token, config);
  bot.use(sequentialize(getSessionKey));
  bot.use(session({ getSessionKey, initial: initialSessionValue }));

  if (workerCount > 1) {
    setEnvironmentData('BotToken', token);
    bot.use(distribute(__filename));
  } else {
    ComposeBot(bot, false);
  }
  const runner = run(bot);
  const stopRunner = () => runner.isRunning() && runner.stop();
  process.once('SIGINT', stopRunner);
  process.once('SIGTERM', stopRunner);
}

module.exports = {
  StartBot,
};

if (!isMainThread) {
  const token = getEnvironmentData('BotToken');
  ComposeBot(new BotWorker(token), token);
}

runner.js

const { StartBot } = require('./bot');
StartBot('Token__A');
StartBot('Token__B');

Since the child threads always use the same file
the cache will cause the child threads to not be created correctly 😳

@KnorpelSenf
Copy link
Member

This isn't at all how this library is supposed to be used.

What's your point? Are you saying that you want to create a new worker thread for each new update?

@malacca
Copy link
Author

malacca commented Nov 22, 2024

i want create a wrapper of grammy Bot

that support config workerCount

https://github.com/grammyjs/runner/blob/main/src/distribute.ts#L46

when I start multiple bots by wrapper function,

Because specifier is always the same file, only the first create ThreadPool really,

@KnorpelSenf
Copy link
Member

KnorpelSenf commented Nov 22, 2024

Yeah the distribute middleware wasn't written with the fact in mind that some people could use it to process updates from unrelated bots. I'd be happy to accept a pull request that adds this feature!

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

No branches or pull requests

2 participants