jamesmh / coravel

Near-zero config .NET library that makes advanced application features like Task Scheduling, Caching, Queuing, Event Broadcasting, and more a breeze!
https://docs.coravel.net/Installation/
MIT License
3.91k stars 257 forks source link

Run() isn't invoking method #333

Closed VitorSancho closed 1 year ago

VitorSancho commented 1 year ago

Describe the bug I'm trying to test my project, but run() isen't calling method Invoke(). When program pass by Run() method I receive the message in console: "thread 0xb84 exited with code 0.".

In fact, i've noticed an odd behaviour. When I comment my code and add command Console.WriteLine("Something..") method run() works well: My program.cs:

    public static void Main(string[] args)
    {
        IHost host = CreateHostBuilder(args).Build();
        host.Services.UseScheduler(scheduler => {
            scheduler
                .Schedule<ColetaProventosFIIJob>()
                .Hourly().Weekday()
                .RunOnceAtStart();
        });
        host.Run();
    }

In my job class, that inhrit IInvocable:

        public async Task Invoke()
        {
            await _coletaProventosFIIBusiness.Coleta();
        }

Class that contains Coleta() method:

namespace RadarFII.Business
{
    public class ColetaProventosFIIBusiness : IColetaProventosFIIBusiness
    {
        //private readonly IColetaEventosFIIService _coletaProventosFIIService;
        //private readonly IProventoFIIRepository _proventoFIIRepository;
        //private DateOnly dataHoje;

        //public ColetaProventosFIIBusiness(IColetaEventosFIIService coletaProventosFIIService,
        //                                    IProventoFIIRepository proventoFIIRepository)
        //{
        //    _coletaProventosFIIService = coletaProventosFIIService;
        //    _proventoFIIRepository = proventoFIIRepository;
        //}

        public async Task Coleta()
        {
            Console.WriteLine("Something...");
           // dataHoje = DateOnly.FromDateTime(DateTime.Now);
            //var AnunciosRealizadosHoje = await BuscarProventosAnunciadosHojeNaoColetados();

           // await SalvarNoBancoDeDados(AnunciosRealizadosHoje);
        }

        //private async Task<IEnumerable<ProventoFII>> BuscarProventosAnunciadosHojeNaoColetados()
        //{
        //    Console.WriteLine("Buscando...");
        //    var proventosAnunciadosHoje = await _coletaProventosFIIService.BuscarEventosFIIAnunciadosEm(dataHoje);

        //    var anunciosNaoColetados = await removeAnunciosProventosJaColetados(proventosAnunciadosHoje);

        //    var listaDeProventosAnunciadosHoje = await _coletaProventosFIIService.ExtraiProventosDeListaDeAnuncios(anunciosNaoColetados);

        //    return listaDeProventosAnunciadosHoje;
        //}

        //private async Task SalvarNoBancoDeDados(IEnumerable<ProventoFII> listaDeProventosDeFII)
        //{
        //    await _proventoFIIRepository.SalvaListaDeProventosFII(listaDeProventosDeFII);
        //}

        //private async Task<IEnumerable<AnuncioFII>> removeAnunciosProventosJaColetados(IEnumerable<AnuncioFII> listaDeProventosDeFIIcoletadosAgora)
        //{
        //    //busca fundos já coletados hoje
        //    var IdEventosJaColetadosHoje = await _proventoFIIRepository.SelectIdAnunciosDivulgadosEm(dataHoje);

        //    if (JaHouveColetaHoje(IdEventosJaColetadosHoje))
        //    {
        //        var novosLancamentosDeProventos = await removeProventosJaColetados(IdEventosJaColetadosHoje, listaDeProventosDeFIIcoletadosAgora);
        //        return novosLancamentosDeProventos;
        //    }
        //    return null;

        //}

        //private bool JaHouveColetaHoje(IEnumerable<string> proventosJaColetadosHoje)
        //{
        //    return proventosJaColetadosHoje.Count() > 0;
        //}

        //private async Task<IEnumerable<AnuncioFII>> removeProventosJaColetados(IEnumerable<string> IdEventosJaColetadosEmExecucaoAnterior,
        //                                                                    IEnumerable<AnuncioFII> anunciosColetadosNaAtualExecucao)
        //{
        //    return anunciosColetadosNaAtualExecucao.Where(anuncio => !IdEventosJaColetadosEmExecucaoAnterior.ToList().Contains(anuncio.id));
        //}
    }
}

The code above works, but when I uncomment, my code brokes. I don't know what is happening. I build the appliction using Console App model.

My DI is above:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices((hostContext, services) =>
            {
                services.AddScheduler();

                services.AddTransient<ColetaProventosFIIJob>();
                services.AddScoped<IColetaEventosFIIService, ColetaEventosFIIService>();
                services.AddScoped<IProventoFIIRepository, ProventoFIIRepository>();
                services.AddScoped<IColetaProventosFIIBusiness, ColetaProventosFIIBusiness>();
            });
jamesmh commented 1 year ago

In your job, what if you do Console.WriteLine("test") -> does that work? If so, then something is going on with your other code. Not sure 🤷‍♂️

jamesmh commented 1 year ago

No feedback - closing. Let me know if you need help.