HangFire.TopicExtensions icon indicating copy to clipboard operation
HangFire.TopicExtensions copied to clipboard

feat: add support for `ScheduleTopic`

Open leynier opened this issue 3 years ago • 2 comments

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>();
    }
}

leynier avatar Jul 30 '22 04:07 leynier

@jmarbutt I can make the pull request if you have in mind to release a new version, thanks for the package, it was useful for me.

leynier avatar Jul 30 '22 04:07 leynier

Sorry for the delay but if you want to make a PR, I will approve it and publish it.

jmarbutt avatar Jan 21 '23 21:01 jmarbutt