mirror of
https://github.com/eventials/goevents.git
synced 2025-04-24 13:48:53 +08:00
Better log messages
This commit is contained in:
parent
53cd7ebcd0
commit
31b724fcc7
@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
consumer := sns.NewConsumer(sns.ConsumerConfig{
|
consumer := sns.MustNewConsumer(&sns.ConsumerConfig{
|
||||||
AccessKey: "",
|
AccessKey: "",
|
||||||
SecretKey: "",
|
SecretKey: "",
|
||||||
Region: "us-east-1",
|
Region: "us-east-1",
|
||||||
|
100
sns/consumer.go
100
sns/consumer.go
@ -15,6 +15,12 @@ import (
|
|||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrEmptyConig = errors.New("empty config")
|
||||||
|
ErrEmptyAccessKey = errors.New("empty access key")
|
||||||
|
ErrEmptySecretKey = errors.New("empty secret key")
|
||||||
|
)
|
||||||
|
|
||||||
type snsMessagePayload struct {
|
type snsMessagePayload struct {
|
||||||
Message string `json:"Message"`
|
Message string `json:"Message"`
|
||||||
MessageID string `json:"MessageId"`
|
MessageID string `json:"MessageId"`
|
||||||
@ -38,6 +44,36 @@ type ConsumerConfig struct {
|
|||||||
QueueUrl string
|
QueueUrl string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *ConsumerConfig) setDefaults() {
|
||||||
|
if c.VisibilityTimeout == 0 {
|
||||||
|
c.VisibilityTimeout = 20
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.WaitTimeSeconds == 0 {
|
||||||
|
c.WaitTimeSeconds = 20
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.MaxNumberOfMessages == 0 {
|
||||||
|
c.MaxNumberOfMessages = 5
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ConsumerConfig) isValid() error {
|
||||||
|
if c == nil {
|
||||||
|
return ErrEmptyConig
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.AccessKey == "" {
|
||||||
|
return ErrEmptyAccessKey
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.SecretKey == "" {
|
||||||
|
return ErrEmptySecretKey
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type handler struct {
|
type handler struct {
|
||||||
action string
|
action string
|
||||||
fn messaging.EventHandler
|
fn messaging.EventHandler
|
||||||
@ -47,30 +83,28 @@ type handler struct {
|
|||||||
type consumer struct {
|
type consumer struct {
|
||||||
sqs *sqs.SQS
|
sqs *sqs.SQS
|
||||||
stop chan bool
|
stop chan bool
|
||||||
config ConsumerConfig
|
config *ConsumerConfig
|
||||||
receiveMessageInput *sqs.ReceiveMessageInput
|
receiveMessageInput *sqs.ReceiveMessageInput
|
||||||
m sync.RWMutex
|
m sync.RWMutex
|
||||||
handlers map[string]handler
|
handlers map[string]handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConsumer(config ConsumerConfig) messaging.Consumer {
|
func NewConsumer(config *ConsumerConfig) (messaging.Consumer, error) {
|
||||||
|
if err := config.isValid(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
config.setDefaults()
|
||||||
|
|
||||||
creds := credentials.NewStaticCredentials(config.AccessKey, config.SecretKey, "")
|
creds := credentials.NewStaticCredentials(config.AccessKey, config.SecretKey, "")
|
||||||
|
|
||||||
sess := session.Must(session.NewSession(&aws.Config{
|
sess, err := session.NewSession(&aws.Config{
|
||||||
Region: aws.String(config.Region),
|
Region: aws.String(config.Region),
|
||||||
Credentials: creds,
|
Credentials: creds,
|
||||||
}))
|
})
|
||||||
|
|
||||||
if config.VisibilityTimeout == 0 {
|
if err != nil {
|
||||||
config.VisibilityTimeout = 20
|
return nil, err
|
||||||
}
|
|
||||||
|
|
||||||
if config.WaitTimeSeconds == 0 {
|
|
||||||
config.WaitTimeSeconds = 20
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.MaxNumberOfMessages == 0 {
|
|
||||||
config.MaxNumberOfMessages = 5
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c := &consumer{
|
c := &consumer{
|
||||||
@ -93,7 +127,17 @@ func NewConsumer(config ConsumerConfig) messaging.Consumer {
|
|||||||
WaitTimeSeconds: aws.Int64(c.config.WaitTimeSeconds),
|
WaitTimeSeconds: aws.Int64(c.config.WaitTimeSeconds),
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func MustNewConsumer(config *ConsumerConfig) messaging.Consumer {
|
||||||
|
consumer, err := NewConsumer(config)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return consumer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *consumer) getHandler(action string) (handler, bool) {
|
func (c *consumer) getHandler(action string) (handler, bool) {
|
||||||
@ -190,13 +234,16 @@ func (c *consumer) handleMessage(message *sqs.Message) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log := logrus.WithFields(logrus.Fields{
|
||||||
|
"action": sns.TopicArn,
|
||||||
|
"message_id": message.MessageId,
|
||||||
|
"body": sns.Message,
|
||||||
|
})
|
||||||
|
|
||||||
handler, ok := c.getHandler(sns.TopicArn)
|
handler, ok := c.getHandler(sns.TopicArn)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
logrus.WithFields(logrus.Fields{
|
log.WithError(err).Error("Action not found.")
|
||||||
"error": err,
|
|
||||||
"action": sns.TopicArn,
|
|
||||||
}).Error("Action not found.")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,12 +255,7 @@ func (c *consumer) handleMessage(message *sqs.Message) {
|
|||||||
}, handler.fn)
|
}, handler.fn)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithFields(logrus.Fields{
|
log.WithError(err).Debug("Failed to process event.")
|
||||||
"action": handler.action,
|
|
||||||
"message_id": message.MessageId,
|
|
||||||
"body": sns.Message,
|
|
||||||
"error": err,
|
|
||||||
}).Debug("Failed to process event.")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -223,15 +265,11 @@ func (c *consumer) handleMessage(message *sqs.Message) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Error("Failed to delete message.")
|
log.WithError(err).Error("Failed to delete message.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.WithFields(logrus.Fields{
|
log.Debug("Message handled successfully.")
|
||||||
"action": handler.action,
|
|
||||||
"body": sns.Message,
|
|
||||||
"message_id": message.MessageId,
|
|
||||||
}).Debug("Message handled successfully.")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *consumer) doConsume() {
|
func (c *consumer) doConsume() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user