Exemple #1
0
func (d *DefaultWorker) Consume(ch *amqp.Channel, cfg config.EndpointConfig) (<-chan amqp.Delivery, error) {
	queueName, ok := cfg.QueueConfig["queuename"].(string)
	if !ok {
		return nil, fmt.Errorf("unable to parse queuename from config")
	}

	prefetch, ok := cfg.QueueConfig["prefetch"].(int)
	if !ok || prefetch < 1 {
		prefetch = 1
	}

	q, err := ch.QueueDeclare(
		queueName, // name
		true,      // durable
		false,     // delete when unused
		false,     // exclusive
		false,     // no-wait
		nil,       // arguments
	)
	if err != nil {
		return nil, err
	}

	err = ch.Qos(
		prefetch, // prefetch count
		0,        // prefetch size
		false,    // global
	)
	if err != nil {
		return nil, err
	}

	msgs, err := ch.Consume(
		q.Name, // queue
		"",     // consumer
		false,  // auto-ack
		false,  // exclusive
		false,  // no-local
		false,  // no-wait
		nil,    // args
	)
	if err != nil {
		return nil, err
	}
	return msgs, nil
}
Exemple #2
0
func (rb *AmqpBeat) consumeIntoStream(stream chan<- *AmqpEvent, ch *amqp.Channel, c *ChannelConfig, wg *sync.WaitGroup) {
	defer wg.Done()

	err := ch.Qos(*c.QosPrefetchCount, 0, false)
	if err != nil {
		logp.Err("Failed to set QoS on queue '%s': %v", *c.Name, err)
	}

	_, err = ch.QueueDeclare(*c.Name, *c.Durable, *c.AutoDelete, false, false, *c.Args)
	if err != nil {
		logp.Err("Failed to declare queue '%s': %v", *c.Name, err)
		return
	}

	q, err := ch.Consume(*c.Name, "", false, false, false, false, *c.Args)
	if err != nil {
		logp.Err("Failed to consume queue %s: %v", *c.Name, err)
		return
	}
	mName := fmt.Sprintf("consumer.%s", *c.Name)
	var msgCount int64
	for {
		select {
		case d := <-q:
			evt, err := rb.newAmqpEvent(&d, c.TypeTag, c.TsField, c.TsFormat)
			msgCount++
			logp.Debug("flow", "Consumed %d into %s", msgCount, *c.Name)
			if err != nil {
				logp.Warn("failed to build event for delivery, will be Nacked. (delivery = %v) (error = %v)", d, err)
				d.Nack(false, true)
			}
			stream <- evt
			rb.metrics <- &metric{name: mName, value: msgCount}

		case <-rb.stop:
			logp.Info("Consumer '%s' is stopping...", *c.Name)
			return
		}
	}
	logp.Info("Consumer '%s' has stopped.")
}
Exemple #3
0
func createQueues(config Configuration, ch *amqp.Channel) {
	if config.Rabbit.CreateQueues {
		// Create Queues for GCM from config
		for i := 0; i < len(config.GcmQueues); i++ {
			if config.GcmQueues[i].IsHourly == true {
				for j := 0; j < 24; j++ {
					jstr := ""
					if j < 10 {
						jstr = "0" + strconv.Itoa(j)
					} else {
						jstr = strconv.Itoa(j)
					}
					_, err := ch.QueueDeclare(
						config.GcmQueues[i].Name+"_"+jstr, // name
						true,  // durable
						false, // delete when unused
						false, // exclusive
						false, // no-wait
						nil,   // arguments
					)
					failOnError(err, "Failed to declare a queue")
				}

			} else {
				_, err := ch.QueueDeclare(
					config.GcmQueues[i].Name, // name
					true,  // durable
					false, // delete when unused
					false, // exclusive
					false, // no-wait
					nil,   // arguments
				)
				failOnError(err, "Failed to declare a queue")
			}

			_, err := ch.QueueDeclare(
				config.GcmQueues[i].GcmTokenUpdateQueue, // name
				true,  // durable
				false, // delete when unused
				false, // exclusive
				false, // no-wait
				nil,   // arguments
			)
			failOnError(err, "Failed to declare a queue")

			_, err = ch.QueueDeclare(
				config.GcmQueues[i].GcmStatusInactiveQueue, // name
				true,  // durable
				false, // delete when unused
				false, // exclusive
				false, // no-wait
				nil,   // arguments
			)
			failOnError(err, "Failed to declare a queue")
		}

		// Create Queues for APN from config
		for i := 0; i < len(config.ApnQueues); i++ {
			if config.ApnQueues[i].IsHourly == true {
				for j := 0; j < 24; j++ {
					jstr := ""
					if j < 10 {
						jstr = "0" + strconv.Itoa(j)
					} else {
						jstr = strconv.Itoa(j)
					}
					_, err := ch.QueueDeclare(
						config.ApnQueues[i].Name+"_"+jstr, // name
						true,  // durable
						false, // delete when unused
						false, // exclusive
						false, // no-wait
						nil,   // arguments
					)
					failOnError(err, "Failed to declare a queue")
				}

			} else {
				_, err := ch.QueueDeclare(
					config.ApnQueues[i].Name, // name
					true,  // durable
					false, // delete when unused
					false, // exclusive
					false, // no-wait
					nil,   // arguments
				)
				failOnError(err, "Failed to declare a queue")
			}

			_, err := ch.QueueDeclare(
				config.ApnQueues[i].ApnStatusInactiveQueue, // name
				true,  // durable
				false, // delete when unused
				false, // exclusive
				false, // no-wait
				nil,   // arguments
			)
			failOnError(err, "Failed to declare a queue")
		}

		err := ch.Qos(
			1,     // prefetch count
			0,     // prefetch size
			false, // global
		)
		failOnError(err, "Failed to set QoS")
	}
}
Exemple #4
0
func (d *Topic) Consume(ch *amqp.Channel, cfg config.EndpointConfig) (<-chan amqp.Delivery, error) {
	queueName, ok := cfg.QueueConfig["queuename"].(string)
	if !ok {
		return nil, fmt.Errorf("unable to parse queuename from config")
	}

	topicName, ok := cfg.QueueConfig["topicname"].(string)
	if !ok {
		return nil, fmt.Errorf("unable to parse topicname from config")
	}

	exchangeName, ok := cfg.QueueConfig["exchangename"].(string)
	if !ok {
		return nil, fmt.Errorf("unable to parse exchangename from config")
	}

	prefetch, ok := cfg.QueueConfig["prefetch"].(int)
	if !ok || prefetch < 1 {
		prefetch = 1
	}

	err := ch.ExchangeDeclare(
		exchangeName, // name
		"topic",      // type
		true,         // durable
		false,        // auto-deleted
		false,        // internal
		false,        // no-wait
		nil,          // arguments
	)
	if err != nil {
		return nil, err
	}

	q, err := ch.QueueDeclare(
		queueName, // name
		true,      // durable
		false,     // delete when usused
		false,     // exclusive
		false,     // no-wait
		nil,       // arguments
	)
	if err != nil {
		return nil, err
	}

	err = ch.Qos(
		prefetch, // prefetch count
		0,        // prefetch size
		false,    // global
	)
	if err != nil {
		return nil, err
	}

	err = ch.QueueBind(
		q.Name,       // queue name
		topicName,    // routing key
		exchangeName, // exchange
		false,
		nil,
	)
	if err != nil {
		return nil, err
	}

	msgs, err := ch.Consume(
		q.Name, // queue
		"",     // consumer
		false,  // auto-ack
		false,  // exclusive
		false,  // no-local
		false,  // no-wait
		nil,    // args
	)
	if err != nil {
		return nil, err
	}
	return msgs, nil
}