GithubHelp home page GithubHelp logo

mikasjp / durable-patterns Goto Github PK

View Code? Open in Web Editor NEW

This project forked from appstream-studio/durable-patterns

0.0 0.0 0.0 102 KB

Library simplifying usage of most common durable functions patterns

License: Apache License 2.0

C# 100.00%

durable-patterns's Introduction

AppStream Studio

License NuGet Package Build Status

Durable Patterns

Welcome to the Durable Patterns Library!

Our library is here to help you make better use of Azure Durable Functions Framework without unnecessary boilerplate code. It provides fluent interface for orchestrator function to easily build efficient and scalable processes that implements the typical application patterns like:

  • fan out / fan in
  • function chaining
  • Monitoring pattern

You just inject business logic enclosed in IPatternActivity implementations and our library will take care of creating activity functions, passing the arguments and injecting objects without the need to write repeatable sections.

Get Started with Durable Patterns

Durable Patterns can be installed using the Nuget package manager or the dotnet CLI.

dotnet add package AppStream.DurablePatterns

To use this library you need to instruct durable functions framework to look for activity functions not only in your startup project by adding this property in of your startup project's csproj:

<FunctionsInDependencies>true</FunctionsInDependencies>

Example - fan out / fan in

Program.cs

using AppStream.DurablePatterns;

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(services => services.AddDurablePatterns(cfg => cfg.AddActivitiesFromAssembly(typeof(GetItems).Assembly))
    .Build();

host.Run();

IPatternActivity classes

using AppStream.DurablePatterns;

internal class GetItems : IPatternActivity<List<Item>>
{
    private readonly IRepository _repository;

    public GetItems(IRepository repository)
    {
        _repository = repository;
    }

    public async Task<PatternActivityResult<List<Item>>> RunAsync(object? input)
    {
        return new PatternActivityResult<List<Item>>(
            value: await _repository.GetItemsAsync(),
            output: new { whateverYouWantToBeDisplayedInOrchestratorOutput = true });
    }
}

internal class FanOut : IPatternActivity<List<Item>, List<Item>>
{
    private readonly IItemProcessingService _service;

    public FanOut(IItemProcessingService service)
    {
        _service = service;
    }

    public Task<PatternActivityResult<List<Item>>> RunAsync(List<Item> input)
    {
        // this block of code will be executed in parallel batches
        var processedItems = new List<Item>();

        foreach (var item in input)
        {
            processedItems.Add(_service.Process(item));
        }

        return Task.FromResult(new PatternActivityResult<List<Item>>(processedItems, new { foo = "bar" }));
    }
}

internal class FanIn : IPatternActivity<List<Item>, List<Item>>
{
    private readonly IOtherItemProcessingService _service;

    public FanIn(IOtherItemProcessingService service)
    {
        _service = service;
    }

    public Task<PatternActivityResult<List<Item>>> RunAsync(List<Item> input)
    {
        // this block of code will be executed once and the input will be all items returned from all FanOut activities
        var processedItems = new List<Item>();

        foreach (var item in input)
        {
            processedItems.Add(_service.Process(item));
        }

        return Task.FromResult(new PatternActivityResult<List<Item>>(processedItems, new { foo = "bar" }));
    }
}

Orchestrator function

using AppStream.DurablePatterns;

internal class MyOrchestrator
{
    private readonly IDurablePatterns _patterns;

    public MyOrchestrator(
        IDurablePatterns patterns)
    {
        _patterns = patterns;
    }

    [Function("Orchestrator")]
    public Task<ExecutionResult> RunOrchestrator(
        [OrchestrationTrigger] TaskOrchestrationContext context)
    {
        return _patterns
            .WithContext(context)
            .RunActivity<GetItems>()
            .FanOutFanIn<FanOut>(new FanOutFanInOptions(BatchSize: 2, ParallelActivityFunctionsCap: 2))
            .RunActivity<FanIn>()
            .ExecuteAsync();
    }
}

Roadmap

  • Updating orchestration status during execution
  • Support for lambdas instead of explicit IPatternActivity implementations
  • Naming the activities for easier debugging
  • Add support for Monitoring pattern
  • Add support for Human interaction pattern

Contributing

Contributions to this open source library are highly appreciated! If you're interested in helping out, please feel free to submit a pull request with your changes. We welcome contributions of all kinds, whether it's bug fixes, new features, or just improving the documentation. Please ensure that your code is well-documented, tested, and adheres to the coding conventions used in the project. Don't hesitate to reach out if you have any questions or need help getting started. You can open an issue on GitHub or email us at [email protected] - we're happy to assist you in any way we can.

durable-patterns's People

Contributors

piotrek-appstream avatar lukasz-appstream avatar bartek-appstream 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.