Code Monkey home page Code Monkey logo

jobber's Introduction

Jobber


alt tag

Jobber is lightweight, simple and distributed task scheduler.

NuGet version

NuGet Packages

PM> Install-Package Jobber

###Features:

  • Easy to use
  • Pub/Sub mode distributed task scheduling
  • Standalone mode task scheduling
  • Includes high availability modes for producers (ActiveActive-ActivePassive)
  • Includes service recovery modes
  • Logging (currently only support NLog)

###To-Do:

  • Dashboard for service instances
  • Abstraction for logging

Usage:

For standalone job worker mode:

class TodoStandaloneJobWorker : StandaloneJobWorkerBase
{
    protected override void ExecuteJob()
    {
        Console.WriteLine("Hello World!");
    }
}

just inherit the StandaloneJobWorkerBase for your job worker class, and then initialize as follows:

class Program
{
    static void Main(string[] args)
    {
        string jobName = "Todo";
        int restartDelayInMinutes = 1;
        TimeSpan schedulingTickTime = TimeSpan.FromSeconds(5);
        TimeSpan lockDuration = TimeSpan.FromSeconds(10);
        List<EndPoint> redisEndPoints = new List<EndPoint>()
            {
                new DnsEndPoint("", 6379)
            };

        JobberBuilder.Instance.SetJobName(jobName)
                              .EnableServiceRecovery(restartDelayInMinutes)
                              .CreateStandaloneJobWorker()
                                    .SetStandaloneJobWorker<TodoStandaloneJobWorker>()
                                    .SetSchedulingTickTime(schedulingTickTime)
                                    .HighAvailabilitySetup()
                                            .UseActivePassive()
                                            .InitializeRedisForLocking(redisEndPoints)
                                            .SetLockingDuration(lockDuration)
                                            .Then()
                                    .RunAsLocalService();
    }
}

For pub/sub job worker mode, firstly let's initialize job producer.

class TodoJobProducer : JobProducerBase<Todo>
{
    protected override List<IJob<Todo>> GetJobs()
    {
        List<IJob<Todo>> jobs = new List<IJob<Todo>>
            {
                new TodoJob
                {
                    Data = new Todo() {TodoId = 1, TodoNumber = Guid.NewGuid().ToString()}
                },
                new TodoJob
                {
                    Data = new Todo() {TodoId = 2, TodoNumber = Guid.NewGuid().ToString()}
                }
            };

        return jobs;
    }
}

just inherit the JobProducerBase. TJob is your job entity. After this, override the GetJobs() method and return your job data. Now initialize producer as follows:

class Program
{
    static void Main(string[] args)
    {
        string jobName = "Todo";
        string rabbitMqUri = "";
        string rabbitMqUserName = "";
        string rabbitMqPassword = "";
        string rabbitMqTodoQueueName = "todo.queue";
        int restartDelayInMinutes = 1;
        TimeSpan schedulingTickTime = TimeSpan.FromSeconds(5);
        TimeSpan lockDuration = TimeSpan.FromSeconds(10);
        List<EndPoint> redisEndPoints = new List<EndPoint>()
            {
                new DnsEndPoint("", 6379)
            };

        JobberBuilder.Instance.SetJobName(jobName)
                              .EnableServiceRecovery(restartDelayInMinutes)
                              .CreateJobProducer()
                                    .SetRabbitMqCredentials(rabbitMqUri, rabbitMqUserName, rabbitMqPassword)
                                    .SetQueueName(rabbitMqTodoQueueName)
                                    .SetJobProducer<TodoJobProducer>()
                                    .SetSchedulingTickTime(schedulingTickTime)
                                    .HighAvailabilitySetup()
                                            .UseActivePassive()
                                            .InitializeRedisForLocking(redisEndPoints)
                                            .SetLockingDuration(lockDuration)
                                            .Then()
                                    .RunAsLocalService();

    }
}

For job consumer:

class TodoJobConsumer : JobConsumerBase<Todo>
{
    public override async Task ConsumeJob(ConsumeContext<IJob<Todo>> job)
    {
        await Console.Out.WriteLineAsync(job.Message.Data.TodoNumber);
    }
}

then initialize job consumer as follow:

class Program
{
    static void Main(string[] args)
    {
        string jobName = "Todo";
        string rabbitMqUri = "";
        string rabbitMqUserName = "";
        string rabbitMqPassword = "";
        string rabbitMqTodoQueueName = "todo.queue";
        int incrementalRetryLimit = 3;
        TimeSpan initialIncrementalRetryInterval = TimeSpan.FromMinutes(5);
        TimeSpan intervalIncrementalRetryIncrement = TimeSpan.FromMinutes(10);
        int restartDelayInMinutes = 1;

        JobberBuilder.Instance.SetJobName(jobName)
                              .EnableServiceRecovery(restartDelayInMinutes)
                              .CreateJobConsumer()
                                    .SetRabbitMqCredentials(rabbitMqUri, rabbitMqUserName, rabbitMqPassword)
                                    .UseIncrementalRetryPolicy(incrementalRetryLimit, initialIncrementalRetryInterval, intervalIncrementalRetryIncrement)
                                    .SetQueueName(rabbitMqTodoQueueName)
                                    .SetJobConsumer<TodoJobConsumer>()
                                    .RunAsLocalService();
    }
}

###Samples:

jobber's People

Contributors

gokgokalp avatar demirermustafa avatar

Watchers

James Cloos 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.