Aspnet Mvc Automated Routine

Asked

Viewed 186 times

0

I have the class Clientefirm and the class Archivists one client has a list of tax files (a maximum of 12 per year, one per month) and one Archivist has a Client.

public class ArquivosFiscais
{
     public virtual ClienteEmpresa ClienteEmpresa { get; set; }
        [ForeignKey("ClienteEmpresa")]
        public int IdClienteEmpresa { get; set; }
}

I need to create a routine, which is launched automatically, without user interaction, on the first day of each month. and I need her to be Stardata, just once every month. I need to create a filing cabinet for each client

So I created this method. (I don’t know if the logic is right).

private Contexto db = new Contexto();

        public void CriarArquivoPrimeiroDiaDeCadaMes()
        {
            DateTime data = DateTime.Today;
            DateTime primeiroDiaDoMes = new DateTime(data.Year, data.Month, 1);
            if (data == new DateTime(data.Year, data.Month, 1))
            {
                var cliente = db.ClienteEmpresaDb.Where(c => c.StatusCliente == Smc.Dominio.Model.StatusCliente.Ativo || c.StatusCliente == Smc.Dominio.Model.StatusCliente.Bloqueado);
                foreach (var item in cliente)
                {
                    using (var sal = new Contexto())
                    {
                        ArquivosFiscais af = new ArquivosFiscais();
                        af.DataAbertura = DateTime.Now;
                        af.DataDoEnvio = null;
                        af.IdClienteEmpresa = item.Id;
                        af.StatusArquivo = Smc.Dominio.Model.ArquivosFiscais.StatusArquivo.NaoEnviado;
                        af.MesDeReferencia = new DateTime(data.Month);
                        sal.ArquivosFiscaisDb.Add(af);
                        sal.SaveChanges();
                    }
                }
            }
        }

I don’t know if the logic is right. how do I start this method automatically?

  • 2

    As your system is web I advise you to do your routine in the bank through some job, or else use some schedule with Hangfire: https://www.hangfire.io/

  • 1

    I would also recommend Hangfire

  • It was the best option, Hangfire, it’s a hand on the wheel, I didn’t know.

1 answer

1


You need some Scheduler, among the several options highlight 2 I’ve used:

  1. Hangfire (the most common) https://hangfire.io Hangfire is very complete for executing tasks like call and forget (fire & Forget), very used to perform tasks outside the user’s thread. It can be used for the purpose of executing a process according to a schedule (1x per day, 1x per month etc) but its focus is not this. It creates its own database where it stores scheduled tasks and has a Dashboard to check the status of each execution. By being very thorough and needing to create a database, I usually use it only when my system will have a lot of communication with third party services, email submissions, long processes etc. It shines in this part.

  2. Quartz (the simplest) https://www.quartz-scheduler.net/ This is the simplest to implement, its own application is the Scheduler and does not require a database. To schedule tasks that occur from time to time, I usually use it. There is no Dashboard, there is no status, but it works...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.