Skip to content

Commit

Permalink
Use Reference field as idempotency key
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergii Kram committed Jun 18, 2023
1 parent 12be887 commit 2c90865
Show file tree
Hide file tree
Showing 21 changed files with 771 additions and 169 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ idempotent-workflow-start ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ idempotent-workflow-start ]
schedule:
- cron: '44 16 * * 1'
workflow_dispatch:

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'csharp' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
# Learn more:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed

steps:
- name: Checkout repository
uses: actions/checkout@v2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
127 changes: 10 additions & 117 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ name: .NET

on:
push:
branches: [ master ]
branches: [ idempotent-workflow-start ]
pull_request:
branches: [ master ]
branches: [ idempotent-workflow-start ]
workflow_dispatch:

jobs:
Unit-Tests:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Setup .NET
Expand All @@ -21,117 +24,7 @@ jobs:
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Unit Tests
run: dotnet test test/WorkflowCore.UnitTests --no-build --verbosity normal -p:ParallelizeTestCollections=false
Integration-Tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Integration Tests
run: dotnet test test/WorkflowCore.IntegrationTests --no-build --verbosity normal -p:ParallelizeTestCollections=false
MongoDB-Tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: MongoDB Tests
run: dotnet test test/WorkflowCore.Tests.MongoDB --no-build --verbosity normal -p:ParallelizeTestCollections=false
MySQL-Tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: MySQL Tests
run: dotnet test test/WorkflowCore.Tests.MySQL --no-build --verbosity normal -p:ParallelizeTestCollections=false
PostgreSQL-Tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: PostgreSQL Tests
run: dotnet test test/WorkflowCore.Tests.PostgreSQL --no-build --verbosity normal -p:ParallelizeTestCollections=false
Redis-Tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Redis Tests
run: dotnet test test/WorkflowCore.Tests.Redis --no-build --verbosity normal -p:ParallelizeTestCollections=false
SQLServer-Tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: SQL Server Tests
run: dotnet test test/WorkflowCore.Tests.SqlServer --no-build --verbosity normal -p:ParallelizeTestCollections=false
Elasticsearch-Tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Elasticsearch Tests
run: dotnet test test/WorkflowCore.Tests.Elasticsearch --no-build --verbosity normal -p:ParallelizeTestCollections=false
- name: Integration Tests
run: dotnet test test/WorkflowCore.IntegrationTests --no-build --verbosity normal -p:ParallelizeTestCollections=false
- name: PostgreSQL Tests
run: dotnet test test/WorkflowCore.Tests.PostgreSQL --no-build --verbosity normal -p:ParallelizeTestCollections=false
34 changes: 34 additions & 0 deletions .github/workflows/nuget.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Publish Nuget

on:
workflow_dispatch:

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
6.0.x
- name: Setup NuGet
uses: NuGet/setup-nuget@v1.0.5
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Integration Tests
run: dotnet test test/WorkflowCore.IntegrationTests --configuration Release --no-build --verbosity normal
- name: PostgreSQL Tests
run: dotnet test test/WorkflowCore.Tests.PostgreSQL --configuration Release --no-build --verbosity normal
- name: Pack
run: dotnet pack src/providers/WorkflowCore.Persistence.EntityFramework --configuration Release --no-build
- name: Pack
run: dotnet pack src/providers/WorkflowCore.Persistence.PostgreSQL --configuration Release --no-build
- name: Publish
run: nuget push **\*.nupkg -Source 'https://api.nuget.org/v3/index.json' -ApiKey ${{secrets.NUGET_API_KEY}}
1 change: 1 addition & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MIT License

Copyright (c) 2016 Daniel Gerlag
Copyright (c) 2021 Sergii Kram

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
46 changes: 44 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@
# Workflow Core
# Idempotent Workflow Core

[![Build and Tests](https://github.com/SergiiKram/workflow-core/actions/workflows/dotnet.yml/badge.svg)](https://github.com/SergiiKram/workflow-core/actions/workflows/dotnet.yml)
[![CodeQL](https://github.com/SergiiKram/workflow-core/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/SergiiKram/workflow-core/actions/workflows/codeql-analysis.yml)
[![Publish Nuget](https://github.com/SergiiKram/workflow-core/actions/workflows/nuget.yml/badge.svg)](https://github.com/SergiiKram/workflow-core/actions/workflows/nuget.yml)

This is a fork of the https://github.com/danielgerlag/workflow-core with idempotent workflow instance creation.

The `Reference` field is used as an idempotency key.

So now you can safely retry workflow creation and have an exactly-once guarantee.
Also, in scenarios where you create a workflow to process document, user, external event, etc.,
you can derive 'reference' from these entities and later find the matching workflow if you failed to persist the returned workflow id the first time.

**Example:**
```xml
<PackageReference Include="SergiiKram.WorkflowCore.Persistence.PostgreSQL.Idempotent" Version="3.9.0" />
```

```cs
/// <summary>
/// Trigger <c>HelloWorldWorkflow</c> manually.
/// </summary>
/// <returns></returns>
[HttpPost("hello-world")]
public async Task<IActionResult> HelloWorld(string reference)
{
try
{
var id = await _workflowController.StartWorkflow(HelloWorldWorkflow.WorkflowId, reference: reference);

[![Build status](https://ci.appveyor.com/api/projects/status/xnby6p5v4ur04u76?svg=true)](https://ci.appveyor.com/project/danielgerlag/workflow-core)
return Ok(new { id = id });
}
catch (WorkflowExistsException)
{
var workflowInstance = await _workflowStore.GetWorkflowInstanceByReference(reference);

return Ok(new { id = workflowInstance.Id });
}
}
```

Only PostgreSQL is supported for now.

# Workflow Core

Workflow Core is a light weight embeddable workflow engine targeting .NET Standard. Think: long running processes with multiple tasks that need to track state. It supports pluggable persistence and concurrency providers to allow for multi-node clusters.

Expand Down
8 changes: 4 additions & 4 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project>
<PropertyGroup>
<PackageProjectUrl>https://github.com/danielgerlag/workflow-core</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/danielgerlag/workflow-core/blob/master/LICENSE.md</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/SergiiKram/workflow-core</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/SergiiKram/workflow-core/blob/idempotent-workflow-start/LICENSE.md</PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/danielgerlag/workflow-core.git</RepositoryUrl>
<RepositoryUrl>https://github.com/SergiiKram/workflow-core</RepositoryUrl>
<Version>3.9.0</Version>
<AssemblyVersion>3.9.0.0</AssemblyVersion>
<FileVersion>3.9.0.0</FileVersion>
<PackageIconUrl>https://github.com/danielgerlag/workflow-core/raw/master/src/logo.png</PackageIconUrl>
<PackageIconUrl>https://github.com/SergiiKram/workflow-core/raw/idempotent-workflow-start/src/logo.png</PackageIconUrl>
<PackageVersion>3.9.0</PackageVersion>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Runtime.Serialization;

namespace WorkflowCore.Persistence.EntityFramework.Exceptions
{
public class WorkflowExistsException : Exception
{
public WorkflowExistsException()
: base("Workflow with provided correlation id already exists.")
{
}

protected WorkflowExistsException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}

public WorkflowExistsException(Exception innerException)
: base("Workflow with provided correlation id already exists.", innerException)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Threading;
using System.Threading.Tasks;
using WorkflowCore.Interface;
using WorkflowCore.Models;

namespace WorkflowCore.Persistence.EntityFramework.Interfaces
{
public interface IExtendedPersistenceProvider: IPersistenceProvider
{
Task<WorkflowInstance> GetWorkflowInstanceByReference(string reference, CancellationToken cancellationToken = default);
}
}
Loading

0 comments on commit 2c90865

Please sign in to comment.