Coder Social home page Coder Social logo

baylorrae / hangfire-framework-sample Goto Github PK

View Code? Open in Web Editor NEW
18.0 2.0 8.0 1.35 MB

This is a framework for wrapping Hangfire with an opinionated syntax for queueing jobs. It essentially draws inspiration from .NET MVC middleware configuration with the following goals.

Home Page: https://youtu.be/ElqBtMQgQqI

License: MIT License

C# 59.55% CSS 11.13% JavaScript 0.16% Dockerfile 5.50% HTML 23.66%
hangfire csharp redis asp-net-core

hangfire-framework-sample's Introduction

Hangfire Framework - Sample

This is a framework for wrapping Hangfire with an opinionated syntax for queueing jobs. It essentially draws inspiration from .NET MVC middleware configuration with the following goals.

  1. Encourage scalars when defining job options.
  2. Use "convention over configuration" when queueing jobs.
  3. Create abstractions to make testing easier.

Usage

A job should be defined in a single class which is instantiated by the global IJobRunner. By convention you should create a job options class, the job handler class and an helper extension on the job runner.

While this sample application doesn't feature validation on IJobOptions, it is recommended. Validation should take place on the job runner when a job is scheduled.

/**
 * Options will be serialized when scheduling the job.
 * They should be scalers or other values that serialize
 * easily.
 */
public class LongRunningJobOptions : IJobOptions
{
    public int TaskTime { get; set; } = 2;
}
    
/**
 * The Job class will be instantiated by the Worker process
 * and should have been configured with an IoC to simplify testing.
 */
public class LongRunningJob : IJob<LongRunningJobOptions>
{
    public void Perform(LongRunningJobOptions options)
    {
        Console.WriteLine("LongRunningTask: Started");

        var stopWatch = Stopwatch.StartNew();
        Thread.Sleep(TimeSpan.FromSeconds(options.TaskTime));
        
        Console.WriteLine($"LongRunningTask: Finished ({stopWatch.Elapsed})");
    }
}

/**
 * Adding a helper method on the +IJobRunner+ provides a
 * cleaner API when testing and reading through application
 * code.
 */
public static class LongRunningJobExtension
{
    public static void QueueLongRunningJob(this IJobRunner jobRunner, Action<LongRunningJobOptions> configureJob)
    {
        jobRunner.Queue<LongRunningJob, LongRunningJobOptions>(configureJob);
    }
}

/**
 * With the options class and helper method defined we
 * can queue a job with a simple api.
 */
public void Main(IJobRunner jobRunner)
{
    jobRunner.QueueLongRunningJob(options => { options.TaskTime = 10; });
}

Demo

Sample Project

$ git clone https://github.com/BaylorRae/hangfire-framework-sample.git
$ cd hangfire-framework-sample

# build the frontend and worker projects
$ docker-compose build

# start everything as a daemon
$ docker-compose up -d

# follow the logs for frontend and worker1
$ docker-compose logs -f frontend worker1

Project Structure

This project is designed to keep all 3rd party dependencies in the top most layers (e.g. frontend, worker and common). This is accomplished by wrapping all third party apis with internal interfaces in our core project.

Dependency Graph

hangfire-framework-sample's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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