120 lines
3.0 KiB
Go
120 lines
3.0 KiB
Go
package faqtable
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const FailureCodeRemoteInsertFailed = "remote_faq_table_insert_failed"
|
|
|
|
type PendingExport struct {
|
|
CaseID string
|
|
Row Row
|
|
Attempts int
|
|
}
|
|
|
|
type ExportRepository interface {
|
|
Claim(context.Context, int, int, time.Time) ([]PendingExport, error)
|
|
MarkExported(context.Context, string, string) error
|
|
MarkFailed(context.Context, string, string) error
|
|
}
|
|
|
|
type FieldsInserter interface {
|
|
InsertFields(context.Context, map[string]interface{}) (string, error)
|
|
}
|
|
|
|
type DispatcherOptions struct {
|
|
Batch int
|
|
MaxAttempts int
|
|
ClaimLease time.Duration
|
|
SendTimeout time.Duration
|
|
}
|
|
|
|
type DispatcherStats struct {
|
|
Claimed int
|
|
Exported int
|
|
Failed int
|
|
}
|
|
|
|
type Dispatcher struct {
|
|
repo ExportRepository
|
|
inserter FieldsInserter
|
|
batch int
|
|
maxAttempts int
|
|
claimLease time.Duration
|
|
sendTimeout time.Duration
|
|
}
|
|
|
|
func NewDispatcher(repo ExportRepository, inserter FieldsInserter, opts *DispatcherOptions) (*Dispatcher, error) {
|
|
if isNil(repo) || isNil(inserter) {
|
|
return nil, errors.New("faqtable dispatcher dependencies are required")
|
|
}
|
|
d := &Dispatcher{repo: repo, inserter: inserter, batch: 10, maxAttempts: 5, claimLease: 5 * time.Minute, sendTimeout: 10 * time.Second}
|
|
if opts != nil {
|
|
if opts.Batch < 0 || opts.MaxAttempts < 0 || opts.ClaimLease < 0 || opts.SendTimeout < 0 {
|
|
return nil, errors.New("faqtable dispatcher options are invalid")
|
|
}
|
|
if opts.Batch > 0 {
|
|
d.batch = opts.Batch
|
|
}
|
|
if opts.MaxAttempts > 0 {
|
|
d.maxAttempts = opts.MaxAttempts
|
|
}
|
|
if opts.ClaimLease > 0 {
|
|
d.claimLease = opts.ClaimLease
|
|
}
|
|
if opts.SendTimeout > 0 {
|
|
d.sendTimeout = opts.SendTimeout
|
|
}
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
func (d *Dispatcher) RunOnce(ctx context.Context) (DispatcherStats, error) {
|
|
var stats DispatcherStats
|
|
if err := ctx.Err(); err != nil {
|
|
return stats, err
|
|
}
|
|
items, err := d.repo.Claim(ctx, d.batch, d.maxAttempts, time.Now().Add(-d.claimLease))
|
|
if err != nil {
|
|
return stats, errors.New("faqtable dispatcher claim failed")
|
|
}
|
|
stats.Claimed = len(items)
|
|
for _, item := range items {
|
|
if err := ctx.Err(); err != nil {
|
|
return stats, err
|
|
}
|
|
itemCtx, cancel := context.WithTimeout(ctx, d.sendTimeout)
|
|
recordID, insertErr := d.inserter.InsertFields(itemCtx, item.Row.ToFields())
|
|
cancel()
|
|
if insertErr != nil || strings.TrimSpace(recordID) == "" {
|
|
if err := d.repo.MarkFailed(ctx, item.CaseID, FailureCodeRemoteInsertFailed); err != nil {
|
|
return stats, errors.New("faqtable dispatcher mark failed failed")
|
|
}
|
|
stats.Failed++
|
|
continue
|
|
}
|
|
if err := d.repo.MarkExported(ctx, item.CaseID, recordID); err != nil {
|
|
return stats, errors.New("faqtable dispatcher mark exported failed")
|
|
}
|
|
stats.Exported++
|
|
}
|
|
return stats, nil
|
|
}
|
|
|
|
func isNil(value interface{}) bool {
|
|
if value == nil {
|
|
return true
|
|
}
|
|
v := reflect.ValueOf(value)
|
|
switch v.Kind() {
|
|
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
|
|
return v.IsNil()
|
|
default:
|
|
return false
|
|
}
|
|
}
|