/Scheduler

A thread-safe asynchronous producer-consumer model.

Primary LanguageC#MIT LicenseMIT

Scheduler

Overview

Scheduler is an asynchronous producer-consumer model that allows producers concurrently add items to the collection and consumes each item from the collection in single thread.

Overview

The flow can be summarized with following steps:

  1. A producer adds an item to the collection by calling AddAndRun or TryAddAndRun method.
  2. Scheduler checks whether the item is the first one in the collection. If so, the main loop thread will be launched.
  3. Action consumes the item from the collection.
  4. Scheduler checks whether the collection is empty. If so, main loop thread ends. If not, repeats step 3.

The Scheduler performs following behaviors to handle main loop thread:

  • If the collection implements INotifyCollectionChanged interface, when CollectionChanged event is fired to notify that new items has been added, the main loop thread will also be launched if it is in idle.
  • Main loop thread can be canceled at any time with Stop method, and resumed with Run method.

Redis Integration

This project defines a set of Redis wrapper that can be used to replace in-memory collection. In this scenario, items are stored in Redis list instead of local memory and wrapped by RedisQueue or RedisStack. This can avoid large local memory use when a number of items are not consumed yet.

Overview Redis

By applying ObservableRedisQueue or ObservableRedisStack, which implements INotifyCollectionChanged interface, it is also possible that multiple application instances share same collection. Once new items have been added via an application instance, all instances sharing same Redis collection will receive the notification and start the main loop thread respectively.

Overview Observable Redis

The Redis wrappers are defined in ModelWorkshop.Scheduling.Redis namespace.

Platforms

This project targets .Net Core and .Net Framework 4.5.