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

refactor: prefer eventService.storeEvent methods #4830

Merged
merged 8 commits into from
Sep 27, 2023

Conversation

nunogois
Copy link
Member

@nunogois nunogois commented Sep 25, 2023

https://linear.app/unleash/issue/2-1403/consider-refactoring-the-way-tags-are-fetched-for-the-events

This adds 2 methods to EventService:

  • storeEvent;
  • storeEvents;

This allows us to run event-specific logic inside these methods. In the case of this PR, this means fetching the feature tags in case the event contains a featureName and there are no tags specified in the event.

This prevents us from having to remember to fetch the tags in order to store feature-related events except for very specific cases, like the deletion of a feature - You can't fetch tags for a feature that no longer exists, so in that case we need to pre-fetch the tags before deleting the feature.

This also allows us to do any event-specific post-processing to the event before reaching the DB layer.
In general I think it's also nicer that we reference the event service instead of the event store directly.

There's a lot of changes and a lot of files touched, but most of it is boilerplate to inject the eventService where needed instead of using the eventStore directly.

Hopefully this will be a better approach than #4729

@vercel
Copy link

vercel bot commented Sep 25, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Ignored Deployments
Name Status Preview Comments Updated (UTC)
unleash-docs ⬜️ Ignored (Inspect) Visit Preview Sep 27, 2023 1:22pm
unleash-monorepo-frontend ⬜️ Ignored (Inspect) Visit Preview Sep 27, 2023 1:22pm

Copy link
Contributor

@gastonfournier gastonfournier left a comment

Choose a reason for hiding this comment

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

This is looking really nice, I just pointed out a few details that we can check tomorrow, sorry for the late review!

Comment on lines +18 to +21
const eventService = new EventService(
{ eventStore, featureTagStore },
{ getLogger },
);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd argue that group service should not need to know how to construct an event service, maybe moving event service as a parameter to createGroupService would help with my other comment

Copy link
Member Author

Choose a reason for hiding this comment

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

I disagree with this one due to the fact that other createXService methods follow the same approach: They create the needed services inside the methods instead of accepting them as arguments.

If we decide to refactor this I would prefer to target all of those methods and do it in a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

I can explain the details behind createXService methods. Those are "composition roots" for each service. So they hide the information how each service is created. Some of our services depend on other services (which is often the consequence of not have read models). If creating a dependent service is difficult I usually create another composition root (createXService) method for the dependent service and call it from my composition root. On the other hand some services are trivial to instantiate or I didn't have time to migrate them to compositon roots. So my recommendation is to decide on a case by case basis. If you feel the pain of constructing a complex graph of objects: create a compositon root for a dependent service.

Copy link
Contributor

Choose a reason for hiding this comment

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

they hide the information how each service is created

I think this implies that they should not know how other services are created other than themselves. It acts as a Dependency Injection layer, where services get services injected rather than creating them on each composition root, which might lead to maintainability issues when having to modify how a service is created...

On the other hand some services are trivial to instantiate or I didn't have time to migrate them to composition roots.

I believe this one is simple, but I'm ok with whatever Nuno decides to do here, we'll have a chat and move this forward.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, in other places we prefer injecting the service: https://github.com/Unleash/unleash/pull/4830/files#diff-0f55a0a1460b7ad5fd21da38762c259c1580e877e4b6b746e0032ad27776c886R93 but as Mateusz said, it can be considered tech debt for later

src/lib/services/addon-service.ts Show resolved Hide resolved
src/lib/services/event-service.ts Outdated Show resolved Hide resolved
src/lib/services/event-service.ts Outdated Show resolved Hide resolved
src/lib/types/events.ts Show resolved Hide resolved
src/test/e2e/api/admin/event.e2e.test.ts Show resolved Hide resolved
Copy link
Contributor

@gastonfournier gastonfournier left a comment

Choose a reason for hiding this comment

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

Left some comments, but I believe this one is good to go 🚀

@nunogois nunogois force-pushed the refactor-event-service-store-event branch from c2cbad3 to f70791f Compare September 27, 2023 13:15
@nunogois nunogois merged commit 87d9497 into main Sep 27, 2023
8 checks passed
@nunogois nunogois deleted the refactor-event-service-store-event branch September 27, 2023 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants