Using Pub/Sub in the Extension

The Publish/Subscribe Implementation

Many operations can happen as the result of an event and don’t need immediate execution. Some of these events can be responded to in the Extension using an Event Handler. There is a small but growing list of these supported events.

Currently, that includes:

  • AssociateStatusEditedHandler
  • UsernameEditedHandler

These Event Handlers are built into the Extension and are run on the “Worker Service”. The Worker Service is an application separate from the one that runs the website. Traditionally, it has been used to run stats and commissions, and process daily run tasks (like AutoShips).

Now, not only do these asynchronous events run without impacting user-involved synchronous calls, but they run on an application server completely detached from serving up the website.

DirectScale recommends the use of asynchronous calling using this method whenever possible.

How Do I Know Which Events are Supported?
Handlers are named with Handler in the class name. For example:

AssociateStatusEditedHandler

Intellisense will help you see the handlers supported by the Disco Extension Abstractions version you’re currently running.

Coding and Testing Event Operations

Events are processed on the Worker Service, so locally testing the event trigger is difficult. You can write your code in a service that can be called and debugged locally.

If you are not familiar with this, we recommend installing and using the DirectScale Emissary NuGet package. The DirectScale team has coded and set up supported events to be fired from the current version of Disco Base. If it’s in the Extension, the message will fire.

If this is in doubt, please reach out to the Customer Care team.

Sample Code

If a client needs to implement a handler, here’s what the extension code would look like:

public class ExtensionEntry : IExtension
{
    public void ConfigureServices(IServiceCollection services)
    {
        // registers a handler, similar to an API
        services.AddSingleton<Handler, MyAssociateStatusEditedHandler>();
    }
}

public class MyAssociateStatusEditedHandler : AssociateStatusEditedHandler
{
    public override HandlerResult Execute(AssociateStatusEditedMessage message)
    {
        Console.WriteLine($"(Extension) Event: {EventName}, AssociateID: {message.AssociateId}, Old Status ID: {message.OldStatusId}, New Status ID: {message.NewStatusId}");

        // additional handler code would go here...

        return new HandlerSuccess();

        // to signify failure and send the message to the DLQ, they would write something like:
        // return new HandlerFailure { Message = "message here" };
    }
}

Note: The Pub/Sub message will be dropped for clients that don’t handle it.

Was this article helpful?
1 out of 1 found this helpful
Previous Next

Comments

0 comments

Please sign in to leave a comment.