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

Question about process managers and remote API calls #481

Open
adampointer opened this issue Mar 24, 2022 · 2 comments
Open

Question about process managers and remote API calls #481

adampointer opened this issue Mar 24, 2022 · 2 comments

Comments

@adampointer
Copy link

Hello,

First off, thanks for all the great work being done in this repo!

I have a process manager which responds to a user initiated event from aggregate A and in response will fire off multiple commands to aggregate B before finally sending another command to aggregate A. Within this flow, I need some data from some remote APIs. This data will directly effect the content of the commands dispatched from the process manager.

My question is where best to call the logic which calls out to the remote APIs? I can either put it in aggregate A and record the results in the domain event which initiates the process manager, or simply keep it within the process manager. I am leaning towards doing it in the aggregate as then the API call result is persisted in an event. However this feels like I am bloating the aggregate code. Is there another alternative or best practice that I am missing?

Thoughts?

@mbuhot
Copy link

mbuhot commented Mar 24, 2022

(Not a maintainer, just a happy Commanded user here).

Some patterns I've used with an API-heavy project using commanded:

Attaching external data to a command can be done with the Command Enrichment middleware: https://gist.github.com/slashdotdash/811d754951b2573a82ff14fe8506012e. That works well for remote read operations.

For external writes, we route the command to an explicit Handler module, where it can make the API calls and pass the request/response along with the command to a function in the aggregate module.

We don't do any remote API calls in process managers, or dispatch commands who's handlers make API calls from process managers. Instead we enqueue Oban jobs from event handlers to ensure that the process manager isn't a bottleneck (due to the shared event store subscription for all instances of a Process Manager), and allows for the command to be retried if dispatch fails due to the external API being unavailable. The downside is we don't get the the process manager state handling or causation_id/correlation_id values being automatically propagated between events/commands.

@adampointer
Copy link
Author

Thanks @mbuhot the command enrichment middleware looks to be exactly what I was looking for. It will nicely separate out the remote API logic from my aggregate and PM modules.

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

No branches or pull requests

2 participants