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 info packet send on stop #1101

Merged
merged 2 commits into from
Jul 10, 2022
Merged

Fix info packet send on stop #1101

merged 2 commits into from
Jul 10, 2022

Conversation

Ubitso
Copy link

@Ubitso Ubitso commented Jul 5, 2022

📝 Description

Use case is basic redeploy moleculer app with consistent event flow.
On redeploy we have large amount of ServiceIsNotAvailable errors.

Minimal reproduction repository:
https://github.com/Ubitso/moleculer-sd-issue

🎯 Relevant issues

No

💎 Type of change

I've changed stop function in service-broker. Error was in regenerateLocalRawInfo in 'registry.js'. During broker stop process broker should send info packet with empty array of services. But it does send packet with current available services.
So we should properly send info packet, and then after delay set broker.stopping = true, otherwise remote services will send requests to registred services when it's locked by 'broker.stopping = true'

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

📜 Example code

I've tried to reproduce using test but i cant get same errors. I think it happens only if services is in separate processes.
Please look at my test, if possible to reproduce in one process please give me advice to how achieve that.

describe("Test proper broker stop", () => {
	const broker = new ServiceBroker({
		transporter: "Redis",
		registry: {
			strategy: "RoundRobin",
			preferLocal: false,
			discoverer: {
				type: 'Local'
			}
		},

		tracking: {
			enabled: false
		}
	});

	jest.setTimeout(30000)

	it("Should not throw service is not available error", async () => {
		const errors = []

		broker.createService({
			name: "consumer",

			started() {
				const ctx = this.broker.ContextFactory.create(
					this.broker,
					null,
					{},
					{ caller: this.fullName }
				);

				let i = 0
				const interval = setInterval(async () => {
					if (i < 1000000000) {
						try {
							console.log('find called')
							await ctx.call("producer.find", {});
						} catch (err) {
							console.error(err)
							errors.push(err)
						}
					} else {
						console.warn('clean interval')
						clearInterval(interval)
					}
				}, 1);
			}
		});

		broker.createService({
			name: "producer",

			started() {},

			actions: {
				async find() {

					return new Promise((resolve) => {
						setTimeout(() => {
							return resolve({
								data: "some result"
							});
						}, 1);
					});
				},
			}
		});

		await broker.start();

		await broker.Promise.delay(2000)

		const brokerStopPromise = broker.stop();
		console.log('Broker stop called')

		await brokerStopPromise

		console.log(errors)
	});
});

🚦 How Has This Been Tested?

I tested on my reproduction repo on versions 0.14.19 also on 0.14.21.
Also I fixed current test with fake timers because it starts to fail after my changes.

No

🏁 Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have commented my code, particularly in hard-to-understand areas

@icebob icebob requested review from icebob and AndreMaz July 10, 2022 16:27
Copy link
Member

@icebob icebob left a comment

Choose a reason for hiding this comment

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

Nice catch, thanks for your PR!

@icebob icebob merged commit 84ef938 into moleculerjs:master Jul 10, 2022
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

Successfully merging this pull request may close these issues.

2 participants