67 lines
1.8 KiB
C#
67 lines
1.8 KiB
C#
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!");
|
|
}
|
|
}
|
|
} |