200 lines
7.0 KiB
C#
200 lines
7.0 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Data;
|
|
using System.Text;
|
|
using System.Diagnostics;
|
|
using System.Collections.Generic;
|
|
using Amib.Threading;
|
|
using PDFGenerator.BusinessLayer;
|
|
using PipelineLib;
|
|
using System.Threading;
|
|
using System.Configuration;
|
|
using System.Linq;
|
|
using PDFGenerator.Presentation.TemplateGenerator;
|
|
using DataAccessLayer;
|
|
using NLog;
|
|
using NLog.Fluent;
|
|
using System.Data.SqlClient;
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using static System.SystemEx;
|
|
|
|
namespace GestorePDF.Logic
|
|
{
|
|
public class ThreadManager
|
|
{
|
|
private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
|
//Thread[] pdfGenerationThreads;
|
|
|
|
public void Main(int _volthreads = 0)
|
|
{
|
|
string ambiente = CACHE.AMBIENTE;
|
|
//string funzione = "GESTORE PDF";
|
|
bool inviaFlussoGiornaliero = !CACHE.Periodico && CACHE.INVIO_HOST;
|
|
int volThreads = CACHE.volThreads;
|
|
if (_volthreads > 0) volThreads = _volthreads;
|
|
System.Globalization.CultureInfo.CreateSpecificCulture(CACHE.CultureToUse);
|
|
|
|
try
|
|
{
|
|
ProcessPdfDocuments(volThreads);
|
|
}
|
|
catch (Exception ex1)
|
|
{
|
|
logger.Errors(ex1);
|
|
}
|
|
|
|
try
|
|
{
|
|
if (inviaFlussoGiornaliero)
|
|
{
|
|
SmartFTPThread.Logic.FtpProcessSeparated.GestioneFlussoGiornaliero();
|
|
inviaFlussoGiornaliero = false;
|
|
}
|
|
}
|
|
catch (Exception ex2)
|
|
{
|
|
logger.Errors(ex2);
|
|
}
|
|
|
|
}
|
|
|
|
private void ProcessPdfDocuments(int volThreads)
|
|
{
|
|
int pagesize = CACHE.page_size;
|
|
List<DataThread> _dataThreads = null;
|
|
|
|
DateTime startsp = DateTime.Now;
|
|
logger.Debugs("* start extracting data at " + startsp.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
|
|
using (UtilityBusinessLayer ubl = new UtilityBusinessLayer())
|
|
{
|
|
_dataThreads = ubl.getDataThreads(volThreads);
|
|
}
|
|
DateTime endsp = DateTime.Now;
|
|
logger.Debugs("* end extracting data at " + endsp.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
|
|
logger.Debugs("GetDataThreads: Elapsed " + ((endsp - startsp).TotalSeconds).ToString() + (char)34);
|
|
|
|
if (_dataThreads != null && _dataThreads.Count > 0)
|
|
{
|
|
int totalthreads = _dataThreads.Count;
|
|
int pagecount = _dataThreads.Count.PageCount(pagesize);
|
|
logger.Debugs($"GetDataThreads: Extracted {_dataThreads.Count} Records");
|
|
|
|
for (int page = 0; page < pagecount; page++)
|
|
{
|
|
|
|
var elements = _dataThreads.GetPage(page, pagesize);
|
|
Track(page,elements);
|
|
|
|
startsp = DateTime.Now;
|
|
logger.Debugs("* start parallel calculation at " + startsp.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
|
|
using (UtilityBusinessLayer ubl = new UtilityBusinessLayer())
|
|
{
|
|
ubl.ParallelExecutionOverThreads(elements);
|
|
}
|
|
endsp = DateTime.Now;
|
|
logger.Debugs("* end parallel calculation at " + endsp.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
|
|
logger.Debugs("ParallelCalculation: Elapsed " + ((endsp - startsp).TotalSeconds).ToString() + (char)34);
|
|
|
|
Thread[] pdfGenerationThreads = new Thread[elements.Count];
|
|
AddDataThreadsToQueue(elements);
|
|
logger.Debugs(string.Format("Aggiunta di {0} PDF da produrre", elements.Count.ToString()));
|
|
|
|
for (int i = 0; i < elements.Count; i++)
|
|
{
|
|
pdfGenerationThreads[i] = new Thread(MasterThread.ProcessDataInSingleThread) { IsBackground = true };
|
|
|
|
pdfGenerationThreads[i].Start();
|
|
}
|
|
|
|
while (true)
|
|
{
|
|
int threadsRunning = pdfGenerationThreads.Where(x => x.IsAlive).Count();
|
|
if (threadsRunning == 0)
|
|
{
|
|
break;
|
|
}
|
|
Thread.Sleep(100);
|
|
}
|
|
|
|
logger.Debugs($"Page={page + 1}/{pagecount} executed at " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
|
|
|
|
// pulisco la memoria
|
|
GC.Collect();
|
|
GC.WaitForPendingFinalizers();
|
|
|
|
} //page
|
|
|
|
}
|
|
else
|
|
{
|
|
logger.Debugs("Nessun pdf da processare!");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
private void AddDataThreadsToQueue(List<DataThread> dataThreads)
|
|
{
|
|
if (dataThreads != null)
|
|
{
|
|
foreach (DataThread t in dataThreads)
|
|
{
|
|
MasterThread.ThreadsQueue.Enqueue(t);
|
|
}
|
|
}
|
|
}
|
|
|
|
public int Recupero(List<RecuperoPDF> letterepdf)
|
|
{
|
|
int inseriti = 0;
|
|
PDFGenerator.PDFGenerator p = new PDFGenerator.PDFGenerator();
|
|
foreach (var lettera in letterepdf)
|
|
{
|
|
DataThread dato = new DataThread(DBProvider.SqlServerStampeC6)
|
|
{
|
|
CodiceContratto = lettera.CodiceContratto,
|
|
Rete = lettera.Rete,
|
|
CodiceFiscale = lettera.CodiceFiscale,
|
|
NomeCliente = lettera.NomeCliente,
|
|
CognomeCliente = lettera.CognomeCliente,
|
|
Agente = lettera.CodicePB,
|
|
NomeAgente = lettera.NomePB,
|
|
CognomeAgente = lettera.CognomePB,
|
|
TotalePagine = lettera.numeroPagine,
|
|
Stream = lettera.Bytes
|
|
};
|
|
|
|
int identificativo = p.InserisciTest(dato);
|
|
if (identificativo > 0)
|
|
{
|
|
logger.Debugs($"Identificativo scritto={identificativo} per CF={lettera.CodiceFiscale} e Rete={lettera.Rete}");
|
|
inseriti++;
|
|
}
|
|
|
|
}
|
|
return inseriti;
|
|
}
|
|
|
|
private void Track(int page,List<DataThread> elements)
|
|
{
|
|
List<MinimalTrack> subelements = new List<MinimalTrack>();
|
|
foreach (var element in elements)
|
|
{
|
|
subelements.Add(
|
|
new MinimalTrack()
|
|
{
|
|
codicefiscale = element.CodiceFiscale,
|
|
rete = element.Rete,
|
|
tipocontratto = EnumHelper<TipoContratto>.GetEnumDescription(element.TipoContratto.ToInt32()),
|
|
SeiDa = EnumHelper<TipoContratto>.GetEnumCategory(element.TipoContratto.ToInt32())
|
|
});
|
|
}
|
|
CACHE.Track(page,subelements);
|
|
}
|
|
|
|
|
|
}
|
|
}
|