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

Example of how to integrate into a larger system #495

Closed
vomc opened this issue Nov 18, 2020 · 8 comments
Closed

Example of how to integrate into a larger system #495

vomc opened this issue Nov 18, 2020 · 8 comments

Comments

@vomc
Copy link

vomc commented Nov 18, 2020

Hi,

This is more of a question for an example / documentation links. I am testing out various circuit breakers and opossum is currently on my list. It seems to work well in the toy example provided where you have a simple function but I found that it fails in strange ways if you try to integrated it into a larger system.

For example using breaker.fire() on a class method that calls other class methods seems to fail where it eventually complaints that some method is not available... This is strange and I am not sure why it happens and how I would go about integrating this into a larger system.

In my case I have a class method that calls another method which I am trying to wrap into the CircuitBreaker, i.e. breaker.fire(this.api.someOtherMethod). The someOtherMethod calls a method called post in the other class and the error I get now is [Node] ERROR with circuit breaker TypeError: this.post is not a function

Any links to examples would be appreciated!
Thanks

@lance
Copy link
Member

lance commented Nov 19, 2020

Hi @vomc - thanks for the report. Any chance you can provide a reproducible example in code? I suspect it is related somehow to how JavaScript deals with this.

@dayvsonsales
Copy link

@lance

Same weird behaviour happened with me. I use an dependency injection resolver and make use of classes. I only can use circuit breaker if I define a anonymous function that calls my class method inside it, otherwise it gives an error inside every foreign classes method calls. For example:

const playlistRecommendationService = container.resolve(PlaylistRecommendationService);

const circuitRecommendByCity = new CircuitBreaker(
      playlistRecommendationService.recommendByCity,
      circuitBreakerOptions,
);

const tracks = await circuitRecommendByCity.fire(cityName);

It throws:

TypeError: Cannot read property 'fetchTemperatureInCelsiusByCityName' of undefined
    at Function.<anonymous> (/Users/dayvsonsales/microservices-resiliency-typescript/src/modules/playlists/services/PlaylistRecommendationService.ts:16:52)
    at step (/Users/dayvsonsales/microservices-resiliency-typescript/src/modules/playlists/services/PlaylistRecommendationService.ts:45:23)
    at Object.next (/Users/dayvsonsales/microservices-resiliency-typescript/src/modules/playlists/services/PlaylistRecommendationService.ts:26:53)
    at /Users/dayvsonsales/microservices-resiliency-typescript/src/modules/playlists/services/PlaylistRecommendationService.ts:20:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/dayvsonsales/microservices-resiliency-typescript/src/modules/playlists/services/PlaylistRecommendationService.ts:16:12)
    at Function.PlaylistRecommendationService.recommendByCity (/Users/dayvsonsales/microservices-resiliency-typescript/src/modules/playlists/services/PlaylistRecommendationService.ts:63:16)
    at /Users/dayvsonsales/microservices-resiliency-typescript/node_modules/opossum/lib/circuit.js:521:38
    at new Promise (<anonymous>)
    at CircuitBreaker.call (/Users/dayvsonsales/microservices-resiliency-typescript/node_modules/opossum/lib/circuit.js:497:12)

But if I use anonymous function, as the example below, everything works.

const playlistRecommendationService = container.resolve(
      PlaylistRecommendationService,
);

const circuitRecommendByCity = new CircuitBreaker(
      () => playlistRecommendationService.recommendByCity(cityName),
      circuitBreakerOptions,
);

const tracks = await circuitRecommendByCity.fire();

Also, inside the PlaylistRecommendationService I use a lot of this context, maybe there's some problem with it.

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity.

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity.

@lance
Copy link
Member

lance commented Jan 27, 2023

@lholmquist do you think we can identify an actual issue to work on here? If not, maybe this issue can be closed?

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity.

@iamelevich
Copy link
Contributor

iamelevich commented Mar 23, 2023

@dayvsonsales It's not connected to the library. This is how JS works. So if you want to call it without anonymous function - you should say to function what it should use as this using bind. In your example this should work:

const playlistRecommendationService = container.resolve(PlaylistRecommendationService);

const circuitRecommendByCity = new CircuitBreaker(
      playlistRecommendationService.recommendByCity.bind(playlistRecommendationService),
      circuitBreakerOptions,
);

const tracks = await circuitRecommendByCity.fire(cityName);

@github-actions
Copy link
Contributor

This issue is stale because it has been open 30 days with no activity.

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

5 participants