186 lines
6.8 KiB
C#
186 lines
6.8 KiB
C#
using bdAsegasa;
|
|
using bdAsegasa.db;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.VisualBasic;
|
|
using Quartz;
|
|
using Quartz.Impl;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
using System.Diagnostics;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Drawing.Text;
|
|
using System.Reflection;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Services.Description;
|
|
using tsUtilidades.SEPA_3414;
|
|
using WSAsegasa;
|
|
|
|
namespace WSAsegasa
|
|
{
|
|
|
|
public class Worker : BackgroundService
|
|
{
|
|
private readonly ILogger<Worker> _logger;
|
|
|
|
public Worker(ILogger<Worker> logger, Configuracion Conf)
|
|
{
|
|
_logger = logger;
|
|
Procesos.Conf = Conf;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override async System.Threading.Tasks.Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
string Mensaje = "WSAsegasa. Versión: " + tsUtilidades.Utilidades.ExtraeValorCadena(Assembly.GetExecutingAssembly().FullName, "Version");
|
|
try
|
|
{
|
|
|
|
if (Procesos.Conf.HoraProcesosDiarios != null)
|
|
{
|
|
Mensaje += " Hora Procesos Programados: " + Procesos.Conf.HoraProcesosDiarios;
|
|
CreaTareaProcesosDiarios();
|
|
}
|
|
CreaTareaEnvioFacturas();
|
|
|
|
Logs.AñadeLog(tsUtilidades.Enumeraciones.TipoLog.InicioServicio, "Inicio " + Mensaje);
|
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
{
|
|
if (_logger.IsEnabled(LogLevel.Information))
|
|
{
|
|
_logger.LogInformation("Servicio en ejecución: {time}", DateTimeOffset.Now);
|
|
//Creo q aqui se deberia llamar a los servicios
|
|
}
|
|
Procesos.Procesar();
|
|
await System.Threading.Tasks.Task.Delay(1000*60*5, stoppingToken);
|
|
}
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
// Logs.AñadeLog(tsUtilidades.Enumeraciones.TipoLog.FinServicio, "Detención " + Mensaje);
|
|
// When the stopping token is canceled, for example, a call made from services.msc,
|
|
// we shouldn't exit with a non-zero exit code. In other words, this is expected...
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "{Message}", ex.Message);
|
|
Environment.Exit(1);
|
|
}
|
|
|
|
}
|
|
public override async Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
string Mensaje = "WSAsegasa. Versión: " + tsUtilidades.Utilidades.ExtraeValorCadena(Assembly.GetExecutingAssembly().FullName, "Version");
|
|
Logs.AñadeLog(tsUtilidades.Enumeraciones.TipoLog.FinServicio, "Finalizando " + Mensaje);
|
|
|
|
await base.StopAsync(cancellationToken);
|
|
Logs.AñadeLog(tsUtilidades.Enumeraciones.TipoLog.FinServicio, "Servicio Finalizado " + Mensaje);
|
|
}
|
|
|
|
|
|
|
|
private void CreaTareaProcesosDiarios()
|
|
{
|
|
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
|
|
IScheduler scheduler = schedulerFactory.GetScheduler().Result;
|
|
scheduler.Start().Wait();
|
|
// Crear un trabajo
|
|
IJobDetail job = JobBuilder.Create<TareasProgramadas>()
|
|
.WithIdentity("TareasProgramadas", "Grupo1")
|
|
.Build();
|
|
// Crear un trigger que se ejecute diariamente a una hora específica
|
|
|
|
|
|
int Hora = int.Parse(Procesos.Conf.HoraProcesosDiarios.Split(":")[0]);
|
|
int Minutos = int.Parse(Procesos.Conf.HoraProcesosDiarios.Split(":")[1]);
|
|
ITrigger trigger = TriggerBuilder.Create()
|
|
.WithIdentity("TareasProgramadas", "Grupo1")
|
|
.StartAt(DateBuilder.TodayAt(Hora, Minutos, 0))
|
|
.WithSimpleSchedule(x => x
|
|
.WithIntervalInHours(24)
|
|
.RepeatForever())
|
|
.Build();
|
|
|
|
// Programar el trabajo con el trigger
|
|
scheduler.ScheduleJob(job, trigger).Wait();
|
|
}
|
|
|
|
private void CreaTareaEnvioFacturas()
|
|
{
|
|
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
|
|
IScheduler scheduler = schedulerFactory.GetScheduler().Result;
|
|
scheduler.Start().Wait();
|
|
// Crear un trabajo
|
|
IJobDetail job = JobBuilder.Create<EnvioFacturas>()
|
|
.WithIdentity("EnvioFacturas", "Grupo1")
|
|
.Build();
|
|
// Crear un trigger que se ejecute diariamente a una hora específica
|
|
|
|
ITrigger trigger = TriggerBuilder.Create()
|
|
.WithIdentity("EnvioFacturas", "Grupo1")
|
|
.StartAt(DateBuilder.TodayAt(DateTime.Now.Hour, DateTime.Now.Minute, 0))
|
|
.WithSimpleSchedule(x => x
|
|
.WithIntervalInMinutes(15)
|
|
.RepeatForever())
|
|
.Build();
|
|
|
|
// Programar el trabajo con el trigger
|
|
scheduler.ScheduleJob(job, trigger).Wait();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public class TareasProgramadas : IJob
|
|
{
|
|
public Task Execute(IJobExecutionContext context)
|
|
{
|
|
//var ch = bdTecnosis.db.chequeos;
|
|
// var bd = bdTecnosis.tscTecnosis.NuevoContexto();
|
|
//var c = bd.chequeos.FirstOrDefault(x => x.idChequeo == ch.idChequeo);
|
|
//Comprobaciones.ChequeaColaCorreo(bd,c);
|
|
//Comprobaciones.CompruebaReplica();
|
|
//Aqui van los procesos y comprobaciones
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|
|
|
|
|
|
public class EnvioFacturas : IJob
|
|
{
|
|
private static readonly SemaphoreSlim _semaforo = new SemaphoreSlim(1, 1);
|
|
|
|
public async Task Execute(IJobExecutionContext context)
|
|
{
|
|
var cancellationToken = context.CancellationToken;
|
|
|
|
if (await _semaforo.WaitAsync(0, cancellationToken)) // no espera si está ocupado
|
|
{
|
|
try
|
|
{
|
|
var ft = new ProcesosVeriFactuAsegasa();
|
|
await ft.Iniciar(cancellationToken);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logs.AñadeLog(tsUtilidades.Enumeraciones.TipoLog.Fallo, ex.Message, ex);
|
|
}
|
|
finally
|
|
{
|
|
_semaforo.Release();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var bd = tscgestionasegasa.NuevoContexto(Procesos.Conf.NombreConexionBD, true, false, true, "ProcesosVeriFactuTecnosis");
|
|
var cvf = bd.enumeraciones.First(x => x.Codigo == "VF.CONF");
|
|
if (DateTime.Now.Hour < cvf.ValorNumerico2.Value) Logs.AñadeLog(tsUtilidades.Enumeraciones.TipoLog.Advertencia, "Bloqueo en EnvioFacturas");
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|