v0.1.27 v0.1.27
Vernon Keenan 2021-01-21 09:28:52 -08:00
parent a12863e619
commit ed3214ce43
7 changed files with 44 additions and 24 deletions

View File

@ -1,12 +1,13 @@
package rules package rules
import ( import (
"time"
"go.temporal.io/sdk/temporal" "go.temporal.io/sdk/temporal"
temporal_workflow "go.temporal.io/sdk/workflow" temporal_workflow "go.temporal.io/sdk/workflow"
) )
// NewLeadWorkflowID is the text identifier for new-lead-workflow
const NewLeadWorkflowID = "new-lead-workflow"
// NewLeadWorkflowWrapper wraps a Lead with auth and saga info // NewLeadWorkflowWrapper wraps a Lead with auth and saga info
type NewLeadWorkflowWrapper struct { type NewLeadWorkflowWrapper struct {
APIKey string APIKey string
@ -24,29 +25,41 @@ type NewLeadWorkflowWrapper struct {
// NewLeadWorkflow is a Temporal workflow // NewLeadWorkflow is a Temporal workflow
func NewLeadWorkflow(ctx temporal_workflow.Context, w NewLeadWorkflowWrapper) error { //nolint:gocritic // don't care func NewLeadWorkflow(ctx temporal_workflow.Context, w NewLeadWorkflowWrapper) error { //nolint:gocritic // don't care
sugar.Info("rules.NewLeadWorkflow: 📥") sugar.Info("rules.NewLeadWorkflow: 📥")
sugar.Debugf("rules.NewLeadWorkflow: wrapper: %v", w) sugar.Debugf("rules.NewLeadWorkflow: ctx: %v", ctx)
transferDetails := NewLeadActivityWrapper(w) sugar.Debugf("rules.NewLeadWorkflow: w: %v", w)
activityDetails := NewLeadActivityWrapper(w)
retryPolicy := &temporal.RetryPolicy{ retryPolicy := &temporal.RetryPolicy{
InitialInterval: time.Second, InitialInterval: initialInterval,
MaximumInterval: time.Minute, BackoffCoefficient: backoffCoefficient,
MaximumAttempts: maxAttempts, MaximumInterval: maximumInterval,
MaximumAttempts: maximumAttempts,
} }
options := temporal_workflow.ActivityOptions{ options := temporal_workflow.ActivityOptions{
// Timeout options specify when to automatically timeout Actvitivy functions. TaskQueue: TaxnexusRulesTaskQueueID,
StartToCloseTimeout: time.Minute, ScheduleToCloseTimeout: scheduleToCloseTimeout,
// Optionally provide a customized RetryPolicy. ScheduleToStartTimeout: scheduleToStartTimeout,
// Temporal retries failures by default, this is just an example. StartToCloseTimeout: startToCloseTimeout,
HeartbeatTimeout: heartbeatTimeout,
WaitForCancellation: waitForCancelation,
ActivityID: NewLeadWorkflowID,
RetryPolicy: retryPolicy, RetryPolicy: retryPolicy,
} }
ctx = temporal_workflow.WithActivityOptions(ctx, options) ctx = temporal_workflow.WithActivityOptions(ctx, options)
sugar.Debugf("rules.NewLeadWorkflow: ctx: %v", ctx)
sugar.Debugf("rules.NewLeadWorkflow: 📏 do Store", ctx) sugar.Debugf("rules.NewLeadWorkflow: 📏 do Store", ctx)
err := temporal_workflow.ExecuteActivity(ctx, StoreLeadActivity, transferDetails).Get(ctx, nil) err := temporal_workflow.ExecuteActivity(
ctx,
StoreLeadActivity,
activityDetails,
).Get(ctx, nil)
if err != nil { if err != nil {
return err return err
} }
sugar.Debugf("rules.NewLeadWorkflow: 📏 do Notify", ctx) sugar.Debugf("rules.NewLeadWorkflow: 📏 do Notify", ctx)
err = temporal_workflow.ExecuteActivity(ctx, NotifyLeadActivity, transferDetails).Get(ctx, nil) err = temporal_workflow.ExecuteActivity(
ctx,
NotifyLeadActivity,
activityDetails,
).Get(ctx, nil)
if err != nil { if err != nil {
return err return err
} }

View File

@ -29,6 +29,8 @@ type NewLeadActivityWrapper struct {
// StoreLeadActivity posts a new lead object to datastore // StoreLeadActivity posts a new lead object to datastore
func StoreLeadActivity(ctx workflow.Context, w NewLeadActivityWrapper) error { //nolint:gocritic // don't care func StoreLeadActivity(ctx workflow.Context, w NewLeadActivityWrapper) error { //nolint:gocritic // don't care
sugar.Info("rules.StoreLeadActivity: 📥")
sugar.Debugf("rules.StoreLeadActivity: 📏 ctx: %v", ctx)
obj := &crm_models.Lead{ obj := &crm_models.Lead{
Company: w.Company, Company: w.Company,
Description: w.Description, Description: w.Description,
@ -51,13 +53,14 @@ func StoreLeadActivity(ctx workflow.Context, w NewLeadActivityWrapper) error { /
if err != nil { if err != nil {
return err return err
} }
sugar.Info("crm.storeLead: 👍 📤") sugar.Info("rules.StoreLeadActivity: 👍 📤")
return nil return nil
} }
// NotifyLeadActivity sends an email to a new lead // NotifyLeadActivity sends an email to a new lead
func NotifyLeadActivity(ctx context.Context, w NewLeadActivityWrapper) error { //nolint:gocritic,lll // todo #2 need email templates func NotifyLeadActivity(ctx context.Context, w NewLeadActivityWrapper) error { //nolint:gocritic,lll // todo #2 need email templates
sugar.Info("workflow.notifyLead: 📥") sugar.Info("rules.NotifyLeadActivity: 📥")
sugar.Debugf("rules.StoreLeadActivity: 📏 ctx: %v", ctx)
var buf bytes.Buffer var buf bytes.Buffer
const textBody = ` const textBody = `
@ -98,6 +101,6 @@ Alert! New Lead Inquiry from Taxnexus.io website.
if err != nil { if err != nil {
return err return err
} }
sugar.Info("workflow.notifyLead: 👍 📤") sugar.Info("rules.NotifyLeadActivity: 👍 📤")
return nil return nil
} }

View File

@ -15,11 +15,18 @@ import (
) )
// const dateFormat = "2006-01-02" // const dateFormat = "2006-01-02"
const getTimeout = 6 * time.Minute
// const dateTimeFormat = "2006-01-02T15:04:05-0800" // const dateTimeFormat = "2006-01-02T15:04:05-0800"
const backoffCoefficient = 2.0
const getTimeout = 6 * time.Minute
const heartbeatTimeout = 0
const initialInterval = time.Second
const maximumAttempts = 50
const maximumInterval = time.Minute
const postTimeout = 6 * time.Minute const postTimeout = 6 * time.Minute
const maxAttempts = 50 const scheduleToCloseTimeout = 0
const scheduleToStartTimeout = 0
const startToCloseTimeout = time.Minute
const waitForCancelation = false
var sugar = logger.New(zapcore.DebugLevel) var sugar = logger.New(zapcore.DebugLevel)
@ -28,9 +35,6 @@ var devopsClient = devops_client.Default
var opsClient = ops_client.Default var opsClient = ops_client.Default
var workflowClient = workflow_client.Default var workflowClient = workflow_client.Default
// NewLeadWorkflowID is the text identifier for new-lead-workflow
const NewLeadWorkflowID = "new-lead-workflow"
// NewDeveloperWorkflowID is the text identifier for new-developer-workflow // NewDeveloperWorkflowID is the text identifier for new-developer-workflow
const NewDeveloperWorkflowID = "new-developer-workflow" const NewDeveloperWorkflowID = "new-developer-workflow"