Initial split migration
This commit is contained in:
38
Pushy.Silo/Grains/LinkGenerator.cs
Normal file
38
Pushy.Silo/Grains/LinkGenerator.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using Pushy.Domain;
|
||||
|
||||
namespace Pushy.Silo.Grains;
|
||||
|
||||
public sealed class LinkGenerator : IGrainBase, ILinkGenerator
|
||||
{
|
||||
private readonly ILogger<LinkGenerator> _logger;
|
||||
private readonly IClusterClient _clusterClient;
|
||||
|
||||
public IGrainContext GrainContext { get; }
|
||||
|
||||
public LinkGenerator(
|
||||
IGrainContext grainContext,
|
||||
IClusterClient clusterClient,
|
||||
ILogger<LinkGenerator> logger)
|
||||
{
|
||||
_clusterClient = clusterClient;
|
||||
_logger = logger;
|
||||
GrainContext = grainContext;
|
||||
}
|
||||
|
||||
public async ValueTask<LinkResult> GenerateTextShare(string text)
|
||||
{
|
||||
string item = $"{Guid.CreateVersion7():N}"[9..];
|
||||
var textGrain = _clusterClient.GetGrain<ITextItem>(item);
|
||||
try
|
||||
{
|
||||
await textGrain.SetText(text);
|
||||
return new LinkResult(textGrain);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
//TODO: Write log message of why it fails?
|
||||
//Swallow small change of error
|
||||
return await GenerateTextShare(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
67
Pushy.Silo/Grains/TextItemGrain.cs
Normal file
67
Pushy.Silo/Grains/TextItemGrain.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using Pushy.Domain;
|
||||
|
||||
namespace Pushy.Silo.Grains;
|
||||
|
||||
public sealed class TextItemGrain : IGrainBase, ITextItem, IRemindable
|
||||
{
|
||||
private readonly IPersistentState<TextItem> _state;
|
||||
private readonly ILogger<TextItemGrain> _logger;
|
||||
|
||||
private IGrainReminder? _reminder;
|
||||
|
||||
public TextItemGrain(
|
||||
IGrainContext grainContext,
|
||||
[PersistentState("text")] IPersistentState<TextItem> state,
|
||||
ILogger<TextItemGrain> logger)
|
||||
{
|
||||
_state = state;
|
||||
_logger = logger;
|
||||
GrainContext = grainContext;
|
||||
}
|
||||
|
||||
public IGrainContext GrainContext { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task OnActivateAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_reminder = await this.RegisterOrUpdateReminder("clear", TimeSpan.FromDays(10), TimeSpan.FromDays(10));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask<TextSetResult> SetText(string text)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_state.State.Text is not null)
|
||||
return TextSetResult.Failure;
|
||||
|
||||
_state.State.Text = text;
|
||||
await _state.WriteStateAsync();
|
||||
|
||||
return TextSetResult.Success;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return TextSetResult.Failure;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public ValueTask<string> GetText()
|
||||
{
|
||||
return ValueTask.FromResult(_state.State.Text ?? throw new InvalidOperationException("No text was available"));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task ReceiveReminder(string reminderName, TickStatus status)
|
||||
{
|
||||
if (reminderName == "clear")
|
||||
{
|
||||
_state.State.Text = null;
|
||||
await _state.WriteStateAsync();
|
||||
this.DeactivateOnIdle();
|
||||
|
||||
_logger.LogInformation("Text item have been cleared!");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user