2018-12-19 07:33:13 +01:00
|
|
|
package manager
|
2018-10-25 15:51:47 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-03-08 07:57:41 +01:00
|
|
|
"errors"
|
2018-10-25 15:51:47 +02:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
2018-11-28 08:59:57 +01:00
|
|
|
"strings"
|
2018-10-31 13:54:21 +01:00
|
|
|
"sync"
|
2018-10-25 15:51:47 +02:00
|
|
|
"time"
|
|
|
|
|
2020-03-07 19:47:54 +01:00
|
|
|
"github.com/knadh/listmonk/internal/messenger"
|
2018-10-25 15:51:47 +02:00
|
|
|
"github.com/knadh/listmonk/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
batchSize = 10000
|
|
|
|
|
|
|
|
// BaseTPL is the name of the base template.
|
|
|
|
BaseTPL = "base"
|
|
|
|
|
|
|
|
// ContentTpl is the name of the compiled message.
|
|
|
|
ContentTpl = "content"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DataSource represents a data backend, such as a database,
|
|
|
|
// that provides subscriber and campaign records.
|
|
|
|
type DataSource interface {
|
|
|
|
NextCampaigns(excludeIDs []int64) ([]*models.Campaign, error)
|
2020-03-08 06:37:24 +01:00
|
|
|
NextSubscribers(campID, limit int) ([]models.Subscriber, error)
|
2018-10-25 15:51:47 +02:00
|
|
|
GetCampaign(campID int) (*models.Campaign, error)
|
2018-11-26 12:10:51 +01:00
|
|
|
UpdateCampaignStatus(campID int, status string) error
|
2018-10-31 13:54:21 +01:00
|
|
|
CreateLink(url string) (string, error)
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
2018-12-19 07:33:13 +01:00
|
|
|
// Manager handles the scheduling, processing, and queuing of campaigns
|
2018-10-25 15:51:47 +02:00
|
|
|
// and message pushes.
|
2018-12-19 07:33:13 +01:00
|
|
|
type Manager struct {
|
2018-10-25 15:51:47 +02:00
|
|
|
cfg Config
|
|
|
|
src DataSource
|
|
|
|
messengers map[string]messenger.Messenger
|
2018-11-28 08:59:57 +01:00
|
|
|
notifCB models.AdminNotifCallback
|
2018-10-25 15:51:47 +02:00
|
|
|
logger *log.Logger
|
|
|
|
|
|
|
|
// Campaigns that are currently running.
|
2020-03-08 07:57:41 +01:00
|
|
|
camps map[int]*models.Campaign
|
|
|
|
campsMutex sync.RWMutex
|
2018-10-25 15:51:47 +02:00
|
|
|
|
2018-10-31 13:54:21 +01:00
|
|
|
// Links generated using Track() are cached here so as to not query
|
|
|
|
// the database for the link UUID for every message sent. This has to
|
|
|
|
// be locked as it may be used externally when previewing campaigns.
|
|
|
|
links map[string]string
|
|
|
|
linksMutex sync.RWMutex
|
|
|
|
|
2020-03-08 07:57:41 +01:00
|
|
|
subFetchQueue chan *models.Campaign
|
|
|
|
campMsgQueue chan CampaignMessage
|
|
|
|
campMsgErrorQueue chan msgError
|
|
|
|
campMsgErrorCounts map[int]int
|
|
|
|
msgQueue chan Message
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
2020-03-08 07:57:41 +01:00
|
|
|
// CampaignMessage represents an instance of campaign message to be pushed out,
|
|
|
|
// specific to a subscriber, via the campaign's messenger.
|
|
|
|
type CampaignMessage struct {
|
2019-12-06 18:10:30 +01:00
|
|
|
Campaign *models.Campaign
|
2020-03-08 06:37:24 +01:00
|
|
|
Subscriber models.Subscriber
|
2019-12-06 18:10:30 +01:00
|
|
|
|
2020-02-09 14:45:14 +01:00
|
|
|
from string
|
|
|
|
to string
|
2020-07-05 15:09:24 +02:00
|
|
|
subject string
|
2020-03-08 10:32:20 +01:00
|
|
|
body []byte
|
2020-02-09 14:45:14 +01:00
|
|
|
unsubURL string
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
2020-03-08 07:57:41 +01:00
|
|
|
// Message represents a generic message to be pushed to a messenger.
|
|
|
|
type Message struct {
|
|
|
|
From string
|
|
|
|
To []string
|
|
|
|
Subject string
|
|
|
|
Body []byte
|
|
|
|
Messenger string
|
|
|
|
}
|
|
|
|
|
2018-12-19 07:33:13 +01:00
|
|
|
// Config has parameters for configuring the manager.
|
2018-10-25 15:51:47 +02:00
|
|
|
type Config struct {
|
|
|
|
Concurrency int
|
2020-04-02 14:52:27 +02:00
|
|
|
MessageRate int
|
2018-11-26 12:10:51 +01:00
|
|
|
MaxSendErrors int
|
|
|
|
RequeueOnError bool
|
2018-11-28 08:59:57 +01:00
|
|
|
FromEmail string
|
2018-10-31 13:54:21 +01:00
|
|
|
LinkTrackURL string
|
2019-12-06 18:10:30 +01:00
|
|
|
UnsubURL string
|
2019-12-01 13:18:36 +01:00
|
|
|
OptinURL string
|
2020-04-26 12:21:43 +02:00
|
|
|
MessageURL string
|
2018-11-02 08:50:32 +01:00
|
|
|
ViewTrackURL string
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
2018-11-26 12:10:51 +01:00
|
|
|
type msgError struct {
|
|
|
|
camp *models.Campaign
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:51:47 +02:00
|
|
|
// New returns a new instance of Mailer.
|
2018-12-19 07:33:13 +01:00
|
|
|
func New(cfg Config, src DataSource, notifCB models.AdminNotifCallback, l *log.Logger) *Manager {
|
|
|
|
return &Manager{
|
2020-03-08 07:57:41 +01:00
|
|
|
cfg: cfg,
|
|
|
|
src: src,
|
|
|
|
notifCB: notifCB,
|
|
|
|
logger: l,
|
|
|
|
messengers: make(map[string]messenger.Messenger),
|
|
|
|
camps: make(map[int]*models.Campaign),
|
|
|
|
links: make(map[string]string),
|
|
|
|
subFetchQueue: make(chan *models.Campaign, cfg.Concurrency),
|
|
|
|
campMsgQueue: make(chan CampaignMessage, cfg.Concurrency*2),
|
|
|
|
msgQueue: make(chan Message, cfg.Concurrency),
|
|
|
|
campMsgErrorQueue: make(chan msgError, cfg.MaxSendErrors),
|
|
|
|
campMsgErrorCounts: make(map[int]int),
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 07:57:41 +01:00
|
|
|
// NewCampaignMessage creates and returns a CampaignMessage that is made available
|
|
|
|
// to message templates while they're compiled. It represents a message from
|
|
|
|
// a campaign that's bound to a single Subscriber.
|
|
|
|
func (m *Manager) NewCampaignMessage(c *models.Campaign, s models.Subscriber) CampaignMessage {
|
|
|
|
return CampaignMessage{
|
2019-12-06 18:10:30 +01:00
|
|
|
Campaign: c,
|
|
|
|
Subscriber: s,
|
|
|
|
|
2020-07-05 15:09:24 +02:00
|
|
|
subject: c.Subject,
|
2020-02-09 14:45:14 +01:00
|
|
|
from: c.FromEmail,
|
|
|
|
to: s.Email,
|
|
|
|
unsubURL: fmt.Sprintf(m.cfg.UnsubURL, c.UUID, s.UUID),
|
2018-10-31 13:54:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-19 07:33:13 +01:00
|
|
|
// AddMessenger adds a Messenger messaging backend to the manager.
|
|
|
|
func (m *Manager) AddMessenger(msg messenger.Messenger) error {
|
2018-10-25 15:51:47 +02:00
|
|
|
id := msg.Name()
|
2018-12-19 07:33:13 +01:00
|
|
|
if _, ok := m.messengers[id]; ok {
|
2018-10-25 15:51:47 +02:00
|
|
|
return fmt.Errorf("messenger '%s' is already loaded", id)
|
|
|
|
}
|
2018-12-19 07:33:13 +01:00
|
|
|
m.messengers[id] = msg
|
2018-10-25 15:51:47 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-08 07:57:41 +01:00
|
|
|
// PushMessage pushes a Message to be sent out by the workers.
|
|
|
|
func (m *Manager) PushMessage(msg Message) error {
|
|
|
|
select {
|
|
|
|
case m.msgQueue <- msg:
|
|
|
|
case <-time.After(time.Second * 3):
|
|
|
|
m.logger.Println("message push timed out: %'s'", msg.Subject)
|
|
|
|
return errors.New("message push timed out")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:51:47 +02:00
|
|
|
// GetMessengerNames returns the list of registered messengers.
|
2018-12-19 07:33:13 +01:00
|
|
|
func (m *Manager) GetMessengerNames() []string {
|
2019-10-25 07:41:47 +02:00
|
|
|
names := make([]string, 0, len(m.messengers))
|
2018-12-19 07:33:13 +01:00
|
|
|
for n := range m.messengers {
|
2018-10-25 15:51:47 +02:00
|
|
|
names = append(names, n)
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasMessenger checks if a given messenger is registered.
|
2018-12-19 07:33:13 +01:00
|
|
|
func (m *Manager) HasMessenger(id string) bool {
|
|
|
|
_, ok := m.messengers[id]
|
2018-10-25 15:51:47 +02:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run is a blocking function (and hence should be invoked as a goroutine)
|
|
|
|
// that scans the source db at regular intervals for pending campaigns,
|
|
|
|
// and queues them for processing. The process queue fetches batches of
|
|
|
|
// subscribers and pushes messages to them for each queued campaign
|
|
|
|
// until all subscribers are exhausted, at which point, a campaign is marked
|
|
|
|
// as "finished".
|
2018-12-19 07:33:13 +01:00
|
|
|
func (m *Manager) Run(tick time.Duration) {
|
2018-11-26 12:10:51 +01:00
|
|
|
go func() {
|
|
|
|
t := time.NewTicker(tick)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// Periodically scan the data source for campaigns to process.
|
|
|
|
case <-t.C:
|
2018-12-19 07:33:13 +01:00
|
|
|
campaigns, err := m.src.NextCampaigns(m.getPendingCampaignIDs())
|
2018-11-26 12:10:51 +01:00
|
|
|
if err != nil {
|
2018-12-19 07:33:13 +01:00
|
|
|
m.logger.Printf("error fetching campaigns: %v", err)
|
2018-11-26 12:10:51 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range campaigns {
|
2018-12-19 07:33:13 +01:00
|
|
|
if err := m.addCampaign(c); err != nil {
|
|
|
|
m.logger.Printf("error processing campaign (%s): %v", c.Name, err)
|
2018-11-26 12:10:51 +01:00
|
|
|
continue
|
|
|
|
}
|
2018-12-19 07:33:13 +01:00
|
|
|
m.logger.Printf("start processing campaign (%s)", c.Name)
|
2018-11-26 12:10:51 +01:00
|
|
|
|
|
|
|
// If subscriber processing is busy, move on. Blocking and waiting
|
|
|
|
// can end up in a race condition where the waiting campaign's
|
|
|
|
// state in the data source has changed.
|
|
|
|
select {
|
2018-12-19 07:33:13 +01:00
|
|
|
case m.subFetchQueue <- c:
|
2018-11-26 12:10:51 +01:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2018-10-25 15:51:47 +02:00
|
|
|
|
2018-11-26 12:10:51 +01:00
|
|
|
// Aggregate errors from sending messages to check against the error threshold
|
|
|
|
// after which a campaign is paused.
|
2020-03-08 07:57:41 +01:00
|
|
|
case e := <-m.campMsgErrorQueue:
|
2018-12-19 07:33:13 +01:00
|
|
|
if m.cfg.MaxSendErrors < 1 {
|
2018-10-25 15:51:47 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-26 12:10:51 +01:00
|
|
|
// If the error threshold is met, pause the campaign.
|
2020-03-08 07:57:41 +01:00
|
|
|
m.campMsgErrorCounts[e.camp.ID]++
|
|
|
|
if m.campMsgErrorCounts[e.camp.ID] >= m.cfg.MaxSendErrors {
|
2018-12-19 07:33:13 +01:00
|
|
|
m.logger.Printf("error counted exceeded %d. pausing campaign %s",
|
|
|
|
m.cfg.MaxSendErrors, e.camp.Name)
|
2018-10-25 15:51:47 +02:00
|
|
|
|
2018-12-19 07:33:13 +01:00
|
|
|
if m.isCampaignProcessing(e.camp.ID) {
|
|
|
|
m.exhaustCampaign(e.camp, models.CampaignStatusPaused)
|
2018-11-26 12:10:51 +01:00
|
|
|
}
|
2020-03-08 07:57:41 +01:00
|
|
|
delete(m.campMsgErrorCounts, e.camp.ID)
|
2018-11-28 08:59:57 +01:00
|
|
|
|
|
|
|
// Notify admins.
|
2020-03-07 20:05:34 +01:00
|
|
|
m.sendNotif(e.camp, models.CampaignStatusPaused, "Too many errors")
|
2018-11-26 12:10:51 +01:00
|
|
|
}
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
2018-11-26 12:10:51 +01:00
|
|
|
}
|
|
|
|
}()
|
2018-10-25 15:51:47 +02:00
|
|
|
|
2018-11-26 12:10:51 +01:00
|
|
|
// Fetch the next set of subscribers for a campaign and process them.
|
2018-12-19 07:33:13 +01:00
|
|
|
for c := range m.subFetchQueue {
|
|
|
|
has, err := m.nextSubscribers(c, batchSize)
|
2018-11-26 12:10:51 +01:00
|
|
|
if err != nil {
|
2018-12-19 07:33:13 +01:00
|
|
|
m.logger.Printf("error processing campaign batch (%s): %v", c.Name, err)
|
2018-11-26 12:10:51 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if has {
|
|
|
|
// There are more subscribers to fetch.
|
2018-12-19 07:33:13 +01:00
|
|
|
m.subFetchQueue <- c
|
|
|
|
} else if m.isCampaignProcessing(c.ID) {
|
2018-11-26 12:10:51 +01:00
|
|
|
// There are no more subscribers. Either the campaign status
|
|
|
|
// has changed or all subscribers have been processed.
|
2018-12-19 07:33:13 +01:00
|
|
|
newC, err := m.exhaustCampaign(c, "")
|
2018-11-28 08:59:57 +01:00
|
|
|
if err != nil {
|
2018-12-19 07:33:13 +01:00
|
|
|
m.logger.Printf("error exhausting campaign (%s): %v", c.Name, err)
|
2018-11-28 08:59:57 +01:00
|
|
|
continue
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
2018-12-19 07:33:13 +01:00
|
|
|
m.sendNotif(newC, newC.Status, "")
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SpawnWorkers spawns workers goroutines that push out messages.
|
2018-12-19 07:33:13 +01:00
|
|
|
func (m *Manager) SpawnWorkers() {
|
|
|
|
for i := 0; i < m.cfg.Concurrency; i++ {
|
2018-11-26 12:10:51 +01:00
|
|
|
go func() {
|
2020-04-02 14:52:27 +02:00
|
|
|
// Counter to keep track of the message / sec rate limit.
|
|
|
|
numMsg := 0
|
|
|
|
|
2020-03-08 07:57:41 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// Campaign message.
|
|
|
|
case msg := <-m.campMsgQueue:
|
2020-04-02 14:52:27 +02:00
|
|
|
// Pause on hitting the message rate.
|
|
|
|
if numMsg >= m.cfg.MessageRate {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
numMsg = 0
|
|
|
|
}
|
|
|
|
numMsg++
|
|
|
|
|
2020-03-08 07:57:41 +01:00
|
|
|
err := m.messengers[msg.Campaign.MessengerID].Push(
|
2020-07-05 15:09:24 +02:00
|
|
|
msg.from, []string{msg.to}, msg.subject, msg.body, nil)
|
2020-03-08 07:57:41 +01:00
|
|
|
if err != nil {
|
|
|
|
m.logger.Printf("error sending message in campaign %s: %v", msg.Campaign.Name, err)
|
2018-11-26 12:10:51 +01:00
|
|
|
|
2020-03-08 07:57:41 +01:00
|
|
|
select {
|
|
|
|
case m.campMsgErrorQueue <- msgError{camp: msg.Campaign, err: err}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arbitrary message.
|
|
|
|
case msg := <-m.msgQueue:
|
|
|
|
err := m.messengers[msg.Messenger].Push(
|
|
|
|
msg.From, msg.To, msg.Subject, msg.Body, nil)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Printf("error sending message '%s': %v", msg.Subject, err)
|
2018-11-06 12:03:46 +01:00
|
|
|
}
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-26 12:10:51 +01:00
|
|
|
}()
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 20:05:34 +01:00
|
|
|
// TemplateFuncs returns the template functions to be applied into
|
|
|
|
// compiled campaign templates.
|
|
|
|
func (m *Manager) TemplateFuncs(c *models.Campaign) template.FuncMap {
|
|
|
|
return template.FuncMap{
|
2020-03-08 07:57:41 +01:00
|
|
|
"TrackLink": func(url string, msg *CampaignMessage) string {
|
2020-03-07 20:05:34 +01:00
|
|
|
return m.trackLink(url, msg.Campaign.UUID, msg.Subscriber.UUID)
|
|
|
|
},
|
2020-03-08 07:57:41 +01:00
|
|
|
"TrackView": func(msg *CampaignMessage) template.HTML {
|
2020-03-07 20:05:34 +01:00
|
|
|
return template.HTML(fmt.Sprintf(`<img src="%s" alt="" />`,
|
|
|
|
fmt.Sprintf(m.cfg.ViewTrackURL, msg.Campaign.UUID, msg.Subscriber.UUID)))
|
|
|
|
},
|
2020-03-08 07:57:41 +01:00
|
|
|
"UnsubscribeURL": func(msg *CampaignMessage) string {
|
2020-03-07 20:05:34 +01:00
|
|
|
return msg.unsubURL
|
|
|
|
},
|
2020-03-08 07:57:41 +01:00
|
|
|
"OptinURL": func(msg *CampaignMessage) string {
|
2020-03-07 20:05:34 +01:00
|
|
|
// Add list IDs.
|
|
|
|
// TODO: Show private lists list on optin e-mail
|
|
|
|
return fmt.Sprintf(m.cfg.OptinURL, msg.Subscriber.UUID, "")
|
|
|
|
},
|
2020-04-26 12:21:43 +02:00
|
|
|
"MessageURL": func(msg *CampaignMessage) string {
|
|
|
|
return fmt.Sprintf(m.cfg.MessageURL, c.UUID, msg.Subscriber.UUID)
|
|
|
|
},
|
2020-03-07 20:05:34 +01:00
|
|
|
"Date": func(layout string) string {
|
|
|
|
if layout == "" {
|
|
|
|
layout = time.ANSIC
|
|
|
|
}
|
|
|
|
return time.Now().Format(layout)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:51:47 +02:00
|
|
|
// addCampaign adds a campaign to the process queue.
|
2018-12-19 07:33:13 +01:00
|
|
|
func (m *Manager) addCampaign(c *models.Campaign) error {
|
2018-10-25 15:51:47 +02:00
|
|
|
// Validate messenger.
|
2018-12-19 07:33:13 +01:00
|
|
|
if _, ok := m.messengers[c.MessengerID]; !ok {
|
|
|
|
m.src.UpdateCampaignStatus(c.ID, models.CampaignStatusCancelled)
|
2018-10-25 15:51:47 +02:00
|
|
|
return fmt.Errorf("unknown messenger %s on campaign %s", c.MessengerID, c.Name)
|
|
|
|
}
|
|
|
|
|
2018-10-31 13:54:21 +01:00
|
|
|
// Load the template.
|
2018-12-19 07:33:13 +01:00
|
|
|
if err := c.CompileTemplate(m.TemplateFuncs(c)); err != nil {
|
2018-10-31 13:54:21 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-25 15:51:47 +02:00
|
|
|
// Add the campaign to the active map.
|
2020-03-08 07:57:41 +01:00
|
|
|
m.campsMutex.Lock()
|
2018-12-19 07:33:13 +01:00
|
|
|
m.camps[c.ID] = c
|
2020-03-08 07:57:41 +01:00
|
|
|
m.campsMutex.Unlock()
|
2018-10-25 15:51:47 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getPendingCampaignIDs returns the IDs of campaigns currently being processed.
|
2018-12-19 07:33:13 +01:00
|
|
|
func (m *Manager) getPendingCampaignIDs() []int64 {
|
2018-10-25 15:51:47 +02:00
|
|
|
// Needs to return an empty slice in case there are no campaigns.
|
2020-03-08 07:57:41 +01:00
|
|
|
m.campsMutex.RLock()
|
|
|
|
ids := make([]int64, 0, len(m.camps))
|
2018-12-19 07:33:13 +01:00
|
|
|
for _, c := range m.camps {
|
2018-10-25 15:51:47 +02:00
|
|
|
ids = append(ids, int64(c.ID))
|
|
|
|
}
|
2020-03-08 07:57:41 +01:00
|
|
|
m.campsMutex.RUnlock()
|
2018-10-25 15:51:47 +02:00
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
|
|
|
// nextSubscribers processes the next batch of subscribers in a given campaign.
|
|
|
|
// If returns a bool indicating whether there any subscribers were processed
|
|
|
|
// in the current batch or not. This can happen when all the subscribers
|
|
|
|
// have been processed, or if a campaign has been paused or cancelled abruptly.
|
2018-12-19 07:33:13 +01:00
|
|
|
func (m *Manager) nextSubscribers(c *models.Campaign, batchSize int) (bool, error) {
|
2018-10-25 15:51:47 +02:00
|
|
|
// Fetch a batch of subscribers.
|
2018-12-19 07:33:13 +01:00
|
|
|
subs, err := m.src.NextSubscribers(c.ID, batchSize)
|
2018-10-25 15:51:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("error fetching campaign subscribers (%s): %v", c.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// There are no subscribers.
|
|
|
|
if len(subs) == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push messages.
|
|
|
|
for _, s := range subs {
|
2020-03-08 07:57:41 +01:00
|
|
|
msg := m.NewCampaignMessage(c, s)
|
2018-12-19 07:33:13 +01:00
|
|
|
if err := msg.Render(); err != nil {
|
|
|
|
m.logger.Printf("error rendering message (%s) (%s): %v", c.Name, s.Email, err)
|
2018-10-25 15:51:47 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-11-26 12:10:51 +01:00
|
|
|
// Push the message to the queue while blocking and waiting until
|
|
|
|
// the queue is drained.
|
2020-03-08 07:57:41 +01:00
|
|
|
m.campMsgQueue <- msg
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2018-11-26 12:10:51 +01:00
|
|
|
// isCampaignProcessing checks if the campaign is bing processed.
|
2018-12-19 07:33:13 +01:00
|
|
|
func (m *Manager) isCampaignProcessing(id int) bool {
|
2020-03-08 07:57:41 +01:00
|
|
|
m.campsMutex.RLock()
|
2018-12-19 07:33:13 +01:00
|
|
|
_, ok := m.camps[id]
|
2020-03-08 07:57:41 +01:00
|
|
|
m.campsMutex.RUnlock()
|
2018-11-26 12:10:51 +01:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2018-12-19 07:33:13 +01:00
|
|
|
func (m *Manager) exhaustCampaign(c *models.Campaign, status string) (*models.Campaign, error) {
|
2020-03-08 07:57:41 +01:00
|
|
|
m.campsMutex.Lock()
|
2018-12-19 07:33:13 +01:00
|
|
|
delete(m.camps, c.ID)
|
2020-03-08 07:57:41 +01:00
|
|
|
m.campsMutex.Unlock()
|
2018-11-26 12:10:51 +01:00
|
|
|
|
|
|
|
// A status has been passed. Change the campaign's status
|
|
|
|
// without further checks.
|
|
|
|
if status != "" {
|
2018-12-19 07:33:13 +01:00
|
|
|
if err := m.src.UpdateCampaignStatus(c.ID, status); err != nil {
|
|
|
|
m.logger.Printf("error updating campaign (%s) status to %s: %v", c.Name, status, err)
|
2018-11-26 12:10:51 +01:00
|
|
|
} else {
|
2018-12-19 07:33:13 +01:00
|
|
|
m.logger.Printf("set campaign (%s) to %s", c.Name, status)
|
2018-11-26 12:10:51 +01:00
|
|
|
}
|
2018-11-28 08:59:57 +01:00
|
|
|
return c, nil
|
2018-11-26 12:10:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the up-to-date campaign status from the source.
|
2018-12-19 07:33:13 +01:00
|
|
|
cm, err := m.src.GetCampaign(c.ID)
|
2018-10-25 15:51:47 +02:00
|
|
|
if err != nil {
|
2018-11-28 08:59:57 +01:00
|
|
|
return nil, err
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a running campaign has exhausted subscribers, it's finished.
|
|
|
|
if cm.Status == models.CampaignStatusRunning {
|
2018-11-28 08:59:57 +01:00
|
|
|
cm.Status = models.CampaignStatusFinished
|
2018-12-19 07:33:13 +01:00
|
|
|
if err := m.src.UpdateCampaignStatus(c.ID, models.CampaignStatusFinished); err != nil {
|
|
|
|
m.logger.Printf("error finishing campaign (%s): %v", c.Name, err)
|
2018-10-25 15:51:47 +02:00
|
|
|
} else {
|
2018-12-19 07:33:13 +01:00
|
|
|
m.logger.Printf("campaign (%s) finished", c.Name)
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-12-19 07:33:13 +01:00
|
|
|
m.logger.Printf("stop processing campaign (%s)", c.Name)
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
2018-11-28 08:59:57 +01:00
|
|
|
return cm, nil
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
|
|
|
|
2018-10-31 13:54:21 +01:00
|
|
|
// trackLink register a URL and return its UUID to be used in message templates
|
|
|
|
// for tracking links.
|
2018-12-19 07:33:13 +01:00
|
|
|
func (m *Manager) trackLink(url, campUUID, subUUID string) string {
|
|
|
|
m.linksMutex.RLock()
|
|
|
|
if uu, ok := m.links[url]; ok {
|
|
|
|
m.linksMutex.RUnlock()
|
|
|
|
return fmt.Sprintf(m.cfg.LinkTrackURL, uu, campUUID, subUUID)
|
2018-10-31 13:54:21 +01:00
|
|
|
}
|
2018-12-19 07:33:13 +01:00
|
|
|
m.linksMutex.RUnlock()
|
2018-10-31 13:54:21 +01:00
|
|
|
|
|
|
|
// Register link.
|
2018-12-19 07:33:13 +01:00
|
|
|
uu, err := m.src.CreateLink(url)
|
2018-10-31 13:54:21 +01:00
|
|
|
if err != nil {
|
2018-12-19 07:33:13 +01:00
|
|
|
m.logger.Printf("error registering tracking for link '%s': %v", url, err)
|
2018-10-31 13:54:21 +01:00
|
|
|
|
|
|
|
// If the registration fails, fail over to the original URL.
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2018-12-19 07:33:13 +01:00
|
|
|
m.linksMutex.Lock()
|
|
|
|
m.links[url] = uu
|
|
|
|
m.linksMutex.Unlock()
|
2018-10-25 15:51:47 +02:00
|
|
|
|
2018-12-19 07:33:13 +01:00
|
|
|
return fmt.Sprintf(m.cfg.LinkTrackURL, uu, campUUID, subUUID)
|
2018-10-25 15:51:47 +02:00
|
|
|
}
|
2018-11-01 17:30:07 +01:00
|
|
|
|
2018-11-28 08:59:57 +01:00
|
|
|
// sendNotif sends a notification to registered admin e-mails.
|
2018-12-19 07:33:13 +01:00
|
|
|
func (m *Manager) sendNotif(c *models.Campaign, status, reason string) error {
|
2018-11-28 08:59:57 +01:00
|
|
|
var (
|
|
|
|
subject = fmt.Sprintf("%s: %s", strings.Title(status), c.Name)
|
|
|
|
data = map[string]interface{}{
|
|
|
|
"ID": c.ID,
|
|
|
|
"Name": c.Name,
|
|
|
|
"Status": status,
|
|
|
|
"Sent": c.Sent,
|
|
|
|
"ToSend": c.ToSend,
|
|
|
|
"Reason": reason,
|
|
|
|
}
|
|
|
|
)
|
2018-12-19 07:33:13 +01:00
|
|
|
return m.notifCB(subject, data)
|
2018-11-28 08:59:57 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 20:05:34 +01:00
|
|
|
// Render takes a Message, executes its pre-compiled Campaign.Tpl
|
|
|
|
// and applies the resultant bytes to Message.body to be used in messages.
|
2020-03-08 07:57:41 +01:00
|
|
|
func (m *CampaignMessage) Render() error {
|
2020-03-07 20:05:34 +01:00
|
|
|
out := bytes.Buffer{}
|
2020-07-05 15:09:24 +02:00
|
|
|
|
|
|
|
// Render the subject if it's a template.
|
|
|
|
if m.Campaign.SubjectTpl != nil {
|
|
|
|
if err := m.Campaign.SubjectTpl.ExecuteTemplate(&out, models.ContentTpl, m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.subject = out.String()
|
|
|
|
out.Reset()
|
|
|
|
}
|
|
|
|
|
2020-03-07 20:05:34 +01:00
|
|
|
if err := m.Campaign.Tpl.ExecuteTemplate(&out, models.BaseTpl, m); err != nil {
|
|
|
|
return err
|
2018-11-01 17:30:07 +01:00
|
|
|
}
|
2020-03-08 10:32:20 +01:00
|
|
|
m.body = out.Bytes()
|
2020-03-07 20:05:34 +01:00
|
|
|
return nil
|
2018-11-01 17:30:07 +01:00
|
|
|
}
|
2020-03-08 10:32:20 +01:00
|
|
|
|
2020-07-05 15:09:24 +02:00
|
|
|
// Subject returns a copy of the message subject
|
|
|
|
func (m *CampaignMessage) Subject() string {
|
|
|
|
return m.subject
|
|
|
|
}
|
|
|
|
|
2020-03-08 10:32:20 +01:00
|
|
|
// Body returns a copy of the message body.
|
|
|
|
func (m *CampaignMessage) Body() []byte {
|
|
|
|
out := make([]byte, len(m.body))
|
|
|
|
copy(out, m.body)
|
|
|
|
return out
|
|
|
|
}
|