Ejemplo n.º 1
0
func (l *SQSListener) waitForMessages(q *sqs.Queue) {
	msgResponse, err := q.ReceiveMessage(1)
	if err != nil || len(msgResponse.Messages) < 1 {
		time.Sleep(l.PollInterval)
		return
	}
	l.handle(&msgResponse.Messages[0], q)
}
Ejemplo n.º 2
0
func (l *SQSListener) handle(msg *sqs.Message, q *sqs.Queue) {
	err := l.Handler.Handle(msg)
	if err != nil {
		q.ChangeMessageVisibility(msg, 10)
		fmt.Println(err)
		return
	}
	_, err = q.DeleteMessage(msg)
	if err != nil {
		fmt.Println("unable to delete msg: ", err)
	}
}
Ejemplo n.º 3
0
func deleteFromQueueOnce(q *sqs.Queue, del chan string, timeout time.Duration) error {
	batch := assembleDeleteMessageBatch(del, timeout)

	if len(batch) > 0 {
		log.Printf("deleting %d messages from q\n", len(batch))
		_, err := q.DeleteMessageBatch(batch)
		if err != nil {
			return err
		}
	}

	return nil
}
Ejemplo n.º 4
0
func (r *SQSReader) readFromQueueOnce(q *sqs.Queue) error {
	r.Logger.Printf("%s: reading messages from queue\n", r.QueueName)
	results, err := q.ReceiveMessage(RECV_MAX_MESSAGES)
	if err != nil {
		return err
	}

	r.Logger.Printf("%s: read %d messages\n", r.QueueName, len(results.Messages))
	for _, message := range results.Messages {
		r.enqueueMessage(message.Body, message.ReceiptHandle)
	}

	return nil
}
Ejemplo n.º 5
0
func pushMessage(q *sqs.Queue, message interface{}) error {

	j, err := json.Marshal(message)
	if err != nil {
		return err
	}

	_, err = q.SendMessage(string(j))
	if err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 6
0
func listenOnQueue(q *sqs.Queue, ch chan *sqs.Message) {

	for {
		resp, err := q.ReceiveMessage(1)
		if err != nil {
			log.Panic(err)
		}

		for _, m := range resp.Messages {
			ch <- &m
			q.DeleteMessage(&m)
		}
	}

}
Ejemplo n.º 7
0
func (w *SQSWriter) writeToQueueOnce(q *sqs.Queue) error {
	batch, err := w.assembleSendMessageBatch()
	if err != nil {
		return err
	}

	if len(batch) > 0 {
		w.Logger.Printf("%s: sending %d messages to q\n", w.QueueName, len(batch))
		result, err := q.SendMessageBatch(batch)
		if err != nil {
			w.Logger.Printf("%#v\n", result)
			return err
		}
	}

	return nil
}
func Start(q *sqs.Queue, h Handler, t time.Duration, receiveMessageNum int, client twilio.Client, sender string) {
	fmt.Println(fmt.Sprintf("worker: Start polling  [%s]", time.Now().Local()))
	for {
		resp, err := q.ReceiveMessage(receiveMessageNum)
		if err != nil {
			log.Println(err)
			continue
		}
		if len(resp.Messages) > 0 {
			fmt.Printf("\r")
			run(q, h, resp, client, sender)
		} else {
			fmt.Printf(".")
		}
		time.Sleep(t)
	}
}
Ejemplo n.º 9
0
func (s *SQSClient) handle(message string, q *sqs.Queue) error {
	var res *sqs.SendMessageResponse
	err := Retrier.Retry(func() error {
		// Call sqs.Queue.SendMessage() - initiates a HTTP request.
		var e error
		res, e = q.SendMessage(message)
		return e
	})
	if err != nil {
		return err
	}
	// check md5
	expectedHash := md5.New()
	expectedHash.Write([]byte(message))
	expected := fmt.Sprintf("%x", expectedHash.Sum(nil))
	if expected != res.MD5 {
		return errors.New(fmt.Sprintf("message %s Did Not match expected %s", res.MD5, expected))
	}
	return nil
}
func handleMessage(q *sqs.Queue, m *sqs.Message, h Handler, client twilio.Client, sender string) error {
	var err error
	err = h.HandleMessage(m)
	fmt.Println("Sending the following message : ", m.Body)
	receiver := m.MessageAttribute[1].Value.StringValue // collect the mobile number
	fmt.Println("To : ", receiver)
	fmt.Println("\n")
	message, err2 := twilio.NewMessage(client, sender, receiver, twilio.Body(m.Body))

	// verification
	if err2 != nil {
		fmt.Println(err2)
	} else {
		fmt.Println(message.Status)
	}
	if err != nil {
		return err
	}
	// delete
	_, err = q.DeleteMessage(m)
	return err
}