Agregar archivos de proyecto.
This commit is contained in:
164
ServicioGrupoSanchoToro/Comprobaciones.cs
Normal file
164
ServicioGrupoSanchoToro/Comprobaciones.cs
Normal file
@@ -0,0 +1,164 @@
|
||||
using bdGrupoSanchoToro;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.VisualBasic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using tsEFCore8.Extensiones;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
|
||||
namespace ServicioGrupoSanchoToro
|
||||
{
|
||||
internal class Comprobaciones
|
||||
{
|
||||
public static void CompruebaReplica(string NombreConexion = "replica")
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(NombreConexion))
|
||||
{
|
||||
if (DateAndTime.Today.Day == 1)
|
||||
{
|
||||
switch (DateAndTime.Today.Month)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
NombreConexion = "enero";
|
||||
break;
|
||||
}
|
||||
|
||||
case 2:
|
||||
{
|
||||
NombreConexion = "febrero";
|
||||
break;
|
||||
}
|
||||
|
||||
case 3:
|
||||
{
|
||||
NombreConexion = "marzo";
|
||||
break;
|
||||
}
|
||||
|
||||
case 4:
|
||||
{
|
||||
NombreConexion = "abril";
|
||||
break;
|
||||
}
|
||||
|
||||
case 5:
|
||||
{
|
||||
NombreConexion = "mayo";
|
||||
break;
|
||||
}
|
||||
|
||||
case 6:
|
||||
{
|
||||
NombreConexion = "junio";
|
||||
break;
|
||||
}
|
||||
|
||||
case 7:
|
||||
{
|
||||
NombreConexion = "julio";
|
||||
break;
|
||||
}
|
||||
|
||||
case 8:
|
||||
{
|
||||
NombreConexion = "agosto";
|
||||
break;
|
||||
}
|
||||
|
||||
case 9:
|
||||
{
|
||||
NombreConexion = "septiembre";
|
||||
break;
|
||||
}
|
||||
|
||||
case 10:
|
||||
{
|
||||
NombreConexion = "octubre";
|
||||
break;
|
||||
}
|
||||
|
||||
case 11:
|
||||
{
|
||||
NombreConexion = "noviembre";
|
||||
break;
|
||||
}
|
||||
|
||||
case 12:
|
||||
{
|
||||
NombreConexion = "diciembre";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
switch (DateAndTime.Today.DayOfWeek)
|
||||
{
|
||||
case DayOfWeek.Monday:
|
||||
{
|
||||
NombreConexion = "lunes";
|
||||
break;
|
||||
}
|
||||
|
||||
case DayOfWeek.Tuesday:
|
||||
{
|
||||
NombreConexion = "martes";
|
||||
break;
|
||||
}
|
||||
|
||||
case DayOfWeek.Wednesday:
|
||||
{
|
||||
NombreConexion = "miercoles";
|
||||
break;
|
||||
}
|
||||
|
||||
case DayOfWeek.Thursday:
|
||||
{
|
||||
NombreConexion = "jueves";
|
||||
break;
|
||||
}
|
||||
|
||||
case DayOfWeek.Friday:
|
||||
{
|
||||
NombreConexion = "viernes";
|
||||
break;
|
||||
}
|
||||
|
||||
case DayOfWeek.Saturday:
|
||||
{
|
||||
NombreConexion = "sabado";
|
||||
break;
|
||||
}
|
||||
|
||||
case DayOfWeek.Sunday:
|
||||
{
|
||||
NombreConexion = "domingo";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
tscGrupoSanchoToro bd = tscGrupoSanchoToro.NuevoContexto();
|
||||
tscGrupoSanchoToro bdr = tscGrupoSanchoToro.NuevoContexto(NombreConexion);
|
||||
int nr = bd.Database.SqlQueryRaw<int>("CALL totalregistrosbd()").ToList().FirstOrDefault();
|
||||
int nrr = bdr.Database.SqlQueryRaw<int>("CALL totalregistrosbd()").ToList().FirstOrDefault();
|
||||
|
||||
if (nr != nrr)
|
||||
throw new Exception("No coinciden los nº de registro de la réplica con los de producción");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logs.AñadeLog(tsUtilidades.Enumeraciones.TipoLog.Fallo, "Error en CompruebaReplica", "Detalle del error: " + Constants.vbCrLf + ex.Message, ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
18
ServicioGrupoSanchoToro/Configuracion.cs
Normal file
18
ServicioGrupoSanchoToro/Configuracion.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ServicioGrupoSanchoToro
|
||||
{
|
||||
public class Configuracion
|
||||
{
|
||||
public int SegundosMinimosEntreProcesos { get; set; }
|
||||
public string? DirectorioLogs { get; set; }
|
||||
public string? HoraProcesosDiarios { get; set; }
|
||||
public string? NombreConexionBD { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
116
ServicioGrupoSanchoToro/Logs.cs
Normal file
116
ServicioGrupoSanchoToro/Logs.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
using Microsoft.VisualBasic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ServicioGrupoSanchoToro
|
||||
{
|
||||
internal class Logs
|
||||
{
|
||||
private static object oBloqueoLog;
|
||||
private static EventLog el;
|
||||
public static void AñadeLog(tsUtilidades.Enumeraciones.TipoLog Tipo, string Asunto, string Mensaje, Exception e = null)
|
||||
{
|
||||
// ----------------------------------------------------------------------------------------------------
|
||||
// Descripción Sub: Gestión de logs de la aplicación
|
||||
// Fecha. Creacion: ???
|
||||
// Creada por: manmog
|
||||
// Ultima Modificacion: 24/11/2010
|
||||
//
|
||||
// Modificaciones:
|
||||
// ===============
|
||||
if (oBloqueoLog == null) oBloqueoLog = new object();
|
||||
lock (oBloqueoLog)
|
||||
{
|
||||
string sFicheroLog = Procesos.Conf.DirectorioLogs + "Log-" + DateTime.UtcNow.Year.ToString() + DateTime.UtcNow.Month.ToString() + ".txt";
|
||||
try
|
||||
{
|
||||
if (e != null)
|
||||
{
|
||||
if (el != null)
|
||||
el.WriteEntry(e.Message + Constants.vbCrLf + e.StackTrace, EventLogEntryType.Error);
|
||||
string sStackTrace = "Tipo excepción: " + e.ToString() + Constants.vbCrLf;
|
||||
Exception exError = e;
|
||||
do
|
||||
{
|
||||
sStackTrace += exError.StackTrace + Constants.vbCrLf;
|
||||
exError = exError.InnerException;
|
||||
}
|
||||
while (!Information.IsNothing(exError));
|
||||
if (sStackTrace != "")
|
||||
Mensaje += Constants.vbCrLf + "StackTrace: " + sStackTrace;
|
||||
}
|
||||
if (Tipo == tsUtilidades.Enumeraciones.TipoLog.Fallo)
|
||||
{
|
||||
sFicheroLog = Procesos.Conf.DirectorioLogs + "Errores-" + DateTime.UtcNow.Year.ToString() + DateTime.UtcNow.Month.ToString().PadLeft(2, '0') + ".txt";
|
||||
Asunto = "Error GrupoSanchoToro. " + ". Version:" + bdGrupoSanchoToro.db.Utilidades.VersionPrograma + ". " + Asunto;
|
||||
Mensaje = "Error GrupoSanchoToro. " + DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss") + " Enviado desde " + Environment.MachineName + ". Version:" + bdGrupoSanchoToro.db.Utilidades.VersionPrograma + ". Mensaje: " + Mensaje;
|
||||
}
|
||||
else
|
||||
{
|
||||
Asunto = "Advertencia GrupoSanchoToro. " + ". Version:" + bdGrupoSanchoToro.db.Utilidades.VersionPrograma + ". " + Asunto;
|
||||
Mensaje = "Advertencia GrupoSanchoToro. " + DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss") + " Enviado desde " + Environment.MachineName + ". Version:" + bdGrupoSanchoToro.db.Utilidades.VersionPrograma + ". " + Mensaje;
|
||||
}
|
||||
string sDireccionesEnvio = "manmog@gmail.com";
|
||||
string sServidorSMTP = "smtp.gmail.com";
|
||||
var sRemitente = "administracion@english-skills.net";
|
||||
tsCorreos.Funciones.EnviaCorreo(sServidorSMTP, sRemitente, sDireccionesEnvio, Asunto, Mensaje,null,null,"","", sRemitente, "", 587, true); // ant: ksswsleqfvmzzbig
|
||||
|
||||
Anadelogtxt(Mensaje + " --- " + ((e==null) ? "" : e.StackTrace), sFicheroLog);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
sFicheroLog = Procesos.Conf.DirectorioLogs + @"Errores\Errores-" + DateTime.UtcNow.Year.ToString() + DateTime.UtcNow.Month.ToString().PadLeft(2, '0') + ".txt";
|
||||
if (e != null)
|
||||
Mensaje += " --- " + e.StackTrace;
|
||||
Anadelogtxt(Mensaje + " ---" + ex.Message + " --- " + ex.StackTrace, sFicheroLog);
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void Anadelogtxt(string Mensaje, string FicheroLog)
|
||||
{
|
||||
System.IO.StreamWriter sw = null;
|
||||
try
|
||||
{
|
||||
Mensaje = Mensaje.Replace(Constants.vbCrLf, "---");
|
||||
if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(FicheroLog)))
|
||||
tsUtilidades.Utilidades.CreaEstructuraDirectorio(System.IO.Path.GetDirectoryName(FicheroLog));
|
||||
if (System.IO.File.Exists(FicheroLog))
|
||||
sw = System.IO.File.AppendText(FicheroLog);
|
||||
else
|
||||
sw = System.IO.File.CreateText(FicheroLog);
|
||||
Mensaje = DateTime.UtcNow.ToString() + "|" + Mensaje;
|
||||
|
||||
sw.WriteLine(Mensaje);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sDireccionesEnvio = "manmog@gmail.com";
|
||||
string sServidorSMTP = "smtp.gmail.com";
|
||||
var sRemitente = "administracion@english-skills.net";
|
||||
tsCorreos.Funciones.EnviaCorreo(sServidorSMTP, sRemitente, sDireccionesEnvio, "Error Anadelogtxt. " + Mensaje, Environment.MachineName + ".- " + ex.Message + Constants.vbCrLf + ex.StackTrace + Constants.vbCrLf + ex.Source, null,null,"","", sRemitente, "", 587, true);
|
||||
}
|
||||
catch (Exception ex2)
|
||||
{
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
sw.Close();
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
44
ServicioGrupoSanchoToro/Procesos.cs
Normal file
44
ServicioGrupoSanchoToro/Procesos.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace ServicioGrupoSanchoToro
|
||||
{
|
||||
public class Procesos
|
||||
{
|
||||
public static Configuracion Conf;
|
||||
|
||||
public static DateTime? HoraUtcUltimaEjecucionProcesos = null;
|
||||
public static bool Procesando;
|
||||
|
||||
public static void Procesar()
|
||||
{
|
||||
if (!Procesando && (HoraUtcUltimaEjecucionProcesos.HasValue == false || DateTime.UtcNow.Subtract(HoraUtcUltimaEjecucionProcesos.Value).TotalSeconds > Conf.SegundosMinimosEntreProcesos));
|
||||
{
|
||||
Procesando = true;
|
||||
HoraUtcUltimaEjecucionProcesos = DateTime.UtcNow;
|
||||
try
|
||||
{
|
||||
ProcesosCorreos.EnviaCorreos();
|
||||
//Comprobaciones.CompruebaReplica();
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
Debug.WriteLine(@"Procesar: EXCEPCIÓN: " + ex.Message + " " + ex.StackTrace);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Procesando = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
122
ServicioGrupoSanchoToro/ProcesosCorreos.cs
Normal file
122
ServicioGrupoSanchoToro/ProcesosCorreos.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
using bdGrupoSanchoToro;
|
||||
using Microsoft.VisualBasic;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Mail;
|
||||
using System.Net.Mime;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using tsUtilidades.Extensiones;
|
||||
|
||||
namespace ServicioGrupoSanchoToro
|
||||
{
|
||||
internal class ProcesosCorreos
|
||||
{
|
||||
internal static void EnviaCorreos()
|
||||
{
|
||||
try
|
||||
{
|
||||
var bd = tscGrupoSanchoToro.NuevoContexto();
|
||||
var correospendientes = bd.correos.Where(x => x.FechaAnulacion == null && x.FechaEnvio == null && x.idcuenta != null).ToList();
|
||||
ArrayList ficherosadjuntos = null/* TODO Change to default(_) if this is not a reference type */;
|
||||
bool bIntentar;
|
||||
var EmailEnPruebas = bd.enumeraciones.First(x => x.Codigo == "CONF.EMAILENPRUEBAS").ValorAlfabeticoLargo;
|
||||
var EmailCopiaOculta = bd.enumeraciones.First(x => x.Codigo == "CONF.EMAILCOPIAOCULTA").ValorAlfabeticoLargo;
|
||||
try
|
||||
{
|
||||
foreach (var email in correospendientes)
|
||||
{
|
||||
email.Asunto = email.Asunto.Replace(Environment.NewLine, " ");
|
||||
bIntentar = false;
|
||||
if (email.FechaUltimoIntento == null)
|
||||
bIntentar = true;
|
||||
else if (DateTime.UtcNow.Subtract(email.FechaUltimoIntento.Value).TotalHours > 1)
|
||||
bIntentar = true;
|
||||
if (bIntentar)
|
||||
{
|
||||
try
|
||||
{
|
||||
string sPasswd = email.idcuentaNavigation.Password;
|
||||
if (System.IO.Directory.Exists(@"c:\tecnosis.tfs"))
|
||||
email.Destinatario = "manmog@gmail.com";
|
||||
else if (!string.IsNullOrEmpty(EmailEnPruebas))
|
||||
{
|
||||
email.Destinatario = EmailEnPruebas;
|
||||
email.CopiaOculta = "manmog@gmail.com";
|
||||
}
|
||||
else if (EmailCopiaOculta != null && EmailCopiaOculta != "" && email.CodigoAplicacion != "ERRORES")
|
||||
{
|
||||
if (string.IsNullOrEmpty(email.CopiaOculta))
|
||||
email.CopiaOculta = EmailCopiaOculta;
|
||||
else
|
||||
email.CopiaOculta = EmailCopiaOculta + ";" + EmailCopiaOculta;
|
||||
}
|
||||
// End If
|
||||
List<Attachment> attachments = new List<Attachment>();
|
||||
string sCuerpoHtml = System.Text.Encoding.UTF8.GetString(bd.ficherosconfiguracion.Where(x => x.Codigo == "PLANTILLA_CORREOS_EMP_" + email.idcuentaNavigation.idEmpresa.ToString()).First().Configuracion);
|
||||
// Dim sCuerpoHtml As String = System.Text.Encoding.UTF8.GetString(IO.File.ReadAllBytes("c:\tmp\PlantillaCorreos.html"))
|
||||
sCuerpoHtml = sCuerpoHtml.Replace("$cuerpo$", email.Cuerpo).Replace(Constants.vbCrLf, "<br>");
|
||||
|
||||
AlternateView avHtml = AlternateView.CreateAlternateViewFromString(sCuerpoHtml, null/* TODO Change to default(_) if this is not a reference type */, MediaTypeNames.Text.Html);
|
||||
List<Attachment> ats = null;
|
||||
if (!string.IsNullOrEmpty(email.RutaFicheroAdjunto))
|
||||
{
|
||||
ats = new List<Attachment>();
|
||||
Attachment at = new Attachment(new MemoryStream(File.ReadAllBytes(email.RutaFicheroAdjunto)), System.IO.Path.GetFileName(email.RutaFicheroAdjunto));
|
||||
ats.Add(at);
|
||||
}
|
||||
if (email.idFicheroAdjunto.HasValue)
|
||||
{
|
||||
if (ats == null)
|
||||
ats = new List<Attachment>();
|
||||
Attachment at = new Attachment(new MemoryStream(email.idFicheroAdjuntoNavigation.Fichero), System.IO.Path.GetFileName(email.idFicheroAdjuntoNavigation.NombreFichero));
|
||||
ats.Add(at);
|
||||
}
|
||||
foreach (var fad in email.ficherosadjuntos)
|
||||
{
|
||||
if (ats == null)
|
||||
ats = new List<Attachment>();
|
||||
Attachment at = new Attachment(new MemoryStream(fad.idFicheroNavigation.Fichero), System.IO.Path.GetFileName(fad.idFicheroNavigation.NombreFichero));
|
||||
ats.Add(at);
|
||||
}
|
||||
List<AlternateView> avs = new List<AlternateView>();
|
||||
avs.Add(avHtml);
|
||||
tsCorreos.Funciones.EnviaCorreo(email.idcuentaNavigation.ServidorSMTP, email.Remitente, email.Destinatario, email.Asunto, sCuerpoHtml, ats, avs, email.Copia, email.CopiaOculta, email.idcuentaNavigation.CuentaCorreo, email.idcuentaNavigation.Password, (int)email.idcuentaNavigation.Puerto, email.idcuentaNavigation.SSL, true, email.idcuentaNavigation.ResponderA);
|
||||
// tsl5.Correo.Funciones.EnviaCorreo(email.cuentascorreo.ServidorSMTP, email.Remitente, email.Destinatario, email.Asunto, email.Cuerpo, attachments, avs, , , email.cuentascorreo.CuentaCorreo, sPasswd, email.cuentascorreo.Puerto, email.cuentascorreo.SSL, True)
|
||||
email.FechaEnvio = DateTime.UtcNow;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Call AñadeLog(Enumeraciones.TipoLog.Advertencia, "El correo para el destinatario " & email.DESTINATARIO & " ha sido rechazado durante 48 horas y será marcado como anulado.", "Detalle del error: " & vbCrLf & ex.Message, ex)
|
||||
email.FechaUltimoIntento = DateTime.UtcNow;
|
||||
email.MensajeError = ex.Message.Acortar(512);
|
||||
if (DateTime.UtcNow.Subtract(email.FechaCreacion.Value).TotalHours > 48)
|
||||
{
|
||||
email.FechaAnulacion = DateTime.UtcNow;
|
||||
// GeneraRegistroCorreoExcepcion(Nothing, ex, "Procesar")
|
||||
Logs.AñadeLog(tsUtilidades.Enumeraciones.TipoLog.Fallo, "El correo para el destinatario " + email.Destinatario + " ha sido rechazado durante 48 horas y será marcado como anulado.", "Detalle del error: " + Constants.vbCrLf + ex.Message, ex);
|
||||
}
|
||||
else
|
||||
{
|
||||
string sFicheroLog = Procesos.Conf.DirectorioLogs + @"Errores\Errores-" + DateTime.UtcNow.Year.ToString() + DateTime.UtcNow.Month.ToString() + ".txt";
|
||||
Logs.Anadelogtxt("Error en EnviaCorreo. " + ex.Message + Constants.vbCrLf + ex.StackTrace, sFicheroLog);
|
||||
}
|
||||
}
|
||||
}
|
||||
bd.SaveChanges();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Dim sFicheroLog = DirectorioLogs & "Errores-" & Now.Year.ToString & Now.Month.ToString.PadLeft(2, "0") & ".txt"
|
||||
Logs.AñadeLog(tsUtilidades.Enumeraciones.TipoLog.Fallo, ex.Message, "Detalle del error: " + Constants.vbCrLf + ex.Message, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
46
ServicioGrupoSanchoToro/Program.cs
Normal file
46
ServicioGrupoSanchoToro/Program.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Microsoft.Extensions.Logging.Configuration;
|
||||
using Microsoft.Extensions.Logging.EventLog;
|
||||
using ServicioGrupoSanchoToro;
|
||||
using System.Reflection;
|
||||
|
||||
|
||||
|
||||
//HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
|
||||
//builder.Services.AddWindowsService(options =>
|
||||
//{
|
||||
// options.ServiceName = ".NET Joke Service";
|
||||
//});
|
||||
|
||||
//LoggerProviderOptions.RegisterProviderOptions<
|
||||
// EventLogSettings, EventLogLoggerProvider>(builder.Services);
|
||||
|
||||
//builder.Services.AddSingleton<JokeService>();
|
||||
//builder.Services.AddHostedService<WindowsBackgroundService>();
|
||||
|
||||
//IHost host = builder.Build();
|
||||
//host.Run();
|
||||
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
bdGrupoSanchoToro.db.Utilidades.VersionPrograma = tsUtilidades.Utilidades.ExtraeValorCadena(Assembly.GetExecutingAssembly().FullName, "Version");
|
||||
var host = CreateHostBuilder(args).Build();
|
||||
host.Run();
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureServices((hostContext, services) =>
|
||||
{
|
||||
IConfiguration configuration = hostContext.Configuration;
|
||||
|
||||
Configuracion options = configuration.GetSection("Configuracion").Get<Configuracion>();
|
||||
|
||||
services.AddSingleton(options);
|
||||
services.AddWindowsService(options=>options.ServiceName="Servicio GrupoSanchoToro");
|
||||
|
||||
services.AddHostedService<Worker>();
|
||||
});
|
||||
}
|
||||
19
ServicioGrupoSanchoToro/Properties/launchSettings.json
Normal file
19
ServicioGrupoSanchoToro/Properties/launchSettings.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"profiles": {
|
||||
"ServicioGrupoSanchoToro": {
|
||||
"commandName": "Project",
|
||||
"environmentVariables": {
|
||||
"DOTNET_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true
|
||||
},
|
||||
"WSL": {
|
||||
"commandName": "WSL2",
|
||||
"environmentVariables": {
|
||||
"DOTNET_ENVIRONMENT": "Development"
|
||||
},
|
||||
"distributionName": ""
|
||||
}
|
||||
},
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json"
|
||||
}
|
||||
23
ServicioGrupoSanchoToro/ServicioGrupoSanchoToro.csproj
Normal file
23
ServicioGrupoSanchoToro/ServicioGrupoSanchoToro.csproj
Normal file
@@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Worker">
|
||||
|
||||
<PropertyGroup>
|
||||
<AssemblyVersion>1.0.0.0</AssemblyVersion>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>dotnet-ServicioGrupoSanchoToro-2a4f24d8-0048-43c3-b44a-f4f5ce65c028</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="9.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="Quartz" Version="3.13.1" />
|
||||
<PackageReference Include="tsCorreos" Version="1.0.2" />
|
||||
<PackageReference Include="tsUtilidades" Version="1.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\bdGrupoSanchoToro\bdGrupoSanchoToro.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
111
ServicioGrupoSanchoToro/Worker.cs
Normal file
111
ServicioGrupoSanchoToro/Worker.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz.Impl;
|
||||
using Quartz;
|
||||
using System.Drawing.Text;
|
||||
using bdGrupoSanchoToro.db;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using Microsoft.VisualBasic;
|
||||
using System.Reflection;
|
||||
using bdGrupoSanchoToro;
|
||||
|
||||
namespace ServicioGrupoSanchoToro
|
||||
{
|
||||
|
||||
public class Worker : BackgroundService
|
||||
{
|
||||
private readonly ILogger<Worker> _logger;
|
||||
|
||||
public Worker(ILogger<Worker> logger, Configuracion Conf)
|
||||
{
|
||||
_logger = logger;
|
||||
Procesos.Conf = Conf;
|
||||
var bd = tscGrupoSanchoToro.NuevoContexto(Conf.NombreConexionBD);
|
||||
var ctasgmail = bd.cuentascorreo.Where(x => x.ServidorSMTP == "smtp.gmail.com").AsEnumerable().GroupBy(x => x.Remitente).Select(x => x.FirstOrDefault()).ToList();
|
||||
List<tsCorreos.TokenGmail> lt = new List<tsCorreos.TokenGmail>();
|
||||
foreach (var cta in ctasgmail)
|
||||
{
|
||||
tsCorreos.TokenGmail tg = new tsCorreos.TokenGmail();
|
||||
var Config = new GmailConfig() { ClientSecret = bd.ficherosconfiguracion.First(x => x.Codigo == cta.Remitente).Configuracion, TokenFolder = @"C:\ProgramData\Gmail\TokenGmail", Usuario = cta.Remitente, Remitente = cta.Remitente, NombreRemitente = cta.Remitente };
|
||||
tg.Config = Config;
|
||||
tg.id = cta.Remitente;
|
||||
lt.Add(tg);
|
||||
}
|
||||
tsCorreos.TokenGmail.ListaTokens = lt;
|
||||
}
|
||||
|
||||
|
||||
protected override async System.Threading.Tasks.Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
string Mensaje = "Servicio GrupoSanchoToro Iniciado. Versi<73>n: " + tsUtilidades.Utilidades.ExtraeValorCadena(Assembly.GetExecutingAssembly().FullName, "Version");
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
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<65>fica
|
||||
if (Procesos.Conf.HoraProcesosDiarios != null)
|
||||
{
|
||||
Mensaje += " Hora Procesos Programados: " + Procesos.Conf.HoraProcesosDiarios;
|
||||
int Hora = int.Parse(Procesos.Conf.HoraProcesosDiarios.Split(":")[0]);
|
||||
int Minutos = int.Parse(Procesos.Conf.HoraProcesosDiarios.Split(":")[1]);
|
||||
ITrigger trigger = TriggerBuilder.Create()
|
||||
.WithIdentity("MiTareaDiaria", "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();
|
||||
|
||||
Console.WriteLine("Tarea diaria programada creada.");
|
||||
}
|
||||
|
||||
Logs.A<EFBFBD>adeLog(tsUtilidades.Enumeraciones.TipoLog.InicioServicio, Mensaje, Mensaje);
|
||||
Comprobaciones.CompruebaReplica("replica");
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
if (_logger.IsEnabled(LogLevel.Information))
|
||||
{
|
||||
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||
}
|
||||
Procesos.Procesar();
|
||||
await System.Threading.Tasks.Task.Delay(1000, stoppingToken);
|
||||
}
|
||||
Logs.A<EFBFBD>adeLog(tsUtilidades.Enumeraciones.TipoLog.FinServicio, "Detenci<63>n " + Mensaje, "Detenci<63>n " + Mensaje);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
Logs.A<EFBFBD>adeLog(tsUtilidades.Enumeraciones.TipoLog.FinServicio, "Detenci<63>n " + Mensaje, "Detenci<63>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 class TareasProgramadas : IJob
|
||||
{
|
||||
public Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
Comprobaciones.CompruebaReplica();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
14
ServicioGrupoSanchoToro/appsettings.json
Normal file
14
ServicioGrupoSanchoToro/appsettings.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"Configuracion": {
|
||||
"SegundosMinimosEntreProcesos": "60",
|
||||
"DirectorioLogs": "c:\\m3soft\\logs\\",
|
||||
"HoraProcesosDiarios": "06:30",
|
||||
"NombreConexionBD": "Producción VPN"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user