Hi,
We need help with Local Events in ABP.IO. We are performing certain notifications when an entity is created or updated. Inside the handleEvent method, we have used a try-catch block and are not throwing any errors. However, this ILocalEvent handling is causing performance issues, and our APIs are taking around 1.5 seconds to respond. One of the handlers performs an insert operation.
We have tried changing our handler code to the following:
`
public async Task HandleEventAsync(EntityCreatedEventData<MyEntity> eventData)
{
// Offload the event handling to a separate thread
await Task.Run(async () =>
{
// Our event handling logic here, notification or database operation
});
}`
Does this look good to you?
Can you provide any insights or suggestions to improve the performance? is await Task.Run(async ()).. is required? is seperate ouw required inside the handler?
-
ABP Framework version: v8.3.0
-
UI Type: Angular
-
Database System: EF Core (PostgreSQL)
-
Tiered (for MVC) or Auth Server Separated (for Angular): yes
-
Exception message and full stack trace: No exception But Angular APIs needs to refresh again to get updated data so we need to put the call in setTimeout for 1-2 seconds.
-
Steps to reproduce the issue: NA
3 Answer(s)
-
0
Hi,
Can you provide any insights or suggestions to improve the performance? is await Task.Run(async ()).. is required? is seperate ouw required inside the handler?
If you need to execute main process and the event itself in a same transaction, then no, you cannot make it faster by executing in a different thread since the roll-back won't work whenever you use
Task.Run
. All the local events are designed to be runned in the same transaction.But if you don't need to execute in the same transaction, you can use
IDistributedEventBus
with RabbitMQ or Kafka. In that case you can use Inbox/Outbox Pattern to ensure all the events are consistently published and consumed. If your application goes bigger, that might be a good scenario for you -
0
Thanks @enisn
One more thing, we are using ILocalEvent to track the entity changes and in the local event handler we are further publishing the IDistributedEventBus. Would this still be causing performance issues.
`MyEntityStatusChangedEventHandler: ILocalEventHandler<EntityCreatedEventData<MyEntity>> { //Handling local event public async Task HandleEventAsync(EntityCreatedEventData<MyEntity> entityCreatedData) { //Publishing Distrubuted event await _distributedEventBus.PublishAsync(new CustomDataEto { //set properties CustomDataEto.Data = MyEntity.Data; } } }`
Also, what if we use new transaction scope using IUnitOfWorkManager.
-
0
You can create different unit of work scopes and do not wait the second one for returning resposne of the http request. That might be the case.
Also if you don't need, you can disable unit of work only for your handler side. But still, handling operation will be executed sync in the same thread, whenever you use
Task.Run
, you detach the the thread from the original one and you don't have to wait it. It logically works as expected but whenever an error occurs in the created task, it'll be harder to track it.By the way, you can manually control unit of work according to your requirements:
https://abp.io/docs/latest/framework/architecture/domain-driven-design/unit-of-work#controlling-the-unit-of-work