From 54452f23a220488d4cfe2cdba27ed78a804406a4 Mon Sep 17 00:00:00 2001 From: JESUS RUIZ HERNANDEZ Date: Thu, 18 Jul 2024 13:34:36 +0200 Subject: [PATCH] Add callbacks to Deploy method and add DeployAsync method to make it awaitable --- .../ZeebeResourceDeployer.cs | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/Zeebe.Client.Accelerator/ZeebeResourceDeployer.cs b/src/Zeebe.Client.Accelerator/ZeebeResourceDeployer.cs index 9fd332a..52d7d6b 100644 --- a/src/Zeebe.Client.Accelerator/ZeebeResourceDeployer.cs +++ b/src/Zeebe.Client.Accelerator/ZeebeResourceDeployer.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading.Tasks; namespace Zeebe.Client.Accelerator { @@ -13,7 +14,7 @@ public class ZeebeResourceDeployer public ZeebeResourceDeployer(IServiceProvider serviceProvider) { - this.ServiceProvider = serviceProvider; + ServiceProvider = serviceProvider; } public ZeebeResourceDeployerWithDirectory UsingDirectory(string directory) @@ -27,10 +28,11 @@ public class ZeebeResourceDeployerWithDirectory : ZeebeResourceDeployer private string Directory; private List Resources = new List(); - public ZeebeResourceDeployerWithDirectory(IServiceProvider serviceProvider, string directory) : base(serviceProvider) + public ZeebeResourceDeployerWithDirectory(IServiceProvider serviceProvider, string directory) + : base(serviceProvider) { - this.Directory = directory; - this.Logger = serviceProvider.GetRequiredService>(); + Directory = directory; + Logger = serviceProvider.GetRequiredService>(); } public ZeebeResourceDeployerWithDirectory AddResource(string resource) @@ -40,23 +42,31 @@ public ZeebeResourceDeployerWithDirectory AddResource(string resource) } public async void Deploy() + => await DeployAsync(); + + public async void Deploy(Action callback) + => await DeployAsync().ContinueWith(t => callback()); + + public async void Deploy(Func asyncCallback) + => await DeployAsync().ContinueWith(async t => await asyncCallback()); + + public async Task DeployAsync() { if (Resources.Count == 0) { // TODO scan directory and add all found bpmn / dmn resources throw new ArgumentException("Illegal call to Deploy() - please add resources first."); } - using (var serviceScope = ServiceProvider.GetRequiredService().CreateScope()) - { - var zeebeClient = serviceScope.ServiceProvider.GetService(); - var cmd = zeebeClient.NewDeployCommand() - .AddResourceFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Directory, Resources.First())); - Resources.RemoveAt(0); - Resources.ForEach(resource => cmd - .AddResourceFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Directory, resource))); - var deployResponse = await cmd.Send(); - deployResponse.Processes.ToList().ForEach(p => Logger.LogInformation("Deployed process {key}", p.BpmnProcessId)); - } + + var serviceScope = ServiceProvider.GetRequiredService().CreateScope(); + var zeebeClient = serviceScope.ServiceProvider.GetService(); + var cmd = zeebeClient.NewDeployCommand() + .AddResourceFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Directory, Resources.First())); + Resources.RemoveAt(0); + Resources.ForEach(resource => cmd + .AddResourceFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Directory, resource))); + var deployResponse = await cmd.Send().ConfigureAwait(false); + deployResponse.Processes.ToList().ForEach(p => Logger.LogInformation("Deployed process {key}", p.BpmnProcessId)); } } }