Skip to content

Commit

Permalink
Removing mult-targetting and upgrading test projects to core 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
aidapsibr committed Jan 20, 2020
1 parent 4e0ac08 commit 3718c0a
Show file tree
Hide file tree
Showing 31 changed files with 551 additions and 174 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Psibernetic Solutions & Donovan "Ovan" Crone
Copyright (c) 2020 Psibernetic Solutions & Aida Crone

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
22 changes: 22 additions & 0 deletions examples/Client-Server/Client/Client.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>7.1</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\REstate.Remote\REstate.Remote.csproj" />
<ProjectReference Include="..\..\..\REstate\REstate.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="SourceLink.Create.CommandLine" Version="2.8.3" />
</ItemGroup>

</Project>
81 changes: 81 additions & 0 deletions examples/Client-Server/Client/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Grpc.Core;
using REstate;
using REstate.Remote;
using Serilog;

namespace Client
{
class Program
{
static async Task Main(string[] args)
{
var logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Console()
.CreateLogger();

Log.Logger = logger;

REstateHost.Agent.Configuration
.RegisterComponent(new GrpcRemoteHostComponent(
new GrpcHostOptions
{
Channel = new Channel("localhost", 12345, ChannelCredentials.Insecure),
UseAsDefaultEngine = true
}));

var stateEngine = REstateHost.Agent
.GetStateEngine<string, string>();

var remoteLoggerSchematic = REstateHost.Agent
.CreateSchematic<string, string>("LoggerMachine")

.WithState("CreatedAndReady", state => state
.AsInitialState())

.WithState("Ready", state => state
.WithTransitionFrom("CreatedAndReady", "log")
.WithReentrance("log")
.WithAction("log info", action => action
.DescribedAs("Logs the payload as a message.")
.WithSetting("messageFormat", "{schematicName}({machineId}) entered {state} on {input}. Message: {payload}")
.OnFailureSend("logFailure")))

.WithState("LogFailure", state => state
.AsSubstateOf("Ready")
.DescribedAs("A message failed to log.")
.WithAction("log error", action => action
.DescribedAs("Logs the failure.")
.WithSetting("messageFormat", "Logging failed, message was: {payload}"))
.WithTransitionFrom("Ready", "logFailure"))

.Build();

// Metadata can be attached to each machine (an instance of a schematic).
// e.g. MachineName, or a correlationId of some kind.
var metadata = new Dictionary<string, string>
{
["MachineName"] = Environment.MachineName
};

try
{
var machine = await stateEngine.CreateMachineAsync(remoteLoggerSchematic, metadata);

var result = await machine.SendAsync("log", "Hello from client!");

}
catch (Exception ex)
{
Log.Logger.Fatal("An error occured that crashed the app.", ex);
throw;
}

Console.WriteLine("Press enter to close...");
Console.ReadLine();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Server.Configuration.REstate
{
public class ServerConfiguration
{
public ServerBinding Binding { get; set; }

public string RepositoryConnectionString { get; set; }
}

public class ServerBinding
{
public string Address { get; set; }

public int Port { get; set; }
}
}
84 changes: 84 additions & 0 deletions examples/Client-Server/Server/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Grpc.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Memory;
using REstate;
using REstate.Engine.Repositories.Redis;
using Serilog;
using Server.Configuration.REstate;

namespace Server
{
class Program
{
static async Task Main(string[] args)
{
var logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Console()
.CreateLogger();

Log.Logger = logger;

var serverShutdownCancellationSource = new CancellationTokenSource();

Console.CancelKeyPress += (sender, eventArgs) =>
serverShutdownCancellationSource.Cancel();

var configuration = new ConfigurationBuilder()
.Add(new MemoryConfigurationSource
{
InitialData = new Dictionary<string, string>
{
["REstate:Server:Binding:Address"] = "0.0.0.0",
["REstate:Server:Binding:Port"] = "12345"
}
})
.AddEnvironmentVariables()
.Build();

var serverConfiguration = configuration
.GetSection("REstate:Server")
.Get<ServerConfiguration>();

//var redisMultiplexer = await StackExchange.Redis.ConnectionMultiplexer
// .ConnectAsync(serverConfiguration.RepositoryConnectionString);

//REstateHost.Agent.Configuration
// .RegisterComponent(
// component: new RedisRepositoryComponent(
// restateDatabase: redisMultiplexer.GetDatabase()));

var server = REstateHost.Agent
.AsRemote()
.CreateGrpcServer(new ServerPort(
host: serverConfiguration.Binding.Address,
port: serverConfiguration.Binding.Port,
credentials: ServerCredentials.Insecure));

logger.Information("Starting REstate gRPC server.");

serverShutdownCancellationSource.Token.Register(() =>
server.ShutdownAsync().GetAwaiter().GetResult());

while (true)
{
await server.StartAsync();

if (server.ShutdownTask.IsFaulted)
{
logger.Error("Server ran into a fault, restarting.");

continue;
}

break;
}

logger.Information("Shutdown of REstate gRPC server completed.");
}
}
}
29 changes: 29 additions & 0 deletions examples/Client-Server/Server/Server.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>7.1</LangVersion>
<ServerGarbageCollection>true</ServerGarbageCollection>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="StackExchange.Redis" Version="1.2.6" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\REstate.Engine.Connectors.AzureServiceBus\REstate.Engine.Connectors.AzureServiceBus.csproj" />
<ProjectReference Include="..\..\..\REstate.Engine.Repositories.Redis\REstate.Engine.Repositories.Redis.csproj" />
<ProjectReference Include="..\..\..\REstate.Remote\REstate.Remote.csproj" />
<ProjectReference Include="..\..\..\REstate\REstate.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="SourceLink.Create.CommandLine" Version="2.8.3" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions examples/NaturalSchematicExamples/NaturalSchematicExamples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\REstate\REstate.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Threading;
using System.Threading.Tasks;
using REstate.Natural;

namespace NaturalSchematicExamples
{
public partial class ProvisioningSystem
{
public class NoReservationsRemain
: NaturalPrecondition<DeprovisionSignal>
{
public override Task<bool> ValidateAsync(
ConnectorContext context,
DeprovisionSignal signal,
CancellationToken cancellationToken = default)
{
return Task.FromResult(true);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace NaturalSchematicExamples
{
public partial class ProvisioningSystem
{
/// <summary>
/// Categorization interface for signals that are
/// accepted in the <see cref="Provisioned"/> state.
/// </summary>
public interface IProvisionedSignal
{
}

public class ReserveSignal : IProvisionedSignal
{

}

public class ReleaseSignal : IProvisionedSignal
{

}

/// <summary>
/// Informs the provisioning system that provisioning has completed.
/// </summary>
public class ProvisioningCompleteSignal : IProvisionedSignal
{
/// <summary>
/// The initial reservation that triggered provisioning
/// </summary>
public ReserveSignal Reservation { get; set; }
}

public class DeprovisionSignal
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Threading;
using System.Threading.Tasks;
using REstate;
using REstate.Natural;

namespace NaturalSchematicExamples
{
public partial class ProvisioningSystem
{
[Description("The resource has not yet been provisioned")]
public class Unprovisioned
: StateDefinition
{
}

public class Provisioning
: StateDefinition<ReserveSignal>
, INaturalAction<ReserveSignal>
{
[Description("Provision necessary resource and forwards the Reservation")]
public async Task InvokeAsync(
ConnectorContext context,
ReserveSignal reserveSignal,
CancellationToken cancellationToken = default)
{
await context.Machine.SignalAsync(
new ProvisioningCompleteSignal
{
Reservation = reserveSignal
},
cancellationToken);
}
}

public class Provisioned
: StateDefinition<IProvisionedSignal>
, INaturalAction<IProvisionedSignal>
{
[Description("Handle Reservation/Release as a counter")]
public Task InvokeAsync(
ConnectorContext context,
IProvisionedSignal provisionedSignal,
CancellationToken cancellationToken = default)
{
return Task.CompletedTask;
}
}

public class Deprovisioning
: StateDefinition<DeprovisionSignal>
, INaturalAction<DeprovisionSignal>
{
public Deprovisioning(IAgent agent)
{
Agent = agent;
}

public IAgent Agent { get; }

[Description("Deprovision all resources allocated")]
public async Task InvokeAsync(
ConnectorContext context,
DeprovisionSignal deprovisionSignal,
CancellationToken cancellationToken = default)
{
await Agent
.GetStateEngine<TypeState, TypeState>()
.DeleteMachineAsync(context.Machine.MachineId, cancellationToken);
}
}
}
}
Loading

0 comments on commit 3718c0a

Please sign in to comment.