using Hncore.Infrastructure.Common;
using Hncore.Pass.Vpn.Service;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;
using Hncore.Infrastructure.Extension;
namespace Hncore.Pass.Vpn.Job
{
    /// <summary>
    /// 充值失败尝试job
    /// </summary>
    public class ChargeTryJob
    {
        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.FaildTry();
                    }
                }
                catch (Exception e)
                {
                    LogHelper.Error("处理订单失败", e);
                    break;
                }
                await Task.Delay(10 * 1000);
            }
        }
    }
}