Skip to content

Commit

Permalink
Merge pull request #36 from JotaDobleEse/deploy-callbacks
Browse files Browse the repository at this point in the history
Add callbacks to Deploy method and add DeployAsync method
  • Loading branch information
VonDerBeck authored Jul 29, 2024
2 parents 3759142 + 54452f2 commit 0edf799
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/Zeebe.Client.Accelerator/ZeebeResourceDeployer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Zeebe.Client.Accelerator
{
Expand All @@ -13,7 +14,7 @@ public class ZeebeResourceDeployer

public ZeebeResourceDeployer(IServiceProvider serviceProvider)
{
this.ServiceProvider = serviceProvider;
ServiceProvider = serviceProvider;
}

public ZeebeResourceDeployerWithDirectory UsingDirectory(string directory)
Expand All @@ -27,10 +28,11 @@ public class ZeebeResourceDeployerWithDirectory : ZeebeResourceDeployer
private string Directory;
private List<string> Resources = new List<string>();

public ZeebeResourceDeployerWithDirectory(IServiceProvider serviceProvider, string directory) : base(serviceProvider)
public ZeebeResourceDeployerWithDirectory(IServiceProvider serviceProvider, string directory)
: base(serviceProvider)
{
this.Directory = directory;
this.Logger = serviceProvider.GetRequiredService<ILogger<ZeebeResourceDeployer>>();
Directory = directory;
Logger = serviceProvider.GetRequiredService<ILogger<ZeebeResourceDeployer>>();
}

public ZeebeResourceDeployerWithDirectory AddResource(string resource)
Expand All @@ -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<Task> 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<IServiceScopeFactory>().CreateScope())
{
var zeebeClient = serviceScope.ServiceProvider.GetService<IZeebeClient>();
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<IServiceScopeFactory>().CreateScope();
var zeebeClient = serviceScope.ServiceProvider.GetService<IZeebeClient>();
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));
}
}
}
Expand Down

0 comments on commit 0edf799

Please sign in to comment.