Example #1
0
func (q *queuePoller) drainMessages(ctx context.Context) error {
	service := sqs.New(q.cfg)
	for {
		select {
		case <-ctx.Done():
			return ctx.Err()
		case <-q.closeSignal:
			return nil
		default:
		}
		msg := sqs.ReceiveMessageInput{
			QueueUrl:        &q.queueURL,
			WaitTimeSeconds: &q.waitTimeSeconds,
		}
		if q.visibilityTimeout != 0 {
			msg.VisibilityTimeout = &q.visibilityTimeout
		}
		resp, err := service.ReceiveMessage(&msg)
		if err != nil {
			return wraperr(err, "cannot receive messagges from queue")
		}
		if err := q.forwardMsgs(ctx, resp); err != nil {
			return err
		}
	}
}
Example #2
0
func (q *QueueInfo) ReceiveMessage(h ReceiveHandle) {
	var input sqs.ReceiveMessageInput
	input.QueueUrl = q.Queue
	input.WaitTimeSeconds = &waitTime
	input.MessageAttributeNames = []*string{&valueName}
	input.MaxNumberOfMessages = &maxMsg

	ro, _ := thisSQS.ReceiveMessage(&input)
	l := len(ro.Messages)
	if l > 0 {
		needDele := set.New()
		wg := new(sync.WaitGroup)
		for i := 0; i < l; i++ {
			msg := ro.Messages[i]
			atts := msg.MessageAttributes
			wg.Add(1)
			go h(wg, atts["value"].BinaryValue, needDele, i)
		}
		wg.Wait()

		indexes := set.IntSlice(needDele)
		var entries []*sqs.DeleteMessageBatchRequestEntry
		for _, index := range indexes {
			msg := ro.Messages[index]

			var entry sqs.DeleteMessageBatchRequestEntry
			entry.Id = msg.MessageId
			entry.ReceiptHandle = msg.ReceiptHandle
			entries = append(entries, &entry)
		}

		q.deleteMessage(&entries)
	}
	q.ReceiveMessage(h)
}
Example #3
0
// GetMessages returns the parsed messages from SQS if any. If an error
// occurs that error will be returned.
func (q *Queue) GetMessages(waitTimeout int64) ([]Message, error) {
	params := sqs.ReceiveMessageInput{
		QueueUrl: aws.String(q.URL),
	}
	if waitTimeout > 0 {
		params.WaitTimeSeconds = aws.Int64(waitTimeout)
	}
	resp, err := q.Client.ReceiveMessage(&params)
	if err != nil {
		return nil, fmt.Errorf("failed to get messages, %v", err)
	}

	msgs := make([]Message, len(resp.Messages))
	for i, msg := range resp.Messages {
		parsedMsg := Message{}
		if err := json.Unmarshal([]byte(aws.StringValue(msg.Body)), &parsedMsg); err != nil {
			return nil, fmt.Errorf("failed to unmarshal message, %v", err)
		}

		msgs[i] = parsedMsg
	}

	return msgs, nil
}