Giter Site home page Giter Site logo

gofer.net's Introduction

Gofer.NET: Easy distributed tasks/jobs for .NET Core

Build Status Myget Version Number

What is this?

Run background jobs (one-off, or scheduled) on a worker pool easily.

  • Scale your worker pool by simply adding new nodes.
  • Tasks are processed at least once, and not lost if a worker dies.
  • Supports Redis backend, with an extensible API for developing additional backends.

Inspired by celery for python, this is a distributed job runner for .NET Standard 2.0 Applications. Currently only tested using .NET Core 2.0.

Here's a quick example using the same machine to execute jobs:

    public static void Main(string[] args)
    {
        var redisConnectString = "..."

        var taskQueue = TaskQueue.Redis(redisConnectString);
        
        taskQueue.Enqueue(() => RunTask("echo"));
        taskQueue.ExecuteNext();
    }
    
    private static void RunTask(string message)
    {
        Console.WriteLine(message);
    }

Currently requires a Redis backend to function, but other backends may be developed relatively easily.

Getting Started

Get started with background jobs for your web app in 1 minute

Install the dotnet cli

We recommend using the dotnet cli, to get started, but it's not a necessity. Gofer.NET is a netstandard2.0 project, so you will need the 2.0.0 version of the dotnet cli.

$ dotnet --version
2.0.0

Create a Project

Getting started is easy. First create a new project:

dotnet new console

Add our NuGet Package to your project

Add the prerelease feed to your project. Your NuGet.config should look like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="Gofer.NET Prerelease" value="https://www.myget.org/F/thor/api/v3/index.json" />
  </packageSources>
</configuration>

Then add the Gofer.NET package:

dotnet package add Gofer.NET

Put it all together

You can enqueue a function with any arguments that can be serialized. See examples below. Setup your Program.cs to run the task client like so:

public class Program
{
    public static void Main(string[] args)
    {
        var redisConnectString = "..."

        var taskClient = new TaskClient(TaskQueue.Redis(redisConnectString));
        
        var aStr = "astring";
        taskClient.TaskQueue.Enqueue(() => RunTaskNow(aStr));
        
        var myCustom = new CustomClass {Value="Custom!"};
        taskClient.TaskQueue.Enqueue(() => RunTaskNow(myCustom));
        
        // Run a scheduled task at a defined interval
        taskClient.TaskScheduler.AddScheduledTask(() => ScheduledTask(), TimeSpan.FromSeconds(5), "scheduledTaskName");
        
        // Start the task listener, effectively turning this node into a worker.
        // At least one listener is required for scheduled or enqueued jobs to run.
        taskClient.Listen();
    }
    
    private static void RunTaskNow(object value)
    {
        Console.WriteLine(value.ToString());
    }
    
    private static void ScheduledTask()
    {
        Console.WriteLine("Scheduled!");
    }
    
    private class CustomClass
    {
        public string Value { get; set; }
        
        public override string ToString()
        {
            return Value;
        }
    }
}

gofer.net's People

Contributors

brthor 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.