Skip to content

Commit 5141ab2

Browse files
committed
feat(Correlator): Continued the implementation of the correlator app (WIP)
Signed-off-by: Charles d'Avernas <charles.davernas@neuroglia.io>
1 parent bed3a32 commit 5141ab2

35 files changed

+1393
-244
lines changed

src/api/Synapse.Api.Http/Extensions/IServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static IServiceCollection AddSynapseHttpApi(this IServiceCollection servi
6767
Contact = new()
6868
{
6969
Name = "The Synapse Authors",
70-
Url = new Uri("https://github.com/neuroglia-io/synapse")
70+
Url = new Uri("https://github.com/serverlessworkflow/synapse")
7171
}
7272
});
7373
builder.IncludeXmlComments(typeof(Workflow).Assembly.Location.Replace(".dll", ".xml"));

src/api/Synapse.Api.Server/Program.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,6 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
using Microsoft.AspNetCore.Diagnostics;
15-
using Microsoft.Extensions.Options;
16-
using Neuroglia;
17-
using Neuroglia.Serialization;
18-
using ServerlessWorkflow.Sdk;
19-
using Swashbuckle.AspNetCore.SwaggerUI;
20-
using Synapse;
21-
using Synapse.Api.Application;
22-
using Synapse.Api.Http;
23-
using Synapse.Api.Http.Hubs;
24-
using Synapse.Api.Server.Configuration;
25-
using System.Net.Mime;
26-
2714
var builder = WebApplication.CreateBuilder(args);
2815
builder.Services.Configure<SynapseApiServerOptions>(builder.Configuration);
2916
builder.Services.AddResponseCompression();

src/api/Synapse.Api.Server/Usings.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
global using Microsoft.AspNetCore.Diagnostics;
2+
global using Microsoft.Extensions.Options;
3+
global using Neuroglia;
4+
global using Neuroglia.Serialization;
5+
global using ServerlessWorkflow.Sdk;
6+
global using Swashbuckle.AspNetCore.SwaggerUI;
7+
global using Synapse;
8+
global using Synapse.Api.Application;
9+
global using Synapse.Api.Http;
10+
global using Synapse.Api.Http.Hubs;
11+
global using Synapse.Api.Server.Configuration;
12+
global using System.Net.Mime;

src/cli/Synapse.Cli/Commands/Correlations/CreateCorrelationCommand.cs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14+
using Neuroglia.Data.Infrastructure.ResourceOriented;
15+
1416
namespace Synapse.Cli.Commands.Correlations;
1517

1618
/// <summary>
@@ -30,29 +32,49 @@ internal class CreateCorrelationCommand
3032
public const string CommandDescription = "Creates a new correlation.";
3133

3234
/// <inheritdoc/>
33-
public CreateCorrelationCommand(IServiceProvider serviceProvider, ILoggerFactory loggerFactory, ISynapseApiClient api)
35+
public CreateCorrelationCommand(IServiceProvider serviceProvider, ILoggerFactory loggerFactory, ISynapseApiClient api, IYamlSerializer yamlSerializer)
3436
: base(serviceProvider, loggerFactory, api, CommandName, CommandDescription)
3537
{
36-
this.Add(new Argument<string>("name") { Description = "The name of the correlation to create." });
38+
this.YamlSerializer = yamlSerializer;
39+
this.Add(CommandOptions.File);
3740
this.Handler = CommandHandler.Create<string>(this.HandleAsync);
3841
}
3942

43+
/// <summary>
44+
/// Gets the service used to serialize/deserialize objects to/from YAML
45+
/// </summary>
46+
protected IYamlSerializer YamlSerializer { get; }
47+
4048
/// <summary>
4149
/// Handles the <see cref="CreateCorrelationCommand"/>
4250
/// </summary>
43-
/// <param name="name">The name of the correlation to create</param>
51+
/// <param name="file">The file that defines the correlation to create</param>
4452
/// <returns>A new awaitable <see cref="Task"/></returns>
45-
public async Task HandleAsync(string name)
53+
public async Task HandleAsync(string file)
4654
{
47-
ArgumentException.ThrowIfNullOrWhiteSpace(name);
48-
await this.Api.Correlations.CreateAsync(new()
49-
{
50-
Metadata = new()
55+
ArgumentException.ThrowIfNullOrWhiteSpace(file);
56+
var yaml = await File.ReadAllTextAsync(file);
57+
var correlation = this.YamlSerializer.Deserialize<Correlation>(yaml) ?? throw new NullReferenceException("Failed to read a correlation resource from the specified file.");
58+
correlation = await this.Api.Correlations.CreateAsync(correlation);
59+
Console.WriteLine($"correlation/{correlation.GetName()} created");
60+
}
61+
62+
static class CommandOptions
63+
{
64+
65+
public static Option<string> File
66+
{
67+
get
5168
{
52-
Name = name
69+
var option = new Option<string>("--file")
70+
{
71+
Description = "The file that contains the definition of the correlation to create."
72+
};
73+
option.AddAlias("-f");
74+
return option;
5375
}
54-
});
55-
Console.WriteLine($"correlation/{name} created");
76+
}
77+
5678
}
5779

5880
}

src/cli/Synapse.Cli/Commands/Correlations/DeleteCorrelationCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public DeleteCorrelationCommand(IServiceProvider serviceProvider, ILoggerFactory
3535
{
3636
this.AddAlias("del");
3737
this.Add(new Argument<string>("name") { Description = "The name of the correlation to delete" });
38+
this.Add(CommandOptions.Namespace);
3839
this.Add(CommandOptions.Confirm);
3940
this.Handler = CommandHandler.Create<string, string, bool>(this.HandleAsync);
4041
}

src/cli/Synapse.Cli/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"profiles": {
33
"Synapse.Cli": {
44
"commandName": "Project",
5-
"commandLineArgs": "workflow create --file test.yaml"
5+
"commandLineArgs": "correlation create -f correlation.yaml"
66
}
77
}
88
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright © 2024-Present Neuroglia SRL. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"),
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
namespace Synapse;
15+
16+
/// <summary>
17+
/// Exposes all default statuses of a correlation context
18+
/// </summary>
19+
public static class CorrelationContextStatus
20+
{
21+
22+
/// <summary>
23+
/// Indicates that the context is currently active and in use.
24+
/// </summary>
25+
public const string Active = "active";
26+
/// <summary>
27+
/// Indicates that the context is inactive or paused
28+
/// </summary>
29+
public const string Inactive = "inactive";
30+
/// <summary>
31+
/// Indicates that the correlation process has been successfully completed.
32+
/// </summary>
33+
public const string Completed = "completed";
34+
/// <summary>
35+
/// Indicates that the correlation process has been cancelled.
36+
/// </summary>
37+
public const string Cancelled = "cancelled";
38+
39+
/// <summary>
40+
/// Gets a new <see cref="IEnumerable{T}"/> used to enumerate the default correlation context statuses
41+
/// </summary>
42+
/// <returns>A new <see cref="IEnumerable{T}"/> used to enumerate the default correlation context statuses</returns>
43+
public static IEnumerable<string> AsEnumerable()
44+
{
45+
yield return Active;
46+
yield return Inactive;
47+
yield return Completed;
48+
yield return Cancelled;
49+
}
50+
51+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright © 2024-Present Neuroglia SRL. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"),
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
namespace Synapse;
15+
16+
/// <summary>
17+
/// Exposes all default correlation status phases
18+
/// </summary>
19+
public static class CorrelationStatusPhase
20+
{
21+
22+
/// <summary>
23+
/// Indicates that the correlation is pending processing by a correlator
24+
/// </summary>
25+
public const string Pending = "pending";
26+
/// <summary>
27+
/// Indicates that the correlation has been picked up by a correlator and is actively correlating ingested cloud events
28+
/// </summary>
29+
public const string Active = "active";
30+
/// <summary>
31+
/// Indicates that the correlation is inactive and is not correlating events
32+
/// </summary>
33+
public const string Inactive = "inactive";
34+
/// <summary>
35+
/// Indicates that an ephemeral correlation has been completed
36+
/// </summary>
37+
public const string Completed = "completed";
38+
/// <summary>
39+
/// Indicates that the correlation has been cancelled
40+
/// </summary>
41+
public const string Cancelled = "cancelled";
42+
43+
/// <summary>
44+
/// Gets an <see cref="IEnumerable{T}"/> containing default correlation status phases
45+
/// </summary>
46+
/// <returns>A new <see cref="IEnumerable{T}"/> containing default correlation status phases</returns>
47+
public static IEnumerable<string> AsEnumerable()
48+
{
49+
yield return Pending;
50+
yield return Active;
51+
yield return Inactive;
52+
yield return Completed;
53+
yield return Cancelled;
54+
}
55+
56+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright © 2024-Present Neuroglia SRL. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"),
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
using Synapse.Resources;
15+
16+
namespace Synapse;
17+
18+
/// <summary>
19+
/// Defines extensions for <see cref="CorrelationContext"/>s
20+
/// </summary>
21+
public static class CorrelationContextExtensions
22+
{
23+
24+
/// <summary>
25+
/// Determines whether or not the <see cref="CorrelationContext"/> satisfies the filter(s) defined by specified <see cref="EventConsumptionStrategyDefinition"/>
26+
/// </summary>
27+
/// <param name="context">The <see cref="CorrelationContext"/> to check</param>
28+
/// <param name="eventConsumptionStrategy">The <see cref="EventConsumptionStrategyDefinition"/> that configures the <see cref="EventFilterDefinition"/>s to satisfy</param>
29+
/// <returns>A boolean indicating whether or not the <see cref="CorrelationContext"/> satisfies the filter(s) defined by specified <see cref="EventConsumptionStrategyDefinition"/></returns>
30+
public static bool Satisfies(this CorrelationContext context, EventConsumptionStrategyDefinition eventConsumptionStrategy)
31+
{
32+
ArgumentNullException.ThrowIfNull(context);
33+
ArgumentNullException.ThrowIfNull(eventConsumptionStrategy);
34+
if (eventConsumptionStrategy.All != null && context.Events.Count == eventConsumptionStrategy.All.Count) return true;
35+
else if(eventConsumptionStrategy.Any != null && context.Events.Count > 0) return true;
36+
else if (eventConsumptionStrategy.One != null && context.Events.Count > 0) return true;
37+
return false;
38+
}
39+
40+
}

src/core/Synapse.Core/Extensions/WorkflowDefinitionEnumerableExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ public static class WorkflowDefinitionEnumerableExtensions
3636
/// <returns>The <see cref="WorkflowDefinition"/> with the specified version, if any</returns>
3737
public static WorkflowDefinition? Get(this IEnumerable<WorkflowDefinition> definitions, string version) => definitions.FirstOrDefault(wf => wf.Document.Version == version);
3838

39-
}
39+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright © 2024-Present Neuroglia SRL. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"),
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
namespace Synapse.Resources;
15+
16+
/// <summary>
17+
/// Represents the definition of a correlation outcome used to correlation an existing workflow instance
18+
/// </summary>
19+
[DataContract]
20+
public record CorrelateWorkflowOutcomeDefinition
21+
{
22+
23+
/// <summary>
24+
/// Gets/sets a '{name}.{namespace}' reference to the workflow instance to correlate
25+
/// </summary>
26+
[Required]
27+
[DataMember(Name = "ref", Order = 1), JsonPropertyName("ref"), JsonPropertyOrder(1), YamlMember(Alias = "ref", Order = 1)]
28+
public required virtual string Ref { get; set; }
29+
30+
}

src/core/Synapse.Core/Resources/Correlation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public record Correlation
2929
public static readonly ResourceDefinitionInfo ResourceDefinition = new CorrelationResourceDefinition()!;
3030

3131
/// <inheritdoc/>
32-
public Correlation() : base(ResourceDefinition) { }
32+
public Correlation() : base(ResourceDefinition) { this.Status = new(); }
3333

3434
/// <inheritdoc/>
35-
public Correlation(ResourceMetadata metadata, CorrelationSpec spec) : base(ResourceDefinition, metadata, spec) { }
35+
public Correlation(ResourceMetadata metadata, CorrelationSpec spec) : base(ResourceDefinition, metadata, spec, new()) { }
3636

3737
}

src/core/Synapse.Core/Resources/CorrelationContext.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ public record CorrelationContext
2828
[DataMember(Name = "id", Order = 1), JsonPropertyName("id"), JsonPropertyOrder(1), YamlMember(Alias = "id", Order = 1)]
2929
public required virtual string Id { get; set; }
3030

31+
/// <summary>
32+
/// Gets/sets the context's status
33+
/// </summary>
34+
[DataMember(Name = "status", Order = 2), JsonPropertyName("status"), JsonPropertyOrder(2), YamlMember(Alias = "status", Order = 2)]
35+
public virtual string Status { get; set; } = CorrelationContextStatus.Active;
36+
3137
/// <summary>
3238
/// Gets a key/value mapping of the context's correlation keys
3339
/// </summary>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright © 2024-Present Neuroglia SRL. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"),
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
namespace Synapse.Resources;
15+
16+
/// <summary>
17+
/// Represents the correlation's outcome
18+
/// </summary>
19+
[DataContract]
20+
public record CorrelationOutcomeDefinition
21+
{
22+
23+
/// <summary>
24+
/// Gets/sets an object used to configure the outcome, if any, used to start a new workflow instance,. Is mutually exclusive to <see cref="Correlate"/>
25+
/// </summary>
26+
27+
[DataMember(Name = "start", Order = 1), JsonPropertyName("start"), JsonPropertyOrder(1), YamlMember(Alias = "start", Order = 1)]
28+
public virtual StartWorkflowOutcomeDefinition? Start { get; set; }
29+
30+
/// <summary>
31+
/// Gets/sets an object used to configure the outcome, if any, used to correlate a correlation context to an existing workflow instance. Is mutually exclusive to <see cref="Start"/>
32+
/// </summary>
33+
[DataMember(Name = "correlate", Order = 2), JsonPropertyName("correlate"), JsonPropertyOrder(2), YamlMember(Alias = "correlate", Order = 2)]
34+
public virtual CorrelateWorkflowOutcomeDefinition? Correlate { get; set; }
35+
36+
}

0 commit comments

Comments
 (0)