using Hangfire;
using Hangfire.SqlServer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Runtime.InteropServices;

namespace IRaCIS.Core.API
{
    public static class hangfireSetup
    {
        public static void AddhangfireSetup(this IServiceCollection services, IConfiguration configuration)
        {
            var hangFireConnStr = configuration.GetSection("ConnectionStrings:Hangfire").Value;

            services.AddHangfire(hangFireConfig =>
            {
                //本地window 调试 使用内存，服务器部署使用数据库，防止服务器任务调度到本地
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    hangFireConfig.UseInMemoryStorage();

                }
                else
                {
                    //指定存储介质
                    hangFireConfig.UseSqlServerStorage(hangFireConnStr, new SqlServerStorageOptions()
                    {
                        SchemaName = "dbo",
                    }).UseRecommendedSerializerSettings();
                    // 移除 UseSimpleAssemblyNameTypeSerializer() 以避免类型加载问题
                }


                //hangFireConfig.UseTagsWithSql(); //nuget引入Hangfire.Tags.SqlServer
                //.UseHangfireHttpJob();

            });

            services.AddHangfireServer(option =>
            {
                //默认15s检查一次
                option.SchedulePollingInterval = TimeSpan.FromSeconds(5);
                option.Queues = new[] { "immediately_once", "default", "sys_init", "not_immediately_once" };
            });

        }
    }
}
