using System;
using Microsoft.Extensions.Logging;
using Sockeye.Util;
using System.Threading.Tasks;

namespace Sockeye.Biz
{
    /// <summary>
    /// called by Generator to kill report generation processor stuck in limbo (chromium at this time)
    /// </summary>
    internal static class CoreJobReportRenderEngineProcessCleanup
    {
        private static ILogger log = Sockeye.Util.ApplicationLogging.CreateLogger("CoreJobReportRenderEngineProcessCleanup");

        private static DateTime _lastRun = DateTime.UtcNow;
        //SET LOW INTENTIONALLY AS CAN EAT UP A LOT OF RESOURCES QUICKLY IF RUN'S PAST TIME
        private static TimeSpan tsRunEvery = new TimeSpan(0, 0, 20);//every twenty seconds run the cleanup task

        ////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        //
        public static async Task DoWork()
        {
            if (DateUtil.IsAfterDuration(_lastRun, tsRunEvery))
            {
                log.LogTrace("Checking for expired report jobs");
                await Util.ReportRenderManager.KillExpiredRenders(log);
                var now = DateTime.UtcNow;
                _lastRun = now;
            }
        }





        /////////////////////////////////////////////////////////////////////

    }//eoc
}//eons

