grammyjs / grammyjs/runner

distribute method should not cached ThreadPool

Open
#24 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
27
Forks
5
PR merge metrics
No merged PRs in 30d

Description

the issue code

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

Reproduction code

bot.js

```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

```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 😳

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.