diff --git a/EnvelopeGenerator.Common/EnvelopeGenerator.Common.vbproj b/EnvelopeGenerator.Common/EnvelopeGenerator.Common.vbproj index 6f69ba5b..10fa4f46 100644 --- a/EnvelopeGenerator.Common/EnvelopeGenerator.Common.vbproj +++ b/EnvelopeGenerator.Common/EnvelopeGenerator.Common.vbproj @@ -281,6 +281,7 @@ + diff --git a/EnvelopeGenerator.Common/Jobs/APIBackendJobs/SendInvitationMailJob.vb b/EnvelopeGenerator.Common/Jobs/APIBackendJobs/SendInvitationMailJob.vb new file mode 100644 index 00000000..f61c54c0 --- /dev/null +++ b/EnvelopeGenerator.Common/Jobs/APIBackendJobs/SendInvitationMailJob.vb @@ -0,0 +1,170 @@ +Imports DigitalData.Modules.Database +Imports DigitalData.Modules.Logging +Imports DigitalData.Modules.Base +Imports GdPicture14 +Imports Quartz +Imports System.Security.Cryptography +Imports System.IO +Imports EnvelopeGenerator.Common.Jobs.FinalizeDocument.FinalizeDocumentExceptions +Imports EnvelopeGenerator.Common.Jobs.FinalizeDocument +Imports EnvelopeGenerator.Common.Constants +Imports DevExpress.DataProcessing +Imports System.Data.SqlClient +Imports DevExpress.XtraRichEdit.Layout.Engine + +Namespace Jobs + Public Class SendInvitationMailJob + Implements IJob + + + Private LogConfig As LogConfig + Private Logger As Logger + Private Database As MSSQLServer + Private Config As DbConfig + + Private ConfigModel As ConfigModel + Private EnvelopeModel As EnvelopeModel + Private ReceiverModel As ReceiverModel + Private ActionService As ActionService + + + Private ReadOnly CompleteWaitTime As Integer = 1 + + Private myTempFiles As TempFiles + + Private Class EnvelopeData + Public EnvelopeId As Integer + Public EnvelopeUUID As String + Public DocumentPath As String + End Class + + Public Function Execute(pContext As IJobExecutionContext) As Task Implements IJob.Execute + LogConfig = pContext.MergedJobDataMap.Item(Constants.LOGCONFIG) + Logger = LogConfig.GetLogger() + myTempFiles = New TempFiles(LogConfig) + myTempFiles.Create() + Dim JobId = pContext.JobDetail.Key + Logger.Info("SendInvMail - Starting job {0}", JobId) + + Try + Logger.Debug("SendInvMail - Loading Database..") + Database = GetDatabase(pContext, LogConfig) + + Logger.Debug("SendInvMail - Loading Models & Services") + Dim oState = GetState() + InitializeModels(oState) + + Logger.Debug("SendInvMail - Loading Configuration..") + Config = ConfigModel.LoadConfiguration() + oState.DbConfig = Config + + InitializeServices(oState) + Config.DocumentPath = Config.DocumentPath + + Logger.Debug("SendInvMail - ExportPath: [{0}]", Config.ExportPath) + + Dim oCompleteStatus As Integer = Constants.EnvelopeStatus.EnvelopeCompletelySigned + Dim oSql = $"SELECT * FROM TBSIG_ENVELOPE where SOURCE = 'API' AND STATUS = 1003 order by guid" + Dim oTable = Database.GetDatatable(oSql) + + Dim oEnvelopeIds As List(Of Integer) = oTable.Rows.Cast(Of DataRow). + Select(Function(r) r.Item("GUID")). + Cast(Of Integer). + ToList() + + If oEnvelopeIds.Count > 0 Then + Logger.Info("SendInvMail - Found [{0}] envelopes.", oEnvelopeIds.Count) + End If + + Dim oTotal As Integer = oEnvelopeIds.Count + Dim oCurrent As Integer = 1 + + For Each oId In oEnvelopeIds + Logger.Info("SendInvMail - Gathering Info for Envelope [{0}] ({1}/{2})", oId, oCurrent, oTotal) + Logger.Debug("SendInvMail - Loading Envelope..") + Try + Dim oEnvelope = EnvelopeModel.GetById(oId) + If oEnvelope Is Nothing Then + Logger.Warn("SendInvMail - Envelope could not be loaded for Id [{0}]!", oId) + Throw New ArgumentNullException("EnvelopeData") + End If + Logger.Debug("SendInvMail - Loading Envelope Data..") + Dim oEnvelopeData = GetEnvelopeData(oId) + oEnvelope.Receivers = ReceiverModel.ListEnvelopeReceivers(oEnvelope.Id).ToList() + Logger.Debug("SendInvMail - Created Reveivers!") + If oEnvelopeData Is Nothing Then + Logger.Warn("SendInvMail - EnvelopeData could not be loaded for Id [{0}]!", oId) + Throw New ArgumentNullException("EnvelopeData") + End If + Logger.Info("SendInvMail - Sending InvitationMails for Envelope [{0}]", oId) + If ActionService.SendEnvelope(oEnvelope) = False Then + Throw New ArgumentNullException("EnvelopeData") + End If + Catch ex As Exception + Logger.Warn(ex, $"Unhandled exception while working envelope [{oId}]") + End Try + + oCurrent += 1 + Logger.Info("SendInvMail - Envelope finalized!") + + Next + + Logger.Debug("SendInvMail - Completed job {0} successfully!", JobId) + Catch ex As Exception + Logger.Warn("SendInvMail job failed!") + Logger.Error(ex) + Finally + Logger.Info("SendInvMail execution for [{0}] ended", JobId) + End Try + + Return Task.FromResult(True) + End Function + Private Sub InitializeModels(pState As State) + ConfigModel = New ConfigModel(pState) + EnvelopeModel = New EnvelopeModel(pState) + ReceiverModel = New ReceiverModel(pState) + End Sub + Private Sub InitializeServices(pState As State) + ActionService = New ActionService(pState) + End Sub + Private Function GetDatabase(pContext As IJobExecutionContext, pLogConfig As LogConfig) As MSSQLServer + Dim oConnectionString As String = pContext.MergedJobDataMap.Item(Constants.DATABASE) + Dim Database = New MSSQLServer(pLogConfig, MSSQLServer.DecryptConnectionString(oConnectionString)) + + Return Database + End Function + Private Function GetEnvelopeData(pEnvelopeId As Integer) As EnvelopeData + Dim oSql = $"SELECT T.GUID, T.ENVELOPE_UUID,T2.FILEPATH, T2.BYTE_DATA FROM [dbo].[TBSIG_ENVELOPE] T + JOIN TBSIG_ENVELOPE_DOCUMENT T2 ON T.GUID = T2.ENVELOPE_ID + WHERE T.GUID = {pEnvelopeId}" + Dim oTable As DataTable = Database.GetDatatable(oSql) + Dim oRow As DataRow = oTable.Rows.Cast(Of DataRow).SingleOrDefault() + If oRow Is Nothing Then + Return Nothing + End If + + Dim oData As New EnvelopeData With { + .EnvelopeId = pEnvelopeId, + .DocumentPath = oRow.ItemEx("FILEPATH", ""), + .EnvelopeUUID = oRow.ItemEx("ENVELOPE_UUID", "") + } + + Logger.Debug("Document path: [{0}]", oData.DocumentPath) + + Return oData + End Function + + + + + Private Function GetState() As State + Return New State With { + .LogConfig = LogConfig, + .Database = Database, + .UserId = 0, + .Config = Nothing, + .DbConfig = Nothing + } + End Function + End Class +End Namespace diff --git a/EnvelopeGenerator.Common/My Project/AssemblyInfo.vb b/EnvelopeGenerator.Common/My Project/AssemblyInfo.vb index 8b6b53ef..52938667 100644 --- a/EnvelopeGenerator.Common/My Project/AssemblyInfo.vb +++ b/EnvelopeGenerator.Common/My Project/AssemblyInfo.vb @@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices ' indem Sie "*" wie unten gezeigt eingeben: ' - - + + diff --git a/EnvelopeGenerator.Service/EnvelopeGenerator.Service.vbproj b/EnvelopeGenerator.Service/EnvelopeGenerator.Service.vbproj index fa08023f..ea20f2d7 100644 --- a/EnvelopeGenerator.Service/EnvelopeGenerator.Service.vbproj +++ b/EnvelopeGenerator.Service/EnvelopeGenerator.Service.vbproj @@ -260,6 +260,7 @@ + Service.vb @@ -288,7 +289,7 @@ Settings.settings True - + diff --git a/EnvelopeGenerator.Service/Scheduler_API_SendEnvelopeMails.vb b/EnvelopeGenerator.Service/Scheduler_API_SendEnvelopeMails.vb new file mode 100644 index 00000000..fac1cff2 --- /dev/null +++ b/EnvelopeGenerator.Service/Scheduler_API_SendEnvelopeMails.vb @@ -0,0 +1,72 @@ +Imports System.Collections.Specialized +Imports DigitalData.Modules.Base +Imports DigitalData.Modules.Database +Imports DigitalData.Modules.Logging +Imports EnvelopeGenerator.Common.Jobs +Imports EnvelopeGenerator.Common.Jobs.FinalizeDocument +Imports Quartz +Public Class Scheduler_API_SendEnvelopeMails + Inherits BaseClass + Private Scheduler As IScheduler + Private ReadOnly ConnectionString As String + + Private Const JobName = "SendInvitationMailsAPI" + Public Sub New(pLogConfig As LogConfig, pConnectionString As String) + MyBase.New(pLogConfig) + ConnectionString = pConnectionString + Dim oLogProvider = New LogProvider(Logger) + Logging.LogProvider.SetCurrentLogProvider(oLogProvider) + End Sub + + Public Async Function Start(pInterval As Integer) As Task + Try + Logger.Debug("Starting Scheduler SendMailsfromAPI..") + + Dim oProperties As New NameValueCollection() + + Scheduler = Await SchedulerBuilder.Create(oProperties). + UseDefaultThreadPool(Sub(x) x.MaxConcurrency = 5). + BuildScheduler() + Dim oJobKey = New JobKey(JobName) + Dim oJobData = New JobDataMap() From { + {Common.Constants.LOGCONFIG, LogConfig}, + {Common.Constants.DATABASE, ConnectionString} + } + + Logger.Debug("Initialized Job [{0}]", JobName) + + Dim oJob As IJobDetail = JobBuilder.Create(Of SendInvitationMailJob). + UsingJobData(oJobData). + WithIdentity(oJobKey). + Build() + + Dim oTrigger As ITrigger = TriggerBuilder.Create(). + ForJob(oJobKey). + WithIdentity($"{JobName}-trigger"). + WithSimpleSchedule(Sub(s) s. + RepeatForever(). + WithIntervalInMinutes(pInterval)). + StartNow(). + Build() + + Logger.Debug($"{JobName}-trigger initialized") + + Await Scheduler.ScheduleJob(oJob, oTrigger) + + Logger.Debug($"{JobName}-scheduled") + + Await Scheduler.Start() + + Logger.Info($"{JobName}-started") + Catch ex As Exception + Logger.Error(ex) + End Try + + End Function + + Public Async Function [Stop]() As Task + Logger.Info("Stopping scheduler SendMailsfromAPI..") + Await Scheduler.Shutdown() + Logger.Info("Scheduler SendMailsfromAPI stopped!") + End Function +End Class diff --git a/EnvelopeGenerator.Service/Scheduler.vb b/EnvelopeGenerator.Service/Scheduler_FinishEnvelope.vb similarity index 96% rename from EnvelopeGenerator.Service/Scheduler.vb rename to EnvelopeGenerator.Service/Scheduler_FinishEnvelope.vb index 1362e355..9bd10a06 100644 --- a/EnvelopeGenerator.Service/Scheduler.vb +++ b/EnvelopeGenerator.Service/Scheduler_FinishEnvelope.vb @@ -6,7 +6,7 @@ Imports EnvelopeGenerator.Common.Jobs Imports EnvelopeGenerator.Common.Jobs.FinalizeDocument Imports Quartz -Public Class Scheduler +Public Class Scheduler_FinishEnvelope Inherits BaseClass Private Scheduler As IScheduler @@ -28,7 +28,7 @@ Public Class Scheduler Public Async Function Start(pInterval As Integer) As Task Try - Logger.Debug("Starting Scheduler..") + Logger.Debug("Starting Scheduler Finish Envelope..") Dim oProperties As New NameValueCollection() diff --git a/EnvelopeGenerator.Service/Service.vb b/EnvelopeGenerator.Service/Service.vb index c0e1a88b..4431b09b 100644 --- a/EnvelopeGenerator.Service/Service.vb +++ b/EnvelopeGenerator.Service/Service.vb @@ -10,7 +10,8 @@ Public Class Service Private Config As Config Private Database As MSSQLServer Private TempFiles As TempFiles - Private Scheduler As Scheduler + Private Scheduler1 As Scheduler_FinishEnvelope + Private Scheduler2 As Scheduler_API_SendEnvelopeMails Protected Overrides Async Sub OnStart(ByVal args() As String) Try @@ -53,10 +54,13 @@ Public Class Service ' === Initialize Queue === - Logger.Debug("Inititalize Quartz") + Logger.Debug("Inititalizing scheduler(s) ...") - Scheduler = New Scheduler(LogConfig, Config.ConnectionString, oKey, Config.PDFBurnerParams) - Await Scheduler.Start(Config.IntervalInMin) + Scheduler1 = New Scheduler_FinishEnvelope(LogConfig, Config.ConnectionString, oKey, Config.PDFBurnerParams) + Await Scheduler1.Start(Config.IntervalInMin) + + Scheduler2 = New Scheduler_API_SendEnvelopeMails(LogConfig, Config.ConnectionString) + Await Scheduler2.Start(Config.IntervalInMin) Logger.Info("Started [{0}] !", ServiceName) @@ -67,8 +71,9 @@ Public Class Service Protected Overrides Async Sub OnStop() Try - Logger.Info("Stopping [{0}] !", ServiceName) - Await Scheduler.Stop() + Logger.Info("Stopping [{0}] ...", ServiceName) + Await Scheduler1.Stop() + Await Scheduler2.Stop() TempFiles.CleanUp() Logger.Info("Stopped [{0}] !", ServiceName) Catch ex As Exception