Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postpone deactive for 30 seconds when the reason is applicaiton idle #132

Merged
merged 2 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions ChatRoom/ChatRoom.Common/Orleans/ChannelGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,27 @@ public ChannelGrain(
{
_logger = logger;
_config = config;
this.DelayDeactivation(TimeSpan.MaxValue);
}

public override async Task OnActivateAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Channel {ChannelId} activated", this.GetPrimaryKeyString());

await base.OnActivateAsync(cancellationToken);
}

public override Task OnDeactivateAsync(DeactivationReason reason, CancellationToken cancellationToken)
{
if (reason.ReasonCode == DeactivationReasonCode.ActivationIdle)
{
_logger?.LogInformation("Channel {ChannelId} deactivated due to inactivity", this.GetPrimaryKeyString());
_logger?.LogInformation("Delaying deactivation for 30 seconds");
this.DelayDeactivation(TimeSpan.FromSeconds(30));

return Task.CompletedTask;
}

var roomID = this.GetPrimaryKeyString();
_logger?.LogInformation($"Channel {roomID} deactivated because {reason.Description} and {reason.ReasonCode}");
return base.OnDeactivateAsync(reason, cancellationToken);
}

public async Task Delete()
Expand Down
17 changes: 16 additions & 1 deletion ChatRoom/ChatRoom.Common/Orleans/RoomGrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,22 @@ public RoomGrain(
: base()
{
_logger = logger;
this.DelayDeactivation(TimeSpan.MaxValue);
}

public override Task OnDeactivateAsync(DeactivationReason reason, CancellationToken cancellationToken)
{
if (reason.ReasonCode == DeactivationReasonCode.ActivationIdle)
{
_logger?.LogInformation("Room {RoomName} deactivated due to inactivity", this.GrainKey);
_logger?.LogInformation("Delaying deactivation for 30 seconds");
this.DelayDeactivation(TimeSpan.FromSeconds(30));

return Task.CompletedTask;
}

var roomID = this.GetPrimaryKeyString();
_logger?.LogInformation($"Room {roomID} deactivated because {reason.Description} and {reason.ReasonCode}");
return base.OnDeactivateAsync(reason, cancellationToken);
}

public virtual string GrainKey => this.GetPrimaryKeyString();
Expand Down
Loading