using Iceshrimp.Backend.Core.Extensions; namespace Iceshrimp.Backend.Core.Services; public class FollowupTaskService( IServiceScopeFactory serviceScopeFactory, ILogger logger ) : ISingletonService { public AsyncLocal IsBackgroundWorker { get; } = new(); public Task ExecuteTaskAsync(string taskName, Func work) { return Task.Run(async () => { await using var scope = serviceScopeFactory.CreateAsyncScope(); try { var instance = scope.ServiceProvider.GetRequiredService(); instance.IsBackgroundWorker.Value = true; await work(scope.ServiceProvider); } catch (Exception e) { logger.LogError("Failed to execute background task {name}: {error}", taskName, e.ToString()); } }); } }