-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from decimo3/PdfSender
Asynchronous invoice send system.
- Loading branch information
Showing
13 changed files
with
233 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Microsoft.Extensions.Logging; | ||
using telbot.Interfaces; | ||
using telbot.Services; | ||
using telbot.models; | ||
using telbot.handle; | ||
using System.Collections.Concurrent; | ||
namespace telbot.Helpers; | ||
public partial class PdfHandle | ||
{ | ||
private readonly IDatabase database; | ||
private readonly Configuration cfg; | ||
private readonly HandleMessage bot; | ||
private readonly ILogger logger; | ||
private readonly ConcurrentDictionary<String, pdfsModel> faturas = new(); | ||
private readonly Object _lock = new(); // Dedicated lock object | ||
public PdfHandle() | ||
{ | ||
this.bot = HandleMessage.GetInstance(); | ||
this.cfg = Configuration.GetInstance(); | ||
this.database = Database.GetInstance(); | ||
this.logger = Logger.GetInstance<PdfHandle>(); | ||
// Delete all files in the temporary folder | ||
var files = Directory.GetFiles(cfg.TEMP_FOLDER); | ||
foreach (var file in files) | ||
{ | ||
File.Delete(file); | ||
} | ||
var folders = Directory.GetDirectories(cfg.TEMP_FOLDER); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using Microsoft.Extensions.Logging; | ||
using telbot.models; | ||
namespace telbot.Helpers | ||
{ | ||
public partial class PdfHandle | ||
{ | ||
public async void Sender() | ||
{ | ||
logger.LogDebug("Realizando a procura de faturas para entregar..."); | ||
while (true) | ||
{ | ||
try | ||
{ | ||
await Task.Delay(cfg.TASK_DELAY); | ||
// Recupera as solicitações redirecionadas | ||
var solicitacoes = database.RecuperarSolicitacao( | ||
r => r.status == 300 && | ||
r.typeRequest == models.TypeRequest.pdfInfo); | ||
// Verifica se há solicitações a serem enviadas | ||
if (solicitacoes is null) | ||
{ | ||
continue; | ||
} | ||
var tasks = new List<Task>(); | ||
// Verifica em cada solicitação, se já foi gerada a fatura | ||
foreach (var solicitacao in solicitacoes) | ||
{ | ||
// Recupera a lista de faturas geradas | ||
|
||
var faturas_info = new List<pdfsModel>( | ||
faturas.Where(f => | ||
f.Value.timestamp >= solicitacao.received_at && | ||
f.Value.instalation == solicitacao.information | ||
).Select(f => f.Value).ToList()); | ||
// Verifica se já tem faturas para a solicitação e se é a quantidade esperada | ||
if (faturas_info.Count == solicitacao.instance) | ||
{ | ||
//! Se tudo der certo, aqui tem que começar a entregar as faturas | ||
tasks.Add(Sender(new List<pdfsModel>(faturas_info), solicitacao)); | ||
} | ||
// Verifica se a solicitação já não expirou | ||
if(solicitacao.response_at.AddMilliseconds(cfg.SAP_ESPERA) < DateTime.Now) | ||
{ | ||
solicitacao.status = 503; | ||
tasks.Add(bot.ErrorReport( | ||
request: solicitacao, | ||
error: new Exception("Não foi gerada nenhuma fatura pelo sistema SAP!") | ||
)); | ||
} | ||
} | ||
await Task.WhenAll(tasks); | ||
} | ||
catch (System.Exception erro) | ||
{ | ||
logger.LogError(erro, "Ocorreu um erro ao tentar enviar a fatura!"); | ||
} | ||
} | ||
} | ||
// Método separado para enviar as faturas | ||
private async Task Sender(List<pdfsModel> faturasInfo, logsModel solicitacao) | ||
{ | ||
var fluxoAtual = 0; | ||
var tasks = new List<Task>(); | ||
var fluxos = new Stream[solicitacao.instance]; | ||
foreach (var fatura in faturasInfo) | ||
{ | ||
if (fatura.status == pdfsModel.Status.sent) continue; | ||
var caminho = System.IO.Path.Combine(cfg.TEMP_FOLDER, fatura.filename); | ||
fluxos[fluxoAtual] = System.IO.File.OpenRead(caminho); | ||
tasks.Add(bot.SendDocumentAsyncWraper( | ||
solicitacao.identifier, | ||
fluxos[fluxoAtual], | ||
fatura.filename | ||
)); | ||
fatura.status = pdfsModel.Status.sent; | ||
logger.LogInformation("Enviada fatura ({fluxoAtual}/{quantidadeEsperada}): {filename}", | ||
++fluxoAtual, solicitacao.instance, fatura.filename); | ||
} | ||
await Task.WhenAll(tasks); | ||
foreach (var fluxo in fluxos) | ||
{ | ||
fluxo.Close(); | ||
} | ||
Remove(new List<pdfsModel>(faturasInfo)); | ||
bot.SucessReport(solicitacao); | ||
logger.LogInformation("Enviadas faturas para a instalação {instalation}", solicitacao.information); | ||
} | ||
} | ||
} |
Oops, something went wrong.