Skip to content

grendlerpl/BackgroundQ

Repository files navigation

BackgroundQ

Simplifies background processing of data inside an application. Library code is based on IHostedService documentation

  1. Define message class that will be added to the queue and subsequently processed

    public class Message
    {
        public int MessageId { get; set; }
        public string MessageText { get; set; }
    }
  2. Implement message processsor that will be called when a new message is added to the queue.

    //This class will be registered as singleton.
    //Messages are processed in order they are added.
    //Messages are processed one by one.
    
    public class Processor : IBackgroundQProcessor<Message>
    {
        public Processor()
        {
            //add processsor initialization here.
            //you can use DI for required services
        }
        public async Task ProcessQElementAsync(Message message)
        {
            //add processing code here            
        }
    }
  3. Register all classes in Startup

    public void ConfigureServices(IServiceCollection services)
    {
        //This extension method registers Processor as singleton,  IBackgroundQ<Message> as singleton and a HostedService released by the queue when message is added.
        services.AddQService<Message, Processor>();
    }
  4. Inject queue into any of your own services and add an element to it. It will processed automatically in the background.

    public class SomeService
    {
        private readonly IBackgroundQ<Message> _queue;
    
        public SomeService(IBackgroundQ<Message> queue)
        {
            _queue = queue;
        }
    
        public void ServiceMethod()
        {
            //do some work
            //and leave message for background processing
    
            _queue.AddElement(new Message()
                                    {
                                        MessageId = 1,
                                        MessageText = "This is text for message 1"
                                    }
                              );
        }
    }

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages