using Hncore.Infrastructure.Common;
using Hncore.Pass.Vpn.Service;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;

namespace Hncore.Pass.Vpn.Job
{
    /// <summary>
    /// 超时支付的job
    /// </summary>
    public class RefrushStatusJob
    {

        public static void Start(IServiceProvider serviceProvider)
        {
            Task.Run(async () => { await Execute(serviceProvider); });
        }

        private static async Task Execute(IServiceProvider serviceProvider)
        {           
            while (true) 
            {
                LogHelper.Trace("刷新代理服务器状态");
                try
                {
                    using (var scope = serviceProvider.CreateScope())
                    {
                        var agentService = scope.ServiceProvider.GetService<AgentService>();
                        await agentService.RefrushAllStatus();
                    }
                }
                catch (Exception e)
                {
                    LogHelper.Error("刷新代理服务器状态失败", e);                   
                }
                await Task.Delay(30 * 1000);
            }
        }
    }
}