├── .github └── workflows │ └── ci-cd.yml ├── .gitignore ├── .idea └── .idea.AsyncFlow │ └── .idea │ ├── .gitignore │ ├── encodings.xml │ └── indexLayout.xml ├── AsyncFlow.Queues.Generator ├── AsyncFlow.Queues.Generator.csproj ├── ExecutorGenerator.cs ├── ExecutorTemplate.txt ├── FlowGenerator.cs ├── FlowsRegistrationExt.txt └── Util │ └── ClassDeclarationExt.cs ├── AsyncFlow.Sample.Test ├── AsyncFlow.Sample.Test.csproj ├── ISampleApplicationClient.cs ├── SampleApplicationTest.cs ├── Usings.cs └── xunit.runner.json ├── AsyncFlow.Sample ├── AsyncFlow.Sample.csproj ├── GenerateDataJob.cs ├── GenerateDataRequest.cs ├── GenerateDataResponse.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── appsettings.Development.json └── appsettings.json ├── AsyncFlow.sln ├── AsyncFlow.sln.DotSettings.user ├── AsyncFlow ├── AsyncFlow.csproj ├── Attributes │ └── Flow.cs ├── Configuration │ └── AsyncFlowEndpointConfigurator.cs ├── Core │ ├── Cache │ │ ├── DistributedFlowCache.cs │ │ ├── IAsyncFlowCache.cs │ │ └── MemoryFlowCache.cs │ ├── Executor.cs │ └── IExecutor.cs ├── Extensions │ └── FlowTypeExtensions.cs ├── Helpers │ ├── DelegateProgress.cs │ └── MonitoringApiExtention.cs ├── IFlowEnqueuer.cs ├── Interfaces │ └── IAsyncFlow.cs ├── Responses │ ├── EnqueueResponse.cs │ └── StatusResponse.cs ├── ServiceCollection │ ├── AsyncFlowOptions.cs │ └── AsyncFlowServiceExtensions.cs └── WebApplicationExtensions.cs ├── LICENSE.md └── readme.md /.github/workflows/ci-cd.yml: -------------------------------------------------------------------------------- 1 | name: Build and Publish 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | jobs: 9 | build_and_test: 10 | runs-on: ubuntu-latest 11 | 12 | steps: 13 | - name: Checkout code 14 | uses: actions/checkout@v2 15 | 16 | - name: Setup .NET 17 | uses: actions/setup-dotnet@v1 18 | with: 19 | dotnet-version: '7.0.x' # change to your .NET version if different 20 | 21 | - name: Restore dependencies 22 | run: dotnet restore 23 | 24 | - name: Build 25 | run: dotnet build --configuration Release --no-restore 26 | 27 | - name: Test 28 | run: dotnet test --no-restore --verbosity normal 29 | 30 | - name: Publish to NuGet 31 | run: dotnet nuget push **/*.nupkg -k ${{secrets.NUGET_API_KEY}} -s https://api.nuget.org/v3/index.json 32 | working-directory: AsyncFlow.Queues.Generator/bin/Release 33 | 34 | - name: Publish to NuGet 35 | run: dotnet nuget push **/*.nupkg -k ${{secrets.NUGET_API_KEY}} -s https://api.nuget.org/v3/index.json 36 | working-directory: AsyncFlow/bin/Release 37 | 38 | 39 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # Tye 66 | .tye/ 67 | 68 | # ASP.NET Scaffolding 69 | ScaffoldingReadMe.txt 70 | 71 | # StyleCop 72 | StyleCopReport.xml 73 | 74 | # Files built by Visual Studio 75 | *_i.c 76 | *_p.c 77 | *_h.h 78 | *.ilk 79 | *.meta 80 | *.obj 81 | *.iobj 82 | *.pch 83 | *.pdb 84 | *.ipdb 85 | *.pgc 86 | *.pgd 87 | *.rsp 88 | *.sbr 89 | *.tlb 90 | *.tli 91 | *.tlh 92 | *.tmp 93 | *.tmp_proj 94 | *_wpftmp.csproj 95 | *.log 96 | *.tlog 97 | *.vspscc 98 | *.vssscc 99 | .builds 100 | *.pidb 101 | *.svclog 102 | *.scc 103 | 104 | # Chutzpah Test files 105 | _Chutzpah* 106 | 107 | # Visual C++ cache files 108 | ipch/ 109 | *.aps 110 | *.ncb 111 | *.opendb 112 | *.opensdf 113 | *.sdf 114 | *.cachefile 115 | *.VC.db 116 | *.VC.VC.opendb 117 | 118 | # Visual Studio profiler 119 | *.psess 120 | *.vsp 121 | *.vspx 122 | *.sap 123 | 124 | # Visual Studio Trace Files 125 | *.e2e 126 | 127 | # TFS 2012 Local Workspace 128 | $tf/ 129 | 130 | # Guidance Automation Toolkit 131 | *.gpState 132 | 133 | # ReSharper is a .NET coding add-in 134 | _ReSharper*/ 135 | *.[Rr]e[Ss]harper 136 | *.DotSettings.user 137 | 138 | # TeamCity is a build add-in 139 | _TeamCity* 140 | 141 | # DotCover is a Code Coverage Tool 142 | *.dotCover 143 | 144 | # AxoCover is a Code Coverage Tool 145 | .axoCover/* 146 | !.axoCover/settings.json 147 | 148 | # Coverlet is a free, cross platform Code Coverage Tool 149 | coverage*.json 150 | coverage*.xml 151 | coverage*.info 152 | 153 | # Visual Studio code coverage results 154 | *.coverage 155 | *.coveragexml 156 | 157 | # NCrunch 158 | _NCrunch_* 159 | .*crunch*.local.xml 160 | nCrunchTemp_* 161 | 162 | # MightyMoose 163 | *.mm.* 164 | AutoTest.Net/ 165 | 166 | # Web workbench (sass) 167 | .sass-cache/ 168 | 169 | # Installshield output folder 170 | [Ee]xpress/ 171 | 172 | # DocProject is a documentation generator add-in 173 | DocProject/buildhelp/ 174 | DocProject/Help/*.HxT 175 | DocProject/Help/*.HxC 176 | DocProject/Help/*.hhc 177 | DocProject/Help/*.hhk 178 | DocProject/Help/*.hhp 179 | DocProject/Help/Html2 180 | DocProject/Help/html 181 | 182 | # Click-Once directory 183 | publish/ 184 | 185 | # Publish Web Output 186 | *.[Pp]ublish.xml 187 | *.azurePubxml 188 | # Note: Comment the next line if you want to checkin your web deploy settings, 189 | # but database connection strings (with potential passwords) will be unencrypted 190 | *.pubxml 191 | *.publishproj 192 | 193 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 194 | # checkin your Azure Web App publish settings, but sensitive information contained 195 | # in these scripts will be unencrypted 196 | PublishScripts/ 197 | 198 | # NuGet Packages 199 | *.nupkg 200 | # NuGet Symbol Packages 201 | *.snupkg 202 | # The packages folder can be ignored because of Package Restore 203 | **/[Pp]ackages/* 204 | # except build/, which is used as an MSBuild target. 205 | !**/[Pp]ackages/build/ 206 | # Uncomment if necessary however generally it will be regenerated when needed 207 | #!**/[Pp]ackages/repositories.config 208 | # NuGet v3's project.json files produces more ignorable files 209 | *.nuget.props 210 | *.nuget.targets 211 | 212 | # Microsoft Azure Build Output 213 | csx/ 214 | *.build.csdef 215 | 216 | # Microsoft Azure Emulator 217 | ecf/ 218 | rcf/ 219 | 220 | # Windows Store app package directories and files 221 | AppPackages/ 222 | BundleArtifacts/ 223 | Package.StoreAssociation.xml 224 | _pkginfo.txt 225 | *.appx 226 | *.appxbundle 227 | *.appxupload 228 | 229 | # Visual Studio cache files 230 | # files ending in .cache can be ignored 231 | *.[Cc]ache 232 | # but keep track of directories ending in .cache 233 | !?*.[Cc]ache/ 234 | 235 | # Others 236 | ClientBin/ 237 | ~$* 238 | *~ 239 | *.dbmdl 240 | *.dbproj.schemaview 241 | *.jfm 242 | *.pfx 243 | *.publishsettings 244 | orleans.codegen.cs 245 | 246 | # Including strong name files can present a security risk 247 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 248 | #*.snk 249 | 250 | # Since there are multiple workflows, uncomment next line to ignore bower_components 251 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 252 | #bower_components/ 253 | 254 | # RIA/Silverlight projects 255 | Generated_Code/ 256 | 257 | # Backup & report files from converting an old project file 258 | # to a newer Visual Studio version. Backup files are not needed, 259 | # because we have git ;-) 260 | _UpgradeReport_Files/ 261 | Backup*/ 262 | UpgradeLog*.XML 263 | UpgradeLog*.htm 264 | ServiceFabricBackup/ 265 | *.rptproj.bak 266 | 267 | # SQL Server files 268 | *.mdf 269 | *.ldf 270 | *.ndf 271 | 272 | # Business Intelligence projects 273 | *.rdl.data 274 | *.bim.layout 275 | *.bim_*.settings 276 | *.rptproj.rsuser 277 | *- [Bb]ackup.rdl 278 | *- [Bb]ackup ([0-9]).rdl 279 | *- [Bb]ackup ([0-9][0-9]).rdl 280 | 281 | # Microsoft Fakes 282 | FakesAssemblies/ 283 | 284 | # GhostDoc plugin setting file 285 | *.GhostDoc.xml 286 | 287 | # Node.js Tools for Visual Studio 288 | .ntvs_analysis.dat 289 | node_modules/ 290 | 291 | # Visual Studio 6 build log 292 | *.plg 293 | 294 | # Visual Studio 6 workspace options file 295 | *.opt 296 | 297 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 298 | *.vbw 299 | 300 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 301 | *.vbp 302 | 303 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 304 | *.dsw 305 | *.dsp 306 | 307 | # Visual Studio 6 technical files 308 | *.ncb 309 | *.aps 310 | 311 | # Visual Studio LightSwitch build output 312 | **/*.HTMLClient/GeneratedArtifacts 313 | **/*.DesktopClient/GeneratedArtifacts 314 | **/*.DesktopClient/ModelManifest.xml 315 | **/*.Server/GeneratedArtifacts 316 | **/*.Server/ModelManifest.xml 317 | _Pvt_Extensions 318 | 319 | # Paket dependency manager 320 | .paket/paket.exe 321 | paket-files/ 322 | 323 | # FAKE - F# Make 324 | .fake/ 325 | 326 | # CodeRush personal settings 327 | .cr/personal 328 | 329 | # Python Tools for Visual Studio (PTVS) 330 | __pycache__/ 331 | *.pyc 332 | 333 | # Cake - Uncomment if you are using it 334 | # tools/** 335 | # !tools/packages.config 336 | 337 | # Tabs Studio 338 | *.tss 339 | 340 | # Telerik's JustMock configuration file 341 | *.jmconfig 342 | 343 | # BizTalk build output 344 | *.btp.cs 345 | *.btm.cs 346 | *.odx.cs 347 | *.xsd.cs 348 | 349 | # OpenCover UI analysis results 350 | OpenCover/ 351 | 352 | # Azure Stream Analytics local run output 353 | ASALocalRun/ 354 | 355 | # MSBuild Binary and Structured Log 356 | *.binlog 357 | 358 | # NVidia Nsight GPU debugger configuration file 359 | *.nvuser 360 | 361 | # MFractors (Xamarin productivity tool) working folder 362 | .mfractor/ 363 | 364 | # Local History for Visual Studio 365 | .localhistory/ 366 | 367 | # Visual Studio History (VSHistory) files 368 | .vshistory/ 369 | 370 | # BeatPulse healthcheck temp database 371 | healthchecksdb 372 | 373 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 374 | MigrationBackup/ 375 | 376 | # Ionide (cross platform F# VS Code tools) working folder 377 | .ionide/ 378 | 379 | # Fody - auto-generated XML schema 380 | FodyWeavers.xsd 381 | 382 | # VS Code files for those working on multiple tools 383 | .vscode/* 384 | !.vscode/settings.json 385 | !.vscode/tasks.json 386 | !.vscode/launch.json 387 | !.vscode/extensions.json 388 | *.code-workspace 389 | 390 | # Local History for Visual Studio Code 391 | .history/ 392 | 393 | # Windows Installer files from build outputs 394 | *.cab 395 | *.msi 396 | *.msix 397 | *.msm 398 | *.msp 399 | 400 | # JetBrains Rider 401 | *.sln.iml 402 | 403 | ## 404 | ## Visual studio for Mac 405 | ## 406 | 407 | 408 | # globs 409 | Makefile.in 410 | *.userprefs 411 | *.usertasks 412 | config.make 413 | config.status 414 | aclocal.m4 415 | install-sh 416 | autom4te.cache/ 417 | *.tar.gz 418 | tarballs/ 419 | test-results/ 420 | 421 | # Mac bundle stuff 422 | *.dmg 423 | *.app 424 | 425 | # content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore 426 | # General 427 | .DS_Store 428 | .AppleDouble 429 | .LSOverride 430 | 431 | # Icon must end with two \r 432 | Icon 433 | 434 | 435 | # Thumbnails 436 | ._* 437 | 438 | # Files that might appear in the root of a volume 439 | .DocumentRevisions-V100 440 | .fseventsd 441 | .Spotlight-V100 442 | .TemporaryItems 443 | .Trashes 444 | .VolumeIcon.icns 445 | .com.apple.timemachine.donotpresent 446 | 447 | # Directories potentially created on remote AFP share 448 | .AppleDB 449 | .AppleDesktop 450 | Network Trash Folder 451 | Temporary Items 452 | .apdisk 453 | 454 | # content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore 455 | # Windows thumbnail cache files 456 | Thumbs.db 457 | ehthumbs.db 458 | ehthumbs_vista.db 459 | 460 | # Dump file 461 | *.stackdump 462 | 463 | # Folder config file 464 | [Dd]esktop.ini 465 | 466 | # Recycle Bin used on file shares 467 | $RECYCLE.BIN/ 468 | 469 | # Windows Installer files 470 | *.cab 471 | *.msi 472 | *.msix 473 | *.msm 474 | *.msp 475 | 476 | # Windows shortcuts 477 | *.lnk 478 | -------------------------------------------------------------------------------- /.idea/.idea.AsyncFlow/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Rider ignored files 5 | /.idea.AsyncFlow.iml 6 | /modules.xml 7 | /contentModel.xml 8 | /projectSettingsUpdater.xml 9 | # Editor-based HTTP Client requests 10 | /httpRequests/ 11 | # Datasource local storage ignored files 12 | /dataSources/ 13 | /dataSources.local.xml 14 | -------------------------------------------------------------------------------- /.idea/.idea.AsyncFlow/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /.idea/.idea.AsyncFlow/.idea/indexLayout.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /AsyncFlow.Queues.Generator/AsyncFlow.Queues.Generator.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | enable 6 | enable 7 | latest 8 | true 9 | AsyncFlow.Queues.Generator 10 | 1.2.1 11 | Ahmed Fouad 12 | Ahmed Fouad 13 | AsyncFlow.Queues.Generator 14 | Library for facilitating the implementation of asynchronous flows in web applications. 15 | MIT 16 | https://github.com/TheFo2sh/AsyncFlow 17 | https://github.com/TheFo2sh/AsyncFlow 18 | git 19 | AsyncFlow Asynchronous WebAPI .NET 20 | readme.md 21 | true 22 | AsyncFlow.Queues.Generator 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | all 31 | runtime; build; native; contentfiles; analyzers; buildtransitive 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /AsyncFlow.Queues.Generator/ExecutorGenerator.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Immutable; 2 | using System.Reflection; 3 | using System.Text; 4 | using System.Text.Json; 5 | using System.Xml.XPath; 6 | using AsyncFlow.Queues.Generator.Util; 7 | using Microsoft.CodeAnalysis.CSharp.Syntax; 8 | 9 | namespace AsyncFlow.Queues.Generator; 10 | using FlowData=ImmutableArray<(string? classNamespace, string className, IEnumerable namespaces, string queueName, ImmutableArray args)>; 11 | using System; 12 | using System.Collections.Generic; 13 | using System.Linq; 14 | using Microsoft.CodeAnalysis; 15 | using Microsoft.CodeAnalysis.CSharp; 16 | using Microsoft.CodeAnalysis.Text; 17 | 18 | [Generator] 19 | public class ExecutorGenerator : IIncrementalGenerator 20 | { 21 | public void Initialize(IncrementalGeneratorInitializationContext context) 22 | { 23 | var sourceProvider = context.SyntaxProvider.CreateSyntaxProvider( 24 | static (node, ct) => node.IsKind(SyntaxKind.ClassDeclaration), 25 | static (context, ct) => (ClassDeclarationSyntax)context.Node) 26 | .Where(classDeclaration => classDeclaration.AttributeLists.Any(x => x.Attributes.Any(y => y.Name.ToString() == "Flow"))) 27 | .Collect(); 28 | 29 | 30 | context.RegisterSourceOutput(sourceProvider, GenerateSource); 31 | } 32 | 33 | private void GenerateSource(SourceProductionContext context, ImmutableArray values) 34 | { 35 | 36 | var classData = GetTemplate("ExecutorTemplate"); 37 | 38 | var flowData = values.Select(@class => { 39 | var className = @class.Identifier.Text; 40 | var queueName = @class.GetAttributeValue("Flow","QueueName")??className; 41 | var args=@class.GetBaseInterfaceGenericParameters("IAsyncFlow").ToImmutableArray(); 42 | var namespaces = @class.Ancestors().OfType(); 43 | var classNamespace = @class.GetNamespaceFromClass(); 44 | return (classNamespace,className,namespaces,queueName, args); 45 | }).ToImmutableArray(); 46 | 47 | GenerateExecutorClasses(context, flowData, classData); 48 | GenerateFlowRegistrationClass(context, flowData); 49 | } 50 | 51 | private static void GenerateExecutorClasses(SourceProductionContext context, ImmutableArray<(string? classNamespace, string className, IEnumerable namespaces, string queueName, ImmutableArray args)> flowData, string classData) 52 | { 53 | foreach (var item in flowData) 54 | { 55 | var sourceBuilder = new System.Text.StringBuilder(); 56 | foreach (var @namespace in item.namespaces) 57 | { 58 | sourceBuilder.AppendLine($"using {@namespace.Name};"); 59 | } 60 | 61 | if (item.classNamespace != null) 62 | sourceBuilder.AppendLine($"using {item.classNamespace};"); 63 | 64 | var source = classData 65 | .Replace("TRequest", item.args[0]) 66 | .Replace("TResult", item.args[1]) 67 | .Replace("TQueueName", item.queueName); 68 | sourceBuilder.Append(source); 69 | var sourceText = SourceText.From(sourceBuilder.ToString(), System.Text.Encoding.UTF8); 70 | context.AddSource($"{item.args[0]}_Executor.g.cs", sourceText); 71 | } 72 | } 73 | 74 | private string GetTemplate(string name) 75 | { 76 | using var classDataStream = this.GetType().Assembly 77 | .GetManifestResourceStream($"AsyncFlow.Queues.Generator.{name}.txt"); 78 | using var streamReader = new StreamReader(classDataStream); 79 | var classData = streamReader.ReadToEnd(); 80 | return classData; 81 | } 82 | 83 | public void GenerateFlowRegistrationClass(SourceProductionContext context,FlowData flowData) 84 | { 85 | var classData=GetTemplate("FlowsRegistrationExt"); 86 | var sourceBuilder = new StringBuilder(); 87 | var usingDirectiveSyntaxes = flowData 88 | .SelectMany(item=>item.namespaces) 89 | .Select(itm=>$"using {itm.Name};"); 90 | 91 | var namespaces = flowData.Select(item => $"using {item.classNamespace};"); 92 | sourceBuilder.Append( classData.Replace("{usings}", string.Join("\n", usingDirectiveSyntaxes.Concat(namespaces)))); 93 | foreach (var item in flowData) 94 | { 95 | AddFlowRegistration(sourceBuilder,item.args[0],item.args[1],item.className); 96 | } 97 | 98 | sourceBuilder.AppendLine("}"); 99 | sourceBuilder.AppendLine("}"); 100 | 101 | var sourceText = SourceText.From(sourceBuilder.ToString(), System.Text.Encoding.UTF8); 102 | context.AddSource("ServiceCollectionExt.g.cs", sourceText); 103 | } 104 | public void AddFlowRegistration(StringBuilder builder,string requestType, string resultType, string flowType) 105 | { 106 | builder.AppendLine( 107 | $"services.AddTransient,{flowType}>();"); 108 | builder.AppendLine( 109 | $"services.AddTransient, {requestType}_Executor>();"); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /AsyncFlow.Queues.Generator/ExecutorTemplate.txt: -------------------------------------------------------------------------------- 1 | using AsyncFlow.Core.Cache; 2 | using AsyncFlow.Helpers; 3 | using AsyncFlow.Interfaces; 4 | using AsyncFlow.Responses; 5 | using Hangfire; 6 | using Hangfire.Server; 7 | using AsyncFlow.Core; 8 | 9 | public class TRequest_Executor:IExecutor 10 | { 11 | private readonly IAsyncFlow _flow; 12 | private readonly IAsyncFlowCache _asyncFlowCache; 13 | 14 | public TRequest_Executor(IAsyncFlow flow, IAsyncFlowCache asyncFlowCache) 15 | { 16 | _flow = flow; 17 | _asyncFlowCache = asyncFlowCache; 18 | } 19 | 20 | [JobDisplayName("Processing {0}")] 21 | [Queue("TQueueName")] 22 | public async Task ExecuteAsync(string name,TRequest request, PerformContext? context,CancellationToken cancellationToken=default) 23 | { 24 | var progress=new DelegateProgress(data=>context!.SetJobParameter("Progress",data)); 25 | var result= await _flow.ProcessAsync(request,progress,cancellationToken); 26 | _asyncFlowCache.Set(context!.BackgroundJob.Id, result); 27 | } 28 | } -------------------------------------------------------------------------------- /AsyncFlow.Queues.Generator/FlowGenerator.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Immutable; 2 | using Microsoft.CodeAnalysis.CSharp.Syntax; 3 | 4 | namespace AsyncFlow.Queues.Generator; 5 | 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using Microsoft.CodeAnalysis; 10 | using Microsoft.CodeAnalysis.CSharp; 11 | using Microsoft.CodeAnalysis.Text; 12 | 13 | [Generator] 14 | public class FlowGenerator : IIncrementalGenerator 15 | { 16 | public void Initialize(IncrementalGeneratorInitializationContext context) 17 | { 18 | var sourceProvider = context.SyntaxProvider.CreateSyntaxProvider( 19 | static (node, ct) => node.IsKind(SyntaxKind.ClassDeclaration), 20 | static (context, ct) => (ClassDeclarationSyntax)context.Node) 21 | .Where(classDeclaration => classDeclaration.AttributeLists.Any(x => x.Attributes.Any(y => y.Name.ToString() == "Flow"))) 22 | .Collect(); 23 | 24 | 25 | context.RegisterSourceOutput(sourceProvider, GenerateSource); 26 | } 27 | 28 | private void GenerateSource(SourceProductionContext context, ImmutableArray values) 29 | { 30 | var sourceBuilder = new System.Text.StringBuilder(); 31 | 32 | sourceBuilder.AppendLine("public static class Flows"); 33 | sourceBuilder.AppendLine("{"); 34 | var classNames = values.Select(v => { 35 | var queueName = v.AttributeLists 36 | .SelectMany(attrList => attrList.Attributes) 37 | .Where(attr => attr.Name.ToString() == "Flow") 38 | .Select(attr => attr.ArgumentList?.Arguments.FirstOrDefault(arg => arg.NameEquals is { Name.Identifier.Text: "QueueName" })) 39 | .Where(arg => arg != null) 40 | .Select(arg => arg.Expression.ToString().Trim('"')) 41 | .FirstOrDefault(); 42 | 43 | return string.IsNullOrEmpty(queueName) ? v.Identifier.Text : queueName; 44 | }).Distinct().ToImmutableArray(); 45 | 46 | var combined = string.Join(", ", classNames.Select(name=>"\"" + name + "\"")); 47 | foreach (var className in classNames) 48 | { 49 | sourceBuilder.AppendLine($" public static readonly string {className} = \"{className.ToLower()}\";"); 50 | } 51 | sourceBuilder.AppendLine($" public static string[] All = new[] {{ {combined.ToLower()} , \"default\" }};"); 52 | 53 | sourceBuilder.AppendLine("}"); 54 | 55 | var sourceText = SourceText.From(sourceBuilder.ToString(), System.Text.Encoding.UTF8); 56 | context.AddSource("Flows.g.cs", sourceText); 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /AsyncFlow.Queues.Generator/FlowsRegistrationExt.txt: -------------------------------------------------------------------------------- 1 | using AsyncFlow.Interfaces; 2 | using Microsoft.Extensions.Caching.Distributed; 3 | using Microsoft.Extensions.Caching.Memory; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using AsyncFlow.Core; 6 | using AsyncFlow.ServiceCollection; 7 | {usings} 8 | 9 | public static class ServiceCollectionExt 10 | { 11 | /// 12 | /// Adds and configures services for AsyncFlow. Will automatically register all thee flow types in the assembly. 13 | /// 14 | /// The IServiceCollection to add services to. 15 | /// A delegate to configure the provided . 16 | /// The original IServiceCollection to allow for chaining. 17 | public static void AddFlows(this IServiceCollection services, Action setupAction) 18 | { 19 | var options = new AsyncFlowOptions(); 20 | setupAction(options); 21 | 22 | services.AddSingleton(options.Cache); -------------------------------------------------------------------------------- /AsyncFlow.Queues.Generator/Util/ClassDeclarationExt.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | using Microsoft.CodeAnalysis.CSharp; 3 | using Microsoft.CodeAnalysis.CSharp.Syntax; 4 | 5 | namespace AsyncFlow.Queues.Generator.Util; 6 | 7 | public static class ClassDeclarationExt 8 | { 9 | public static string? GetNamespaceFromClass(this ClassDeclarationSyntax classSyntax) 10 | { 11 | // For traditional namespaces 12 | var traditionalNamespace = classSyntax.Ancestors().OfType().FirstOrDefault(); 13 | if (traditionalNamespace != null) 14 | { 15 | return traditionalNamespace.Name.ToString(); 16 | } 17 | var parentCompilationUnit = classSyntax.SyntaxTree.GetCompilationUnitRoot(); 18 | return parentCompilationUnit 19 | .ChildNodes() 20 | .OfType() 21 | .First() 22 | .Name 23 | .ToString(); 24 | } 25 | public static string? GetAttributeValue(this ClassDeclarationSyntax classDeclarationSyntax, string attributeName, 26 | string propertyName) 27 | { 28 | return classDeclarationSyntax.AttributeLists 29 | .SelectMany(attrList => attrList.Attributes) 30 | .Where(attr => attr.Name.ToString() == attributeName) 31 | .Select(attr => 32 | attr.ArgumentList?.Arguments.FirstOrDefault(arg => 33 | arg.NameEquals?.Name.Identifier.Text == propertyName)) 34 | .Where(arg => arg != null) 35 | .Select(arg => arg.Expression.ToString().Trim('"')) 36 | .FirstOrDefault(); 37 | } 38 | 39 | public static IEnumerable GetBaseInterfaceGenericParameters( 40 | this ClassDeclarationSyntax classDeclarationSyntax, string interfaceName) 41 | { 42 | 43 | if (classDeclarationSyntax.BaseList?.Types.Count > 0) 44 | { 45 | foreach (var baseType in 46 | classDeclarationSyntax.BaseList.Types.Select(baseTypeSyntax => baseTypeSyntax.Type)) 47 | { 48 | if (baseType is not GenericNameSyntax genericName || genericName.Identifier.Text != interfaceName) 49 | continue; 50 | 51 | var typeArguments = genericName.TypeArgumentList.Arguments; 52 | 53 | foreach (var typeArgument in typeArguments) 54 | { 55 | yield return typeArgument.ToString(); 56 | } 57 | 58 | break; 59 | } 60 | } 61 | else 62 | throw new Exception($"class does not implement interface {interfaceName}"); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /AsyncFlow.Sample.Test/AsyncFlow.Sample.Test.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net7.0 5 | enable 6 | enable 7 | false 8 | 9 | 10 | true 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | runtime; build; native; contentfiles; analyzers; buildtransitive 20 | all 21 | 22 | 23 | runtime; build; native; contentfiles; analyzers; buildtransitive 24 | all 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /AsyncFlow.Sample.Test/ISampleApplicationClient.cs: -------------------------------------------------------------------------------- 1 | using AsyncFlow.Responses; 2 | using Refit; 3 | 4 | namespace AsyncFlow.Sample.Test; 5 | 6 | public interface ISampleApplicationClient 7 | { 8 | [Post("/data")] 9 | Task EnqueueJob([Body] GenerateDataRequest request); 10 | 11 | [Get("/data/{jobId}/status")] 12 | Task GetJobStatus(string jobId); 13 | 14 | [Get("/data/{jobId}/result")] 15 | Task GetJobResult(string jobId); 16 | 17 | [Delete("/data/{jobId}")] 18 | Task DeleteJob(string jobId); 19 | } -------------------------------------------------------------------------------- /AsyncFlow.Sample.Test/SampleApplicationTest.cs: -------------------------------------------------------------------------------- 1 | using AsyncFlow.Responses; 2 | using FluentAssertions; 3 | using Microsoft.AspNetCore.Mvc.Testing; 4 | using Microsoft.Extensions.Configuration; 5 | using Refit; 6 | 7 | namespace AsyncFlow.Sample.Test; 8 | 9 | public class SampleApplicationTest : IClassFixture> 10 | { 11 | private readonly WebApplicationFactory _factory; 12 | private readonly ISampleApplicationClient _client; 13 | 14 | public SampleApplicationTest(WebApplicationFactory factory) 15 | { 16 | _factory = factory; 17 | _client=RestService.For(_factory.CreateClient()); 18 | } 19 | 20 | [Fact] 21 | public async Task ShouldProcessJobCorrectly() 22 | { 23 | var enqueueResponse = await _client.EnqueueJob(new GenerateDataRequest(-1)); 24 | var statusResponse = await StatusResponse(enqueueResponse).WaitAsync(TimeSpan.FromMinutes(1)); 25 | 26 | statusResponse.Status.Should().Be("Succeeded"); 27 | var resultResponse = await _client.GetJobResult(enqueueResponse.RequestId); 28 | resultResponse.Data.Should().Be("Ahmed"); 29 | } 30 | 31 | private async Task StatusResponse(EnqueueResponse enqueueResponse) 32 | { 33 | StatusResponse statusResponse; 34 | do 35 | { 36 | statusResponse = await _client.GetJobStatus(enqueueResponse.RequestId); 37 | } 38 | while (statusResponse.Status == "Processing"); 39 | 40 | return statusResponse; 41 | } 42 | } -------------------------------------------------------------------------------- /AsyncFlow.Sample.Test/Usings.cs: -------------------------------------------------------------------------------- 1 | global using Xunit; -------------------------------------------------------------------------------- /AsyncFlow.Sample.Test/xunit.runner.json: -------------------------------------------------------------------------------- 1 | { 2 | "shadowCopy": false 3 | } -------------------------------------------------------------------------------- /AsyncFlow.Sample/AsyncFlow.Sample.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net7.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /AsyncFlow.Sample/GenerateDataJob.cs: -------------------------------------------------------------------------------- 1 | using AsyncFlow.Attributes; 2 | using AsyncFlow.Core; 3 | using AsyncFlow.Interfaces; 4 | using AsyncFlow.Responses; 5 | using Bogus; 6 | 7 | namespace AsyncFlow.Sample; 8 | 9 | [Flow(QueueName = "ahmed")] 10 | public class GenerateDataJob:IAsyncFlow 11 | { 12 | public async Task ProcessAsync(GenerateDataRequest request, IProgress progress, CancellationToken cancellationToken) 13 | { 14 | if (request.Count == -1) 15 | return new GenerateDataResponse("Ahmed"); 16 | 17 | if(request.Count == -2) 18 | throw new ArgumentException("Count cannot be -2"); 19 | var result = ""; 20 | var faker = new Faker(); 21 | for (var i = 0; i < 10; i++) 22 | { 23 | result = faker.Random.Words(request.Count); 24 | await Task.Delay(1000, cancellationToken); 25 | 26 | progress.Report(new("GenerateDataJob",i*10)); 27 | 28 | } 29 | return new GenerateDataResponse(result); 30 | } 31 | } -------------------------------------------------------------------------------- /AsyncFlow.Sample/GenerateDataRequest.cs: -------------------------------------------------------------------------------- 1 | namespace AsyncFlow.Sample; 2 | 3 | public record GenerateDataRequest(int Count); -------------------------------------------------------------------------------- /AsyncFlow.Sample/GenerateDataResponse.cs: -------------------------------------------------------------------------------- 1 | namespace AsyncFlow.Sample; 2 | 3 | public record GenerateDataResponse(string Data); -------------------------------------------------------------------------------- /AsyncFlow.Sample/Program.cs: -------------------------------------------------------------------------------- 1 | using AsyncFlow; 2 | using AsyncFlow.Core; 3 | using AsyncFlow.Interfaces; 4 | using AsyncFlow.Sample; 5 | using AsyncFlow.ServiceCollection; 6 | using Hangfire; 7 | using Hangfire.MemoryStorage; 8 | using Hangfire.Storage.SQLite; 9 | 10 | 11 | var builder = WebApplication.CreateBuilder(args); 12 | builder.Services.AddEndpointsApiExplorer(); 13 | builder.Services.AddSwaggerGen(); 14 | 15 | builder.Services.AddHangfire(config => 16 | { 17 | config.UseMemoryStorage(); 18 | }); 19 | builder.Services.AddHangfireServer(options => 20 | { 21 | options.Queues = Flows.All; 22 | }); 23 | builder.Services.AddFlows(options => options.UseMemoryCache()); 24 | builder.Services.AddMemoryCache(); 25 | var app = builder.Build(); 26 | 27 | if (app.Environment.IsDevelopment()) 28 | { 29 | app.UseSwagger(); 30 | app.UseSwaggerUI(); 31 | } 32 | 33 | app.UseHangfireDashboard(); 34 | GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 0}); 35 | 36 | app.MapGet("/", () => "Hello World!"); 37 | app.MapFlow("data"); 38 | app.Run(); 39 | public partial class Program { } -------------------------------------------------------------------------------- /AsyncFlow.Sample/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:35340", 7 | "sslPort": 44388 8 | } 9 | }, 10 | "profiles": { 11 | "http": { 12 | "commandName": "Project", 13 | "dotnetRunMessages": true, 14 | "launchBrowser": true, 15 | "applicationUrl": "http://localhost:5281", 16 | "environmentVariables": { 17 | "ASPNETCORE_ENVIRONMENT": "Development" 18 | } 19 | }, 20 | "https": { 21 | "commandName": "Project", 22 | "dotnetRunMessages": true, 23 | "launchBrowser": true, 24 | "applicationUrl": "https://localhost:7101;http://localhost:5281", 25 | "environmentVariables": { 26 | "ASPNETCORE_ENVIRONMENT": "Development" 27 | } 28 | }, 29 | "IIS Express": { 30 | "commandName": "IISExpress", 31 | "launchBrowser": true, 32 | "environmentVariables": { 33 | "ASPNETCORE_ENVIRONMENT": "Development" 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /AsyncFlow.Sample/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /AsyncFlow.Sample/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | }, 8 | "AllowedHosts": "*" 9 | } 10 | -------------------------------------------------------------------------------- /AsyncFlow.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncFlow", "AsyncFlow\AsyncFlow.csproj", "{FE345E5B-1B77-44B0-B673-57B0BCC8D0FD}" 4 | EndProject 5 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncFlow.Sample", "AsyncFlow.Sample\AsyncFlow.Sample.csproj", "{5E8F56C9-8645-4015-8D83-44C24BF539EB}" 6 | EndProject 7 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncFlow.Sample.Test", "AsyncFlow.Sample.Test\AsyncFlow.Sample.Test.csproj", "{9F3F0865-5B29-49DD-9C66-189B36824B37}" 8 | EndProject 9 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AsyncFlow.Queues.Generator", "AsyncFlow.Queues.Generator\AsyncFlow.Queues.Generator.csproj", "{D0B1DFEA-9243-493C-B137-3F6E5C630D17}" 10 | EndProject 11 | Global 12 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 13 | Debug|Any CPU = Debug|Any CPU 14 | Release|Any CPU = Release|Any CPU 15 | EndGlobalSection 16 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 17 | {FE345E5B-1B77-44B0-B673-57B0BCC8D0FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 18 | {FE345E5B-1B77-44B0-B673-57B0BCC8D0FD}.Debug|Any CPU.Build.0 = Debug|Any CPU 19 | {FE345E5B-1B77-44B0-B673-57B0BCC8D0FD}.Release|Any CPU.ActiveCfg = Release|Any CPU 20 | {FE345E5B-1B77-44B0-B673-57B0BCC8D0FD}.Release|Any CPU.Build.0 = Release|Any CPU 21 | {5E8F56C9-8645-4015-8D83-44C24BF539EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 22 | {5E8F56C9-8645-4015-8D83-44C24BF539EB}.Debug|Any CPU.Build.0 = Debug|Any CPU 23 | {5E8F56C9-8645-4015-8D83-44C24BF539EB}.Release|Any CPU.ActiveCfg = Release|Any CPU 24 | {5E8F56C9-8645-4015-8D83-44C24BF539EB}.Release|Any CPU.Build.0 = Release|Any CPU 25 | {9F3F0865-5B29-49DD-9C66-189B36824B37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 26 | {9F3F0865-5B29-49DD-9C66-189B36824B37}.Debug|Any CPU.Build.0 = Debug|Any CPU 27 | {9F3F0865-5B29-49DD-9C66-189B36824B37}.Release|Any CPU.ActiveCfg = Release|Any CPU 28 | {9F3F0865-5B29-49DD-9C66-189B36824B37}.Release|Any CPU.Build.0 = Release|Any CPU 29 | {D0B1DFEA-9243-493C-B137-3F6E5C630D17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 30 | {D0B1DFEA-9243-493C-B137-3F6E5C630D17}.Debug|Any CPU.Build.0 = Debug|Any CPU 31 | {D0B1DFEA-9243-493C-B137-3F6E5C630D17}.Release|Any CPU.ActiveCfg = Release|Any CPU 32 | {D0B1DFEA-9243-493C-B137-3F6E5C630D17}.Release|Any CPU.Build.0 = Release|Any CPU 33 | EndGlobalSection 34 | EndGlobal 35 | -------------------------------------------------------------------------------- /AsyncFlow.sln.DotSettings.user: -------------------------------------------------------------------------------- 1 |  2 | <SessionState ContinuousTestingMode="0" IsActive="True" Name="ShouldProcessJobCorrectly" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> 3 | <TestAncestor> 4 | <TestId>xUnit::9F3F0865-5B29-49DD-9C66-189B36824B37::net7.0::AsyncFlow.Sample.Test.SampleApplicationTest.ShouldProcessJobCorrectly</TestId> 5 | </TestAncestor> 6 | </SessionState> -------------------------------------------------------------------------------- /AsyncFlow/AsyncFlow.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net7.0;net6.0 5 | enable 6 | enable 7 | preview 8 | AsyncFlow 9 | 1.2.1 10 | Ahmed Fouad 11 | Ahmed Fouad 12 | AsyncFlow 13 | Library for facilitating the implementation of asynchronous flows in web applications. 14 | MIT 15 | https://github.com/TheFo2sh/AsyncFlow 16 | https://github.com/TheFo2sh/AsyncFlow 17 | git 18 | AsyncFlow Asynchronous WebAPI .NET 19 | readme.md 20 | true 21 | AsyncFlow 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /AsyncFlow/Attributes/Flow.cs: -------------------------------------------------------------------------------- 1 | namespace AsyncFlow.Attributes; 2 | 3 | [AttributeUsage(AttributeTargets.Class)] 4 | public class Flow : Attribute 5 | { 6 | public string QueueName { get; set; } 7 | } -------------------------------------------------------------------------------- /AsyncFlow/Configuration/AsyncFlowEndpointConfigurator.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | 3 | namespace AsyncFlow.Configuration; 4 | 5 | /// 6 | /// Provides an abstract base for configuring the behavior of async flow endpoints. 7 | /// This allows custom configuration for endpoints related to enqueuing, status retrieval, result retrieval, and result deletion. 8 | /// 9 | public abstract class AsyncFlowEndpointConfigurator 10 | { 11 | internal Action? EnqueueConfiguration; 12 | internal Action? StatusConfiguration; 13 | internal Action? ResultConfiguration; 14 | internal Action? DeleteConfiguration; 15 | internal Action? ErrorConfiguration; 16 | 17 | /// 18 | /// Configures the behavior of the enqueue endpoint. 19 | /// 20 | /// The action to configure the enqueue endpoint. 21 | /// The current instance of for further configuration. 22 | public AsyncFlowEndpointConfigurator ForEnqueueEndpoint(Action configuration) 23 | { 24 | EnqueueConfiguration = configuration; 25 | return this; 26 | } 27 | 28 | /// 29 | /// Configures the behavior of the status retrieval endpoint. 30 | /// 31 | /// The action to configure the status retrieval endpoint. 32 | /// The current instance of for further configuration. 33 | public AsyncFlowEndpointConfigurator ForStatusEndpoint(Action configuration) 34 | { 35 | StatusConfiguration = configuration; 36 | return this; 37 | } 38 | 39 | 40 | /// 41 | /// Configures the behavior of the error retrieval endpoint. 42 | /// 43 | /// The action to configure the error retrieval endpoint. 44 | /// The current instance of for further configuration. 45 | public AsyncFlowEndpointConfigurator ForErrorEndpoint(Action configuration) 46 | { 47 | StatusConfiguration = configuration; 48 | return this; 49 | } 50 | 51 | /// 52 | /// Configures the behavior of the result retrieval endpoint. 53 | /// 54 | /// The action to configure the result retrieval endpoint. 55 | /// The current instance of for further configuration. 56 | public AsyncFlowEndpointConfigurator ForResultEndpoint(Action configuration) 57 | { 58 | ResultConfiguration = configuration; 59 | return this; 60 | } 61 | 62 | /// 63 | /// Configures the behavior of the result deletion endpoint. 64 | /// 65 | /// The action to configure the result deletion endpoint. 66 | /// The current instance of for further configuration. 67 | public AsyncFlowEndpointConfigurator ForDeleteEndpoint(Action configuration) 68 | { 69 | DeleteConfiguration = configuration; 70 | return this; 71 | } 72 | } 73 | 74 | -------------------------------------------------------------------------------- /AsyncFlow/Core/Cache/DistributedFlowCache.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json; 2 | using Microsoft.Extensions.Caching.Distributed; 3 | 4 | namespace AsyncFlow.Core.Cache; 5 | 6 | public class DistributedFlowCache : IAsyncFlowCache 7 | { 8 | private readonly IDistributedCache _distributedCache; 9 | 10 | public DistributedFlowCache(IDistributedCache distributedCache) 11 | { 12 | _distributedCache = distributedCache; 13 | } 14 | 15 | public void Set(string key, T value) 16 | { 17 | var jsonData = JsonSerializer.Serialize(value); 18 | _distributedCache.SetString(key, jsonData); 19 | } 20 | 21 | public T? Get(string key) 22 | { 23 | var jsonData = _distributedCache.GetString(key); 24 | return jsonData is null ? default : JsonSerializer.Deserialize(jsonData); 25 | } 26 | 27 | public void Delete(string key) 28 | { 29 | _distributedCache.Remove(key); 30 | } 31 | } -------------------------------------------------------------------------------- /AsyncFlow/Core/Cache/IAsyncFlowCache.cs: -------------------------------------------------------------------------------- 1 | namespace AsyncFlow.Core.Cache; 2 | 3 | public interface IAsyncFlowCache 4 | { 5 | void Set(string key, T value); 6 | T? Get(string key); 7 | void Delete(string key); 8 | } -------------------------------------------------------------------------------- /AsyncFlow/Core/Cache/MemoryFlowCache.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Caching.Memory; 2 | 3 | namespace AsyncFlow.Core.Cache; 4 | 5 | public class MemoryFlowCache : IAsyncFlowCache 6 | { 7 | private readonly IMemoryCache _memoryCache; 8 | 9 | public MemoryFlowCache(IMemoryCache memoryCache) 10 | { 11 | _memoryCache = memoryCache; 12 | } 13 | 14 | public void Set(string key, T value) 15 | { 16 | _memoryCache.Set(key, value); 17 | } 18 | 19 | public T? Get(string key) 20 | { 21 | return _memoryCache.TryGetValue(key, out T value) ? value : default; 22 | } 23 | 24 | public void Delete(string key) 25 | { 26 | _memoryCache.Remove(key); 27 | } 28 | } -------------------------------------------------------------------------------- /AsyncFlow/Core/Executor.cs: -------------------------------------------------------------------------------- 1 | using AsyncFlow.Core.Cache; 2 | using AsyncFlow.Helpers; 3 | using AsyncFlow.Interfaces; 4 | using AsyncFlow.Responses; 5 | using Hangfire; 6 | using Hangfire.Server; 7 | namespace AsyncFlow.Core; 8 | 9 | internal class Executor:IExecutor where TFlow: IAsyncFlow 10 | { 11 | private readonly TFlow _flow; 12 | private readonly IAsyncFlowCache _asyncFlowCache; 13 | 14 | public Executor(TFlow flow, IAsyncFlowCache asyncFlowCache) 15 | { 16 | _flow = flow; 17 | _asyncFlowCache = asyncFlowCache; 18 | } 19 | 20 | [JobDisplayName("Processing {0}")] 21 | [Queue("TRequest")] 22 | public async Task ExecuteAsync(string name,TRequest request, PerformContext? context,CancellationToken cancellationToken=default) 23 | { 24 | var progress=new DelegateProgress(data=>context!.SetJobParameter("Progress",data)); 25 | var result= await _flow.ProcessAsync(request,progress,cancellationToken); 26 | _asyncFlowCache.Set(context!.BackgroundJob.Id, result); 27 | } 28 | } -------------------------------------------------------------------------------- /AsyncFlow/Core/IExecutor.cs: -------------------------------------------------------------------------------- 1 | using Hangfire.Server; 2 | 3 | namespace AsyncFlow.Core; 4 | 5 | public interface IExecutor< TRequest> 6 | { 7 | Task ExecuteAsync(string name, TRequest request, PerformContext? context, 8 | CancellationToken cancellationToken = default); 9 | } -------------------------------------------------------------------------------- /AsyncFlow/Extensions/FlowTypeExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using AsyncFlow.Attributes; 3 | 4 | namespace AsyncFlow.Extensions; 5 | 6 | public static class FlowTypeExtensions 7 | { 8 | public static string GetQueueName(this Type type) 9 | { 10 | var flowAttribute = type.GetCustomAttribute(); 11 | if(flowAttribute == null) 12 | { 13 | throw new ArgumentException($"Type {type.Name} is not a flow"); 14 | } 15 | return !string.IsNullOrEmpty(flowAttribute.QueueName) ? flowAttribute.QueueName : type.Name.ToLower(); 16 | } 17 | } -------------------------------------------------------------------------------- /AsyncFlow/Helpers/DelegateProgress.cs: -------------------------------------------------------------------------------- 1 | namespace AsyncFlow.Helpers; 2 | 3 | public class DelegateProgress:IProgress 4 | { 5 | private readonly Action _action; 6 | 7 | public DelegateProgress(Action action) 8 | { 9 | _action = action; 10 | } 11 | 12 | public void Report(T value) 13 | { 14 | _action(value); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /AsyncFlow/Helpers/MonitoringApiExtention.cs: -------------------------------------------------------------------------------- 1 | using Hangfire.Storage; 2 | using Hangfire.Storage.Monitoring; 3 | 4 | namespace AsyncFlow.Helpers; 5 | 6 | public static class MonitoringApiExtention 7 | { 8 | public static IEnumerable> GetFailedJobs(this IMonitoringApi api, int pullby = 1000) 9 | { 10 | var page = 0; 11 | while (true) 12 | { 13 | var jobs = api.FailedJobs(page, pullby); 14 | if (jobs.Count == 0) 15 | yield break; 16 | foreach (var job in jobs) 17 | yield return job; 18 | page++; 19 | } 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /AsyncFlow/IFlowEnqueuer.cs: -------------------------------------------------------------------------------- 1 | using System.Linq.Expressions; 2 | 3 | namespace AsyncFlow; 4 | 5 | public interface IFlowEnqueuer 6 | { 7 | Expression> Enqueue(string queueName); 8 | } -------------------------------------------------------------------------------- /AsyncFlow/Interfaces/IAsyncFlow.cs: -------------------------------------------------------------------------------- 1 | using AsyncFlow.Responses; 2 | 3 | namespace AsyncFlow.Interfaces; 4 | 5 | 6 | /// 7 | /// Represents an asynchronous flow that processes a request of type and returns a result of type . 8 | /// 9 | /// The type of the input request. 10 | /// The type of the result returned after processing the request. 11 | public interface IAsyncFlow 12 | { 13 | /// 14 | /// Processes the specified request asynchronously, optionally reports progress, and returns the result. 15 | /// 16 | /// The request of type to process. 17 | /// An optional progress reporter that can report updates about the processing progress. 18 | /// An token to observe for cancellation requests. It allows the operation to be cancelled if requested. 19 | /// A task that represents the asynchronous operation, containing the processed result of type . 20 | Task ProcessAsync(TRequest request, IProgress progress , CancellationToken cancellationToken ); 21 | } 22 | -------------------------------------------------------------------------------- /AsyncFlow/Responses/EnqueueResponse.cs: -------------------------------------------------------------------------------- 1 | namespace AsyncFlow.Responses; 2 | 3 | public record EnqueueResponse(string RequestId, DateTime DateTime); -------------------------------------------------------------------------------- /AsyncFlow/Responses/StatusResponse.cs: -------------------------------------------------------------------------------- 1 | namespace AsyncFlow.Responses; 2 | 3 | public record StatusResponse(string RequestId, string Status, DateTime CreatedAt, ProgressData? ProgressData = null); 4 | public record ProgressData(string Progress,double Percentage); 5 | public record ErrorResponse(string RequestId, Error Error); 6 | public record Error(string Type,string Message, string? StackTrace = null); -------------------------------------------------------------------------------- /AsyncFlow/ServiceCollection/AsyncFlowOptions.cs: -------------------------------------------------------------------------------- 1 | using AsyncFlow.Core.Cache; 2 | 3 | namespace AsyncFlow.ServiceCollection; 4 | 5 | public class AsyncFlowOptions 6 | { 7 | public IAsyncFlowCache Cache { get; set; } 8 | } -------------------------------------------------------------------------------- /AsyncFlow/ServiceCollection/AsyncFlowServiceExtensions.cs: -------------------------------------------------------------------------------- 1 | using AsyncFlow.Core; 2 | using AsyncFlow.Core.Cache; 3 | using Microsoft.Extensions.Caching.Distributed; 4 | using Microsoft.Extensions.Caching.Memory; 5 | using Microsoft.Extensions.DependencyInjection; 6 | 7 | namespace AsyncFlow.ServiceCollection 8 | { 9 | /// 10 | /// Provides extension methods to configure AsyncFlow within an IServiceCollection. 11 | /// 12 | public static class AsyncFlowServiceExtensions 13 | { 14 | 15 | /// 16 | /// Configures the provided to use in-memory caching. 17 | /// 18 | /// The options to configure. 19 | /// Optional configurations for the memory cache. If not provided, default configurations will be used. 20 | /// The configured . 21 | public static AsyncFlowOptions UseMemoryCache(this AsyncFlowOptions options, MemoryCacheOptions? memoryCacheOptions = null) 22 | { 23 | memoryCacheOptions ??= new MemoryCacheOptions(); 24 | options.Cache = new MemoryFlowCache(new MemoryCache(memoryCacheOptions)); 25 | return options; 26 | } 27 | 28 | /// 29 | /// Configures the provided to use distributed caching. 30 | /// 31 | /// The options to configure. 32 | /// The instance of IDistributedCache to be used for caching. 33 | /// The configured . 34 | public static AsyncFlowOptions UseDistributedCache(this AsyncFlowOptions options, IDistributedCache cache) 35 | { 36 | options.Cache = new DistributedFlowCache(cache); 37 | return options; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /AsyncFlow/WebApplicationExtensions.cs: -------------------------------------------------------------------------------- 1 | using AsyncFlow.Configuration; 2 | using AsyncFlow.Core; 3 | using AsyncFlow.Core.Cache; 4 | using AsyncFlow.Extensions; 5 | using AsyncFlow.Helpers; 6 | using AsyncFlow.Interfaces; 7 | using AsyncFlow.Responses; 8 | using Hangfire; 9 | using Hangfire.Common; 10 | using Hangfire.States; 11 | using Hangfire.Storage.Monitoring; 12 | using Microsoft.AspNetCore.Builder; 13 | using Microsoft.AspNetCore.Http; 14 | using Microsoft.AspNetCore.Mvc; 15 | using Microsoft.Extensions.Caching.Memory; 16 | using Microsoft.Extensions.DependencyInjection; 17 | 18 | namespace AsyncFlow; 19 | 20 | public static class WebApplicationExtensions 21 | { 22 | /// 23 | /// Maps the asynchronous flow endpoints to the specified web application for processing requests, checking status, and retrieving results. 24 | /// 25 | /// The type of the asynchronous flow that implements . 26 | /// The type of the input request. 27 | /// The type of the response returned after processing the request. 28 | /// The web application to which the flow endpoints will be mapped. 29 | /// The name of the flow used to define the route segment for the flow's endpoints. 30 | /// An instance of which allows users to configure the behavior of the flow endpoints. If not provided, default behaviors are used. 31 | /// The same web application after mapping the flow's endpoints, allowing for further configuration or endpoint mapping. 32 | public static WebApplication MapFlow(this WebApplication app, string flowName, AsyncFlowEndpointConfigurator? configurator=default) 33 | where TFlow : IAsyncFlow 34 | { 35 | var enqueueEndpoint=app.MapPost($"/{flowName}", HandleEnqueue); 36 | configurator?.EnqueueConfiguration?.Invoke(enqueueEndpoint); 37 | 38 | var statusEndpoint=app.MapGet($"/{flowName}/{{jobId}}/status", HandleGetStatus); 39 | configurator?.StatusConfiguration?.Invoke(statusEndpoint); 40 | 41 | var getErrorEndpoint= app.MapGet($"/{flowName}/{{jobId}}/error", HandleGetError); 42 | configurator?.ErrorConfiguration?.Invoke(getErrorEndpoint); 43 | 44 | var getResultEndpoint=app.MapGet($"/{flowName}/{{jobId}}/result", HandleGetResult); 45 | configurator?.ResultConfiguration?.Invoke(getResultEndpoint); 46 | 47 | var deleteEndpoint=app.MapDelete($"/{flowName}/{{jobId}}", HandleDeleteResult); 48 | configurator?.DeleteConfiguration?.Invoke(deleteEndpoint); 49 | 50 | return app; 51 | } 52 | 53 | private static Task HandleDeleteResult(HttpContext context,string jobId) 54 | { 55 | BackgroundJob.Delete(jobId); 56 | var cache = context.RequestServices.GetRequiredService(); 57 | cache.Delete(jobId); 58 | return Task.CompletedTask; 59 | } 60 | 61 | 62 | private static Task HandleGetResult(HttpContext context, string jobId) 63 | { 64 | var cache = context.RequestServices.GetRequiredService(); 65 | var result = cache.Get(jobId); 66 | return Task.FromResult(result == null ? Results.NotFound("Resource not found") : Results.Ok(result)); 67 | } 68 | 69 | private static Task HandleGetStatus(HttpContext context,string jobId) 70 | { 71 | var connection = JobStorage.Current.GetConnection(); 72 | var jobData = connection.GetJobData(jobId); 73 | var stateName = jobData.State; 74 | var statusResponse = new StatusResponse(jobId,stateName,jobData.CreatedAt); 75 | 76 | 77 | var progressData = connection.GetJobParameter(jobId,"Progress"); 78 | if(!string.IsNullOrEmpty(progressData)) 79 | statusResponse=statusResponse with { ProgressData = SerializationHelper.Deserialize(progressData) }; 80 | 81 | return Task.FromResult(statusResponse); 82 | } 83 | private static Task HandleGetError(HttpContext context,string jobId) 84 | { 85 | var connection = JobStorage.Current.GetConnection(); 86 | var jobData = connection.GetJobData(jobId); 87 | var stateName = jobData.State; 88 | switch (stateName) 89 | { 90 | case "Failed": 91 | { 92 | var monitoringApi = JobStorage.Current.GetMonitoringApi(); 93 | var failureDto = monitoringApi.GetFailedJobs().FirstOrDefault(job => job.Key == jobId); 94 | 95 | 96 | var error = new Error(failureDto.Value.ExceptionType,failureDto.Value.ExceptionMessage,failureDto.Value.ExceptionDetails); 97 | return Task.FromResult(Results.Ok(new ErrorResponse(jobId,error))); 98 | } 99 | default: 100 | return Task.FromResult(Results.NoContent()); 101 | } 102 | } 103 | 104 | private static Task HandleEnqueue([FromServices] IExecutor executor,TRequest request)where TFlow : IAsyncFlow 105 | { 106 | var jobId = BackgroundJob.Enqueue(() => executor.ExecuteAsync(typeof(TFlow).Name,request,null,CancellationToken.None)); 107 | return Task.FromResult(new EnqueueResponse(jobId, DateTime.Now)); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Pagar.me Pagamentos S/A 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # AsyncFlow Library 2 | 3 | Integrate asynchronous job flows with ease in your .NET applications. AsyncFlow is designed for web applications that use a pattern where users call an invoke API, receive a job ID, and can then track and retrieve the job's results using separate endpoints. 4 | 5 | ## Features 6 | 7 | - **Asynchronous Job Invocation**: Easily handle long-running jobs by providing an immediate response with a job ID. 8 | - **Status Tracking**: Allow users to track the status of their job. 9 | - **Flexible Result Retrieval**: Once the job is complete, users can retrieve the result. 10 | - **Integration with Hangfire**: Make use of the robust background processing library, Hangfire, to handle job execution. 11 | - **Extensible Cache Options**: Store job results in-memory or use a distributed cache. 12 | 13 | ## Getting Started 14 | 15 | ### Installation 16 | 17 | Use the package manager [NuGet](https://www.nuget.org/packages/AsyncFlow/) to install AsyncFlow packages: 18 | 19 | ```bash 20 | dotnet add package AsyncFlow 21 | dotnet add package AsyncFlow.Queues.Generator 22 | ``` 23 | 24 | ### Configuration 25 | 26 | 1. **Setup Hangfire (or any supported background job processor)**: 27 | 28 | ```csharp 29 | builder.Services.AddHangfire(x => x.UseMemoryStorage()); 30 | builder.Services.AddHangfireServer(options => options.Queues = Flows.All)); 31 | ``` 32 | 33 | 2. **Add AsyncFlow with Desired Cache**: 34 | 35 | For in-memory cache: 36 | 37 | ```csharp 38 | builder.Services.AddAsyncFlow(options => options.UseMemoryCache()); 39 | ``` 40 | 41 | For distributed cache: 42 | 43 | ```csharp 44 | builder.Services.AddAsyncFlow(options => options.UseDistributedCache(yourDistributedCacheInstance)); 45 | ``` 46 | 47 | 3. **Map Endpoints**: 48 | 49 | ```csharp 50 | app.MapFlow("endpointName"); 51 | ``` 52 | Certainly! Let's replace the C# record descriptions with JSON examples for the responses: 53 | 54 | ## The Auto created API Endpoints 55 | 56 | When you integrate the `AsyncFlow` library into your application, the following API endpoints are provided for you: 57 | 58 | 1. **Enqueue Endpoint**: 59 | 60 | - **Path**: `/[flowName]` 61 | - **HTTP Method**: POST 62 | - **Purpose**: To initiate the async flow process. 63 | - **Response**: 64 | ```json 65 | { 66 | "RequestId": "12345-abcd", 67 | "DateTime": "2023-08-14T15:30:45Z" 68 | } 69 | ``` 70 | 71 | 2. **Status Endpoint**: 72 | 73 | - **Path**: `/[flowName]/{jobId}/status` 74 | - **HTTP Method**: GET 75 | - **Purpose**: To check the status of a previously enqueued request. 76 | - **Response**: 77 | ```json 78 | { 79 | "RequestId": "12345-abcd", 80 | "Status": "Processing", 81 | "CreatedAt": "2023-08-14T15:30:45Z", 82 | "progressData": { 83 | "progress": "Generating Data", 84 | "percentage": 90 85 | } 86 | } 87 | ``` 88 | 3. **Result Endpoint**: 89 | 90 | - **Path**: `/[flowName]/{jobId}/result` 91 | - **HTTP Method**: GET 92 | - **Purpose**: To retrieve the result of the job. 93 | - **Response**: JSON that represents the result object. 94 | 95 | 4. **Delete Endpoint**: 96 | 97 | - **Path**: `/[flowName]/{jobId}` 98 | - **HTTP Method**: DELETE 99 | - **Purpose**: To delete the result of the job. 100 | - **Response**: No content (empty response). 101 | 102 | 5. **Error Endpoint**: 103 | 104 | - **Path**: /[flowName]/{jobId}/error 105 | - **HTTP Method**: GET 106 | - **Purpose**: To retrieve error details if a job fails. 107 | - **Response**: 108 | ```json{ 109 | "JobId": "unique-job-id", 110 | "Error": { 111 | "Type": "ExceptionType", 112 | "Message": "Detailed error message", 113 | "StackTrace": "Stack trace details..." 114 | } 115 | } 116 | ``` 117 | --- 118 | 119 | Remember to replace the placeholders like "*Details about the Result endpoint, including a JSON example.*" with the actual details for those endpoints if they provide responses similar to the ones you've described. If not, describe them as needed. 120 | 121 | Also, make sure to guide your users on how to replace the `[flowName]` placeholder appropriately. 122 | ### Custom Endpoint Configuration 123 | 124 | You can customize the behavior of the AsyncFlow endpoints using the `AsyncFlowEndpointConfigurator`: 125 | 126 | ```csharp 127 | var configurator = new YourConfiguratorSubClass(); 128 | app.MapFlow("endpointName", configurator); 129 | ``` 130 | 131 | ## Usage Example 132 | 133 | Define a job: 134 | 135 | ```csharp 136 | public class GenerateDataJob : IAsyncFlow 137 | { 138 | public async Task ProcessAsync(GenerateDataRequest request ,IProgress progress , CancellationToken cancellationToken) 139 | { 140 | // Your logic here 141 | } 142 | } 143 | ``` 144 | 145 | Invoke it: 146 | 147 | ```csharp 148 | app.MapFlow("data"); 149 | ``` 150 | 151 | ## Contributing 152 | 153 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. 154 | 155 | ## License 156 | 157 | This project is licensed under the MIT License. 158 | 159 | 160 | 161 | --------------------------------------------------------------------------------