Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Created September 26, 2023 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KevinJump/696e15dd720910d844269af1fe3465a9 to your computer and use it in GitHub Desktop.
Save KevinJump/696e15dd720910d844269af1fe3465a9 to your computer and use it in GitHub Desktop.
Umbraco Hangfire + uSync.Complete Create a restore point
using Hangfire;
using Hangfire.Server;
using Umbraco.Cms.Core.Composing;
using uSync.Expansions.Core.Restore.Services;
namespace uSync.Hangfire;
public class DailyRestoreComposer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.Services.AddScoped<RestorePointScheduler>();
RecurringJob.AddOrUpdate<RestorePointScheduler>("RestorePoint", x => x.CreateRestorePoint(null), Cron.Daily(), new RecurringJobOptions
{});
}
}
/// <summary>
/// queue a create restore point request, in uSync.Complete's request queue.
/// </summary>
/// <remarks>
/// this will create the restore point request, which when executed
/// will create a new restore point.
/// </remarks>
internal class RestorePointScheduler
{
private readonly ISyncRestorePointService _restorePointService;
private readonly ILogger<RestorePointScheduler> _logger;
public RestorePointScheduler(
ISyncRestorePointService restorePointService,
ILogger<RestorePointScheduler> logger)
{
_restorePointService = restorePointService;
_logger = logger;
}
public void CreateRestorePoint(PerformContext context)
{
var attempt = _restorePointService.QueueCreate(
id: Guid.NewGuid(),
name: $"Daily restore point {DateTime.Now:yyyyMMdd}",
source: "Hangfire Job",
includeMedia: true,
user: "a background user");
if (attempt.Success)
{
_logger.LogInformation("Restore point tiggered");
}
else
{
_logger.LogError(attempt.Exception, "Failed to queue restore");
}
}
}
@KevinJump
Copy link
Author

requires uSync.Complete v12.2rc or higher.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment