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

fix(core): Add safeguard for command publishing #11337

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/cli/src/scaling/__tests__/publisher.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ describe('Publisher', () => {
});

describe('publishCommand', () => {
it('should do nothing if not in scaling mode', async () => {
config.set('executions.mode', 'regular');
const publisher = new Publisher(logger, redisClientService, instanceSettings);
const msg = mock<PubSub.Command>({ command: 'reload-license' });

await publisher.publishCommand(msg);

expect(client.publish).not.toHaveBeenCalled();
});

it('should publish command into `n8n.commands` pubsub channel', async () => {
const publisher = new Publisher(logger, redisClientService, instanceSettings);
const msg = mock<PubSub.Command>({ command: 'reload-license' });
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/scaling/pubsub/publisher.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Publisher {
private readonly redisClientService: RedisClientService,
private readonly instanceSettings: InstanceSettings,
) {
// @TODO: Once this class is only ever initialized in scaling mode, throw in the next line instead.
// @TODO: Once this class is only ever initialized in scaling mode, assert in the next line.
if (config.getEnv('executions.mode') !== 'queue') return;

this.logger = this.logger.scoped(['scaling', 'pubsub']);
Expand All @@ -46,6 +46,9 @@ export class Publisher {

/** Publish a command into the `n8n.commands` channel. */
async publishCommand(msg: Omit<PubSub.Command, 'senderId'>) {
// @TODO: Once this class is only ever used in scaling mode, remove next line.
if (config.getEnv('executions.mode') !== 'queue') return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it feels like we are repeating this check. Since the execution mode can't change at runtime, could we not change await this.client.publish to await this.client?.publish instead ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of either execution mode sanity checks or calling methods with an uninitialized client. This is only a temporary fix until we can decouple and dynamically load the Publisher as mentioned here.


await this.client.publish(
'n8n.commands',
JSON.stringify({
Expand Down
Loading