Overhaul email sending system

Add logging and saving templates to the database for historical
accuracy.
This commit is contained in:
Eli Ribble 2026-01-23 20:36:16 +00:00
parent 3fed489258
commit 196792810b
No known key found for this signature in database
44 changed files with 4846 additions and 2361 deletions

View file

@ -3,6 +3,8 @@ package background
import (
"context"
"sync"
"github.com/Gleipnir-Technology/nidus-sync/comms/email"
)
var waitGroup sync.WaitGroup
@ -10,9 +12,9 @@ var waitGroup sync.WaitGroup
func Start(ctx context.Context) {
newOAuthTokenChannel = make(chan struct{}, 10)
channelJobAudio = make(chan jobAudio, 100) // Buffered channel to prevent blocking
channelJobEmail = make(chan jobEmail, 100) // Buffered channel to prevent blocking
channelJobText = make(chan jobText, 100) // Buffered channel to prevent blocking
channelJobAudio = make(chan jobAudio, 100) // Buffered channel to prevent blocking
channelJobEmail = make(chan email.Job, 100) // Buffered channel to prevent blocking
channelJobText = make(chan jobText, 100) // Buffered channel to prevent blocking
waitGroup.Add(1)
go func() {

42
background/email.go Normal file
View file

@ -0,0 +1,42 @@
package background
import (
"context"
"github.com/Gleipnir-Technology/nidus-sync/comms/email"
"github.com/rs/zerolog/log"
)
var channelJobEmail chan email.Job
func ReportSubscriptionConfirmationEmail(destination, report_id string) {
enqueueJobEmail(email.NewJobReportSubscriptionConfirmation(
destination,
report_id,
))
}
func enqueueJobEmail(job email.Job) {
select {
case channelJobEmail <- job:
return
default:
log.Warn().Msg("email job channel is full, dropping job")
}
}
func startWorkerEmail(ctx context.Context, channel chan email.Job) {
go func() {
for {
select {
case <-ctx.Done():
log.Info().Msg("Email worker shutting down.")
return
case job := <-channel:
err := email.Handle(ctx, job)
if err != nil {
}
}
}
}()
}

View file

@ -11,17 +11,8 @@ import (
"github.com/rs/zerolog/log"
)
var channelJobEmail chan jobEmail
var channelJobText chan jobText
func ReportSubscriptionConfirmationEmail(destination string) {
enqueueJobEmail(jobEmail{
Destination: destination,
Source: config.ForwardEmailReportAddress,
Type: enums.CommsMessagetypeemailReportSubscriptionConfirmation,
})
}
func ReportSubscriptionConfirmationText(destination comms.E164, report_id string) {
enqueueJobText(jobText{
Destination: destination,
@ -31,12 +22,6 @@ func ReportSubscriptionConfirmationText(destination comms.E164, report_id string
})
}
type jobEmail struct {
Destination string
ReportID string
Source string
Type enums.CommsMessagetypeemail
}
type jobText struct {
Destination comms.E164
ReportID string
@ -44,15 +29,6 @@ type jobText struct {
Type enums.CommsMessagetypetext
}
func enqueueJobEmail(job jobEmail) {
select {
case channelJobEmail <- job:
log.Info().Str("destination", job.Destination).Msg("Enqueued email job")
default:
log.Warn().Msg("email job channel is full, dropping job")
}
}
func enqueueJobText(job jobText) {
select {
case channelJobText <- job:
@ -62,23 +38,6 @@ func enqueueJobText(job jobText) {
}
}
func startWorkerEmail(ctx context.Context, channel chan jobEmail) {
go func() {
for {
select {
case <-ctx.Done():
log.Info().Msg("Email worker shutting down.")
return
case job := <-channel:
err := jobProcessEmail(ctx, job)
if err != nil {
log.Error().Err(err).Str("dest", job.Destination).Str("type", string(job.Type)).Msg("Error processing email")
}
}
}
}()
}
func startWorkerText(ctx context.Context, channel chan jobText) {
go func() {
for {
@ -96,22 +55,6 @@ func startWorkerText(ctx context.Context, channel chan jobText) {
}()
}
func jobProcessEmail(ctx context.Context, job jobEmail) error {
switch job.Type {
case enums.CommsMessagetypeemailInitialContact:
return comms.SendEmailInitialContact(ctx, job.Destination)
default:
return errors.New("not implemented")
}
/*
case enums.CommsMessagetypeemailReportSubscriptionConfirmation:
case enums.CommsMessagetypeemailReportStatusScheduled:
case enums.CommsMessagetypeemailReportStatusComplete:
}
*/
}
func jobProcessText(job jobText) error {
var message string
switch job.Type {