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


namespace Sockeye.Biz
{
    /// <summary>
    /// called by Generator to keep temp folder squeaky clean
    /// </summary>
    internal static class CoreJobTempFolderCleanup
    {
        private static ILogger log = Sockeye.Util.ApplicationLogging.CreateLogger("CoreJobTempFolderCleanup");

        private static DateTime _lastRun = DateTime.UtcNow;


#if (DEBUG)
        private static TimeSpan RUN_EVERY_INTERVAL = new TimeSpan(0, 0, 21);//no more frequently than once every 20 seconds
#else 
        private static TimeSpan RUN_EVERY_INTERVAL = new TimeSpan(0, 5, 2);//no more frequently than once every 5 minutes
#endif

       // private static TimeSpan tsRunEvery = new TimeSpan(0, 5, 2);//every this minutes run the cleanup task

        //erase any files found to be older than 15 minutes (which coincides with max report rendering timeout)
#if (DEBUG)
        private static TimeSpan DELETE_IF_OLDER_THAN = new TimeSpan(0, 2, 1);//2 minutes max
#else 
        private static TimeSpan DELETE_IF_OLDER_THAN = new TimeSpan(0, 15, 1);
#endif


        ////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        //
        public static void DoWork()
        {
            if (DateUtil.IsAfterDuration(_lastRun, RUN_EVERY_INTERVAL))
            {
                log.LogDebug("Temp cleanup now");
                FileUtil.CleanTemporaryFilesFolder(DELETE_IF_OLDER_THAN);
                var now = DateTime.UtcNow;
                _lastRun = now;
            }
        }





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

    }//eoc
}//eons

