using Microsoft.Extensions.DependencyInjection;
using Quartz.Spi;

namespace Tiobon.Core.Tasks;

public class JobFactory : IJobFactory
{
    /// <summary>
    /// 注入反射获取依赖对象
    /// </summary>
    private readonly IServiceProvider _serviceProvider;
    public JobFactory(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }
    /// <summary>
    /// 实现接口Job
    /// </summary>
    /// <param name="bundle"></param>
    /// <param name="scheduler"></param>
    /// <returns></returns>
    public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
    {
        try
        {
            var serviceScope = _serviceProvider.CreateScope();
            var job = serviceScope.ServiceProvider.GetService(bundle.JobDetail.JobType) as IJob;
            return job;

        }
        catch (Exception)
        {
            throw;
        }
    }

    public void ReturnJob(IJob job)
    {
        var disposable = job as IDisposable;
        if (disposable != null)
        {
            disposable.Dispose();
        }

    }
}
