Initial implementation of procfile
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
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