Skip to content

feat: add support for ScheduleTopic #2

@leynier

Description

@leynier

Currently only EnqueueTopic is supported.

Proposal:

public interface ITopicPublisher
{
    ...

    void ScheduleTopic<T>(string topic, T data, TimeSpan delay);
    void ScheduleTopic<T>(string topic, T data, DateTimeOffset enqueueAt);
}

public class TopicPublisher : ITopicPublisher
{
    ...

    public void ScheduleTopic<T>(string topic, T data, TimeSpan delay)
    {
        BackgroundJob.Schedule(() => DispatchTopic(topic, data), delay);
    }

    public void ScheduleTopic<T>(string topic, T data, DateTimeOffset enqueueAt)
    {
        BackgroundJob.Schedule(() => DispatchTopic(topic, data), enqueueAt);
    }
}

Workaround:

Implement and use a new interface and implementation that inherit from "ITopicPublisher" and "TopicPublisher"

public interface ICustomTopicPublisher : ITopicPublisher
{
    void ScheduleTopic<T>(string topic, T data, TimeSpan delay);
    void ScheduleTopic<T>(string topic, T data, DateTimeOffset enqueueAt);
}

public class CustomTopicPublisher : TopicPublisher, ICustomTopicPublisher
{
    public CustomTopicPublisher(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }

    public void ScheduleTopic<T>(string topic, T data, TimeSpan delay)
    {
        BackgroundJob.Schedule(() => DispatchTopic(topic, data), delay);
    }

    public void ScheduleTopic<T>(string topic, T data, DateTimeOffset enqueueAt)
    {
        BackgroundJob.Schedule(() => DispatchTopic(topic, data), enqueueAt);
    }
}

public static class CustomTopicPublisherExtensions
{
    public static void AddCustomTopicServices(this IServiceCollection services)
    {
        services.AddTopicServices();
        services.AddScoped<ICustomTopicPublisher, CustomTopicPublisher>();
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions