GithubHelp home page GithubHelp logo

bubdm / scheduler-2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mhanafy/scheduler

0.0 0.0 0.0 11 KB

A .Net scheduler that handles parallaization and adaptive sleeping on inactivity, can be used to develop long running workers and is specially suited for pulling scenarios

License: MIT License

C# 100.00%

scheduler-2's Introduction

Scheduler

A .Net scheduler that handles parallaization and adaptive sleeping on inactivity, can be used to develop long running workers and is specially suited for pulling scenarios

Scheduler takes care of two things:

  1. Running a task in parallel, resolving the instance for each thread
  2. Sleeping in a progressive fashion when no processing is needed; i.e. when a worker process finds no items to process

This helps building worker roles that need to pull periodically, the sleeping pattern would minimize the impact of the server allowing for more efficient execution.

The level of concurrency and the sleeping behaviour are configurable, To get started, just create a scheduler and either call Start() or StartAndBlock()

Scheduler is designed to be used via DI, and using the Options pattern

var scheduler = serviceProvider.GetService<IScheduler>();
//Since we're using Start, the code won't block, and scheduler will keep executing as long as it's in scope
scheduler.Start(async () =>
{
    Console.WriteLine($"{DateTime.Now:hh:mm:ss.ff} Settings-based scheduler");
    //Simulate some work
    await Task.Delay(5000);
    return true;
});

We can still create a scheduler without using the service provider, a service provider is still needed to resolve types when calling Start

var options = new OptionsWrapper<SchedulerSettings>(new SchedulerSettings
{
    ImmediateResume = true, //causes the scheduler to immediately call the function again when it returns true
    Concurrency = 1, //runs the function on a single thread
    InitialSleepTime = 100, //when the function returns false, scheduler will wait for 100ms before calling it again; this number is then multiplied until MaxSleepTime is reached
    MaxSleepTime = 450 //time between calls can't exceed this value, in this case if the function keeps returning false wait times would be 100, 200, 400, then 450 instead of 800
});

var blockingScheduler = new Scheduler(options, serviceProvider);
//This is a blocking call, so the program will keep executing and any code beyond this call won't be reached until the program is terminated
blockingScheduler.StartAndBlock<Test>(async (Test t) =>
{
    //here we're using the instance resolved by the scheduler for this thread.
    Console.WriteLine($"{DateTime.Now:hh:mm:ss.ff} Blocking scheduler Test instance: {t.Guid}");
    //wait for a bit
    await Task.Delay(2000);
    //returning false will cause the scheduler to call the function immediately if ImmediateResume is set to true, otherwise calls after waiting for InitialSleepTime
    return true;
});

Have a look at the samples project for more information

scheduler-2's People

Contributors

mhanafy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.