Provides a Restore Hooks library to help you register before snapshot and after restore hooks.
$ dotnet add package SnapshotRestore.RegistryThe primary aim of this project is to develop a new API to register and retrieve tasks of type ValueTask.
The class uses a ConcurrentStack and a ConcurrentQueue to store the registered hooks, which are Func<ValueTask> objects.
The RegisterBeforeSnapshot and RegisterAfterRestore methods allow users to register their own hooks, while the InvokeBeforeSnapshotCallbacks and InvokeAfterRestoreCallbacks methods allow the caller to invoke these snapstart hooks.
This implementation is used for Snapstart, a feature that allows for quick restoration of application state.
/// <summary>
/// Example class to demonstrate usage of SnapshotRestore.Registry library
/// </summary>
public class SnapstartExample
{
private Guid _myExecutionEnvironmentGuid;
public SnapstartExample()
{
// This GUID is set for non-restore use-cases such as testing or if SnapStart is turned off
_myExecutionEnvironmentGuid = new Guid();
// Register the method which will run after each restore. You may need to update Amazon.Lambda.Core to see this
Amazon.Lambda.Core.SnapshotRestore.RegisterAfterRestore(MyAfterRestore);
}
private ValueTask MyAfterRestore()
{
// After we restore this snapshot to a new execution environment, update the GUID
_myExecutionEnvironmentGuid = new Guid();
return ValueTask.CompletedTask;
}
public string Handler()
{
return $"Hello World! My Execution Environment GUID is {_myExecutionEnvironmentGuid}";
}
}