Example #1
0
// Function to send keep alive message over specified channel
func sendKeepAlive(channel *amqp.Channel, client ClientConfig) error {
	// Create a keepalive struct to send to server
	body := &KeepAlive{
		Name:          client.Name,
		Address:       client.Address,
		Subscriptions: client.Subscriptions,
		Timestamp:     time.Now().Unix(),
	}

	// Parse the keepalive struct to json
	bodyJson, err := json.Marshal(body)
	if err != nil {
		return err
	}

	// Create the amqp message to publish
	msg := amqp.Publishing{
		ContentType:  "application/octet-stream",
		DeliveryMode: amqp.Persistent,
		Priority:     0,
		Body:         bodyJson,
	}

	// Publish message to amqp server
	if err := channel.Publish("keepalives", "", false, false, msg); err != nil {
		return err
	}

	// Returns nil as error if message was sent successfully
	return nil
}
Example #2
0
func (rc RequestChan) publish() error {
	var (
		err error
		out *amqp.Channel
	)
	for r := range rc {
		if r == nil {
			break
		}
		for out == nil {
			out, err = dial()
			if err != nil {
				out = nil
				log.Println(err)
				time.Sleep(*backoff)
			}
		}

		err = out.Publish(*exchange, *key, false, false,
			amqp.Publishing{
				Headers:     amqp.Table(r.Headers),
				ContentType: r.ContentType,
				Body:        r.Body,
			},
		)
		if err != nil {
			return err
		}
	}
	return nil
}
Example #3
0
func handleChange(channel *amqp.Channel, ev *fsnotify.FileEvent) error {
	// check to see if the file is locked. If it is then break
	path, _ := filepath.Rel(rootDir, ev.Name)
	if FileIsLocked(path) {
		return nil
	}
	log.Printf("PUBLISHER: publishing %dB path (%q)", len(path), path)

	var change = &Change{
		IsCreate: ev.IsCreate(),
		IsDelete: ev.IsDelete(),
		IsMod:    ev.IsModify(),
		IsMove:   ev.IsRename(),
		ModDate:  time.Now().UTC(),
		Path:     path,
		Source:   hostname,
		Checksum: Checksum(ev.Name),
	}

	msg, err := json.Marshal(change)
	if err != nil {
		log.Fatalf("ERROR marshaling msg %s", change)
	}

	if err := channel.Publish(
		"changes", // publish to an exchange
		"",        // routing to 0 or more queues
		false,     // mandatory
		false,     // immediate
		amqp.Publishing{
			Headers:         amqp.Table{},
			ContentType:     "application/json",
			ContentEncoding: "",
			Body:            msg,
			DeliveryMode:    amqp.Transient, // 1=non-persistent, 2=persistent
			Priority:        0,              // 0-9
			// a bunch of application/implementation-specific fields
			/*

				ContentType     string    // MIME content type
				ContentEncoding string    // MIME content encoding
				DeliveryMode    uint8     // Transient (0 or 1) or Persistent (2)
				Priority        uint8     // 0 to 9
				CorrelationId   string    // correlation identifier
				ReplyTo         string    // address to to reply to (ex: RPC)
				Expiration      string    // message expiration spec
				MessageId       string    // message identifier
				Timestamp       time.Time // message timestamp
				Type            string    // message type name
				UserId          string    // creating user id - ex: "guest"
				AppId           string    // creating application id
			*/
		},
	); err != nil {
		log.Fatalf("Exchange Publish: %s", err)
		panic(err)
	}

	return nil
}
Example #4
0
func cmdMain(ch *amqp.Channel) {
	fmt.Println(len(os.Args))
	if len(os.Args) != 4 {
		cmdUsage()
	}
	name := os.Args[2]
	val, _ := strconv.Atoi(os.Args[3])
	fmt.Println("ARGS", os.Args, name, val)

	m := UpdateMsg{name, val}

	msg, err := json.Marshal(m)
	if err != nil {
		log.Fatal("Barf!", err)
	}
	err = ch.Publish(
		"updateMsgs", // exchange
		"",           // routing key
		false,        // mandatory
		false,        // immediate
		amqp.Publishing{
			ContentType: "text/json",
			Body:        msg,
		})
	failOnError(err, "Failed to publish a message")
	log.Printf("Sending msg -- %s", msg)
	os.Exit(0)

}
Example #5
0
func (rc requestCh) publish() error {
	var (
		err error
		out *amqp.Channel
	)
	for r := range rc {
		if r == nil {
			break
		}
		for out == nil {
			out, err = dial()
			if err != nil {
				out = nil
				log.Println(err)
				time.Sleep(*backoff)
			}
		}

		err = out.Publish(*exchange, *key, false, false,
			amqp.Publishing{
				Headers:      amqp.Table(r.headers),
				DeliveryMode: amqp.Persistent,
				Body:         []byte(r.body),
			},
		)
		if err != nil {
			return err
		}
	}
	return nil
}
Example #6
0
func (r *Rabbitmq) Publish(ch *amqp.Channel, msg *amqp.Publishing) (err error) {
	try_count := 3
	if ch == nil {
		log.Printf("[WARNING] send to mq failed: Channel must not be empty\n")
		return errors.New("ampq channel is nil")
	}

	defer func() {
		if f := recover(); f != nil {
			log.Printf("[WARNING] send to mq failed:%v\n", f)
			err = f.(error)
		}
	}()

	for i := 0; i < try_count; i++ {

		err = ch.Publish(r.Exchange, r.RoutingKey, false, false, *msg)

		if err != nil {
			conn_err := r.Reconn()
			ch1, _ := r.Channel()
			*ch = *ch1
			log.Printf("[WARNING] RabbitMQ Reconn err is %v send to bnow failed:%v try Count:%v\n", conn_err, err, i)
			time.Sleep(time.Second)
			continue
		}
		break
	}
	return
}
func monitoring(uri string, queueName string, mgmtUri string, prefix string) {
	var (
		queueConn *amqp.Connection
		queueChan *amqp.Channel
		err       error
	)
	queueConn, queueChan, err = rabbitmqConnect(uri, queueName)
	if err != nil {
		return
	}
	for {
		log.Printf("fetch rabbitmq stats")
		var metrics []graphite.Metric
		for _, metric := range fetchQueueMetrics(mgmtUri, prefix) {
			metrics = append(metrics, metric)
		}
		for _, metric := range fetchExchangeMetrics(mgmtUri, prefix) {
			metrics = append(metrics, metric)
		}
		for _, metric := range metrics {
			body := []byte(metric.Name + "\t" + metric.Value + "\t" + strconv.FormatInt(metric.Timestamp, 10))
			msg := amqp.Publishing{ContentType: "text/plain", Body: body}
			err = queueChan.Publish("", queueName, false, false, msg)
			if err != nil {
				log.Printf("publish err: %s", err)
				return
			}
			//log.Printf("metric\t%s\t\t%s", metric.Name, metric.Value)
		}
		time.Sleep(time.Second * 5)
	}
	queueChan.Close()
	queueConn.Close()
}
Example #8
0
func produce(conn *amqp.Connection, channel *amqp.Channel, val *interface{}) {

	if val == nil {
		log.Warn("the redis json is nil")
		return
	}

	body, err := json.Marshal(val)

	if err != nil || body == nil {
		log.Error("redis event to json error: %s , oplog is : %s ", err, string(body))
	} else {

		routingKey := "redis.event"
		log.Info("routing key is : %s ", routingKey)

		err = channel.Publish(
			EXCHANGE_KEY, // exchange
			routingKey,   // routing key
			false,        // mandatory
			false,        // immediate
			amqp.Publishing{
				ContentType: "text/plain",
				Body:        body,
			})
		if err != nil {
			log.Error("publish message err : %s ", err)
		}

		//TODO recreate channel ?
	}
}
func SendEmail(req *http.Request, mail Email, r render.Render, ch *amqp.Channel) {
	msg, _ := json.Marshal(mail)

	q, _ := ch.QueueDeclare(
		"email", // name
		true,    // durable
		false,   // delete when usused
		false,   // exclusive
		false,   // no-wait
		nil,     // arguments
	)

	err := ch.Publish(
		"",     // exchange
		q.Name, // routing key
		false,  // mandatory
		false,  // immediate
		amqp.Publishing{
			ContentType: "json/plain",
			Body:        []byte(msg),
		})

	if err == nil {
		r.JSON(http.StatusOK, map[string]interface{}{"Email": mail})
	} else {
		r.JSON(http.StatusInternalServerError, err)
	}
}
Example #10
0
func (s *Server) run() error {
	ch, err := s.dial()
	if err != nil {
		return err
	}

	for name, sink := range s.Sinks {
		if err := ch.ExchangeDeclare(name, sink.Type, true, false, false, false, nil); err != nil {
			return err
		}

		go func(name string, sink *Sink) {
			var (
				ch  *amqp.Channel
				elt []byte
				err error
			)
			for s.alive {
				if err != nil {
					ch = nil
					log.Println(err)
					err = nil
					time.Sleep(*backoff)
					continue
				}
				if len(elt) == 0 {
					elt = <-sink.ch
				}
				if ch == nil {
					if ch, err = s.dial(); err != nil {
						continue
					}
				}
				if ch != nil {
					err = ch.Publish(name, name, false, false, amqp.Publishing{Body: elt})
					if err == nil {
						elt = nil
					}
				}
			}
		}(name, sink)
	}

	for i := 0; i < *threads; i++ {
		go func() {
			for s.alive {
				elt := <-s.ch
				for _, sink := range s.Sinks {
					if sink.regex != nil && !sink.regex.Match(elt) {
						continue
					}
					sink.ch <- elt
				}
			}
		}()
	}
	go s.consumer()
	return nil
}
Example #11
0
func moveToFailQueue(ch *amqp.Channel, msgBody []byte, headers map[string]string) bool {
	var tableHeader amqp.Table
	tableHeader = make(amqp.Table)
	mapB, _ := json.Marshal(headers)
	tableHeader["sended"] = string(mapB)
	ch.Publish("", "events-hook-comm-fail-queue", false, false, amqp.Publishing{Headers: tableHeader, Body: msgBody})
	return true
}
Example #12
0
func publishQueueName(ch *amqp.Channel) {
	msg := amqp.Publishing{Body: []byte(*name)}

	ch.Publish(
		"amq.fanout", // exchange string
		"",           // key string
		false,        // mandatory bool
		false,        // immediate bool
		msg)          // msg amqp.Publishing
}
Example #13
0
func PublishJson(content []byte, exchange string, ch *amqp.Channel) error {
	return ch.Publish(
		exchange, // exchange
		"",       // routing key
		false,    // mandatory
		false,    // immediate
		amqp.Publishing{
			ContentType: "application/json",
			Body:        content,
		})
}
Example #14
0
func PushKey(ch *amqp.Channel, r HttpResponse, queueName string) error {
	v := fmt.Sprintf("http_response:%s:%s", r.CrawlerName, r.Url)
	msg := amqp.Publishing{
		// DeliveryMode: amqp.Persistent,
		DeliveryMode: amqp.Transient,
		ContentType:  "text/plain",
		Body:         []byte(v),
	}
	err := ch.Publish("", queueName, false, false, msg)
	return err
}
Example #15
0
func HandleMessageBytes(bytes []byte, channel *amqp.Channel, debugger amqptools.Debugger) {
	var err error

	delivery := &amqptools.DeliveryPlus{}

	err = json.Unmarshal(bytes, &delivery)

	if debugger.WithError(err, "Unable to unmarshal delivery into JSON: ", err) {
		os.Exit(7)
	}

	// DO STUFF WITH INPUT LINE

	rawDelivery := delivery.RawDelivery
	bodyBytes := rawDelivery.Body

	errorMessage := &ErrorMessage{}

	err = json.Unmarshal(bodyBytes, &errorMessage)
	if debugger.WithError(err, "Unable to unmarshal delivery into JSON: ", err) {
		os.Exit(7)
	}

	oMsg := errorMessage.OriginalMessage

	debugger.Print(fmt.Sprintf("consumed message: %+v", errorMessage.OriginalMessage))

	var timestamp time.Time

	if *timeNow {
		timestamp = time.Now().UTC()
	} else {
		timestamp, err = time.Parse(timeFormat, oMsg.Properties.Timestamp)
		if debugger.WithError(err, fmt.Sprintf("Unable to parse timestamp: %+v", oMsg.Properties.Timestamp), err) {
			os.Exit(11)
		}
	}

	msg := &amqp.Publishing{
		ContentType:     oMsg.Properties.ContentType,
		ContentEncoding: oMsg.Properties.ContentEncoding,
		DeliveryMode:    uint8(oMsg.Properties.DeliveryMode),
		CorrelationId:   oMsg.Properties.CorrelationId,
		MessageId:       oMsg.Properties.MessageId,
		Timestamp:       timestamp,
		AppId:           oMsg.Properties.AppId,
		Body:            []byte(oMsg.Payload),
	}

	err = channel.Publish(oMsg.Exchange, oMsg.RoutingKey, true, false, *msg)
	if debugger.WithError(err, "Unable to publish: ", err) {
		os.Exit(19)
	}
}
Example #16
0
func Publish(messages chan *amqp.Publishing, connectionUri, exchange,
	routingKey string, mandatory, immediate bool, results chan *PublishResult) {

	var err error
	var conn *amqp.Connection
	var channel *amqp.Channel

	defer close(results)

	if conn, err = amqp.Dial(connectionUri); err != nil {
		results <- &PublishResult{"Failed to connect", err, true}
		return
	}

	defer conn.Close()

	if channel, err = conn.Channel(); err != nil {
		results <- &PublishResult{"Failed to get channel", err, true}
		return
	}

	pubAcks, pubNacks := channel.NotifyConfirm(make(chan uint64), make(chan uint64))
	chanClose := channel.NotifyClose(make(chan *amqp.Error))
	if err = channel.Confirm(false); err != nil {
		results <- &PublishResult{
			"Failed to put channel into confirm mode",
			err,
			true,
		}
		return
	}

	for message := range messages {
		err = channel.Publish(exchange, routingKey, mandatory, immediate, *message)
		if err != nil {
			results <- &PublishResult{"Failed to publish message", err, false}
			continue
		}

		select {
		case err = <-chanClose:
			results <- &PublishResult{"Channel closed!", err, true}
		case <-pubAcks:
			results <- &PublishResult{
				fmt.Sprintf("Published to exchange '%s' routing key '%v': %+v", exchange, routingKey, message),
				nil,
				false,
			}
		case <-pubNacks:
			results <- &PublishResult{"Received basic.nack for message", errors.New("'basic.nack'"), false}
		}
	}
}
Example #17
0
func ensureNonZeroMessageRate(ch *amqp.Channel) {
	for i := 0; i < 2000; i++ {
		q, _ := ch.QueueDeclare(
			"",    // name
			false, // durable
			false, // delete when usused
			true,  // exclusive
			false,
			nil)
		ch.Publish("", q.Name, false, false, amqp.Publishing{Body: []byte("")})
	}
}
Example #18
0
func binary(ch *amqp.Channel, exchange string, n int) error {
	for i := 0; i < n; i++ {
		next := fmt.Sprintf("%63b", rand.Int63())
		err := ch.Publish(exchange, exchange, false, false, amqp.Publishing{
			Body: []byte(next),
		})
		if err != nil {
			return err
		}
	}
	return nil
}
Example #19
0
func publish(ch *amqp.Channel, q amqp.Queue, c <-chan string) {
	for {
		connectionStatus := <-c
		if connectionStatus == "lost" {
			for i := 0; i < 40; i++ {
				conn, err := amqp.Dial("amqp://*****:*****@localhost:5672/")
				if err != nil {
					fmt.Println("Failed to connect to RabbitMQ")
					time.Sleep(1 * time.Second)
					continue
				} else {
					defer conn.Close()
				}
			}
		}
		if connectionStatus == "ok" {
			conn, err := amqp.Dial("amqp://*****:*****@localhost:5672/")
			if err != nil {
				fmt.Println("Failed to connect to RabbitMQ")
				time.Sleep(1 * time.Second)
				continue
			} else {
				defer conn.Close()
			}
			ch, _ := conn.Channel()
			defer ch.Close()
			body := bodyFrom(os.Args)
			q, err := ch.QueueDeclare(
				"task_queue", // name
				true,         // durable
				false,        // delete when unused
				false,        // exclusive
				false,        // no-wait
				nil,          // arguments
			)
			err = ch.Publish(
				"",     // exchange
				q.Name, // routing key
				false,  // mandatory
				false,
				amqp.Publishing{
					DeliveryMode: amqp.Persistent,
					ContentType:  "text/plain",
					Body:         []byte(body),
				})
			if err == nil {
				log.Printf(" [x] Sent %s", body)
			}
		}
		time.Sleep(1 * time.Second)
	}
}
Example #20
0
// send sends a message using the given queue and a channel.
func send(msg []byte, rk string, ch *amqp.Channel, q amqp.Queue) {
	err := ch.Publish(
		"",    // exchange
		rk,    // routing key
		false, // mandatory
		false, // immediate
		amqp.Publishing{
			ContentType: "text/plain",
			Body:        msg,
		})

	if err != nil {
		log.Fatalf("Failed to publish a message: %s", err)
	}
}
Example #21
0
func connectKey(ch *amqp.Channel, key string, exchange string) {
	body := utils.RandomTimelineEvent()
	bodyJSON, err := json.Marshal(body)
	err = ch.Publish(
		exchange, // exchange
		key,      // routing key
		false,    // mandatory
		false,    // immediate
		amqp.Publishing{
			ContentType: "application/json",
			Body:        []byte(bodyJSON),
		})
	utils.FailOnError(err, "failed to publish a message")
	log.Printf(" [x] Sent %s", body)
}
Example #22
0
// Publish a task to an AMQP channel,
// default exchange is "",
// default routing key is "celery"
func (t *Task) Publish(ch *amqp.Channel, exchange, key, content_type string) error {
	body, err := json.Marshal(t)
	if err != nil {
		return err
	}

	msg := amqp.Publishing{
		DeliveryMode:    amqp.Persistent,
		Timestamp:       time.Now(),
		ContentType:     content_type,
		ContentEncoding: "utf-8",
		Body:            body,
	}

	return ch.Publish(exchange, key, false, false, msg)
}
Example #23
0
func publishMessages(wg *sync.WaitGroup, ch *amqp.Channel, qName string, msgs chan string) {
	for m := range msgs {
		err := ch.Publish(
			"",
			qName,
			false,
			false,
			amqp.Publishing{
				MessageId:   fmt.Sprintf(m),
				ContentType: "text/plain",
				Body:        []byte(m),
			})
		failOnError(err, "Failed to send msg")
	}
	wg.Done()
}
Example #24
0
func publishMessage(ch *amqp.Channel, exchange, routingKey, text string) {
	ch.Publish(
		exchange,
		routingKey,
		false,
		false,
		amqp.Publishing{
			Headers:         amqp.Table{},
			ContentType:     "application/json",
			ContentEncoding: "",
			Body:            []byte(text),
			DeliveryMode:    amqp.Transient,
			Priority:        0,
		})

}
Example #25
0
// Function to send keep alive message over specified channel
func Send(channel *amqp.Channel, msgType string, msgBody []byte) error {
	// Create the amqp message to publish
	msg := amqp.Publishing{
		ContentType:  "application/octet-stream",
		DeliveryMode: amqp.Persistent,
		Priority:     0,
		Body:         msgBody,
	}

	// Publish message to amqp server
	if err := channel.Publish(msgType, "", false, false, msg); err != nil {
		return err
	}

	// Returns nil as error if message was sent successfully
	return nil
}
Example #26
0
func publish(channel *amqp.Channel, i int, tenant string, queue string) (err error) {
	err = channel.Publish(
		"",    //exchange
		queue, //routingKey, for some reason we need to put the queue name here
		false,
		false,
		amqp.Publishing{
			Headers:         amqp.Table{},
			ContentType:     "text/plain",
			ContentEncoding: "UTF-8",
			Body:            []byte(fmt.Sprintf("%d,%s", i, tenant)),
			DeliveryMode:    2, // 1=non-persistent, 2=persistent
			Priority:        9,
		},
	)
	return
}
Example #27
0
File: sender.go Project: relops/rmq
func (s *client) send(ch *amqp.Channel, group uint64, o *Options, payload []byte) ([]byte, error) {

	id, err := s.flake.Next()
	if err != nil {
		return nil, err
	}

	groupString := fmt.Sprintf("%d", group)

	envelope := amqp.Publishing{
		MessageId:     fmt.Sprintf("%d", id),
		CorrelationId: groupString,
		Body:          payload,
		DeliveryMode:  amqp.Transient,
		Headers:       amqp.Table{timestampHeader: time.Now().UnixNano()},
	}

	if o.Persistent {
		envelope.DeliveryMode = amqp.Persistent
	}

	mandatory := false
	immediate := false

	if err := ch.Publish(o.Exchange, o.Key, mandatory, immediate, envelope); err != nil {
		return nil, fmt.Errorf("Could not publish to exchange %s: %s", o.Exchange, err)
	}

	h := murmur3.New32()
	h.Write(payload)
	sum := h.Sum(nil)

	size := float32(len(payload)) / 1024

	if len(o.Verbose) > 0 {
		log.Infof("[%d] sending %.2f kB (%x) to %s", id, size, sum, o.Key)
	} else {
		log.Infof("[%d] sending %.2f kB (%x)", id, size, sum)
	}

	return sum, nil
}
func dispatchMessage(c *amqp.Channel, message interface{}, queueName string) (err error) {
	body, err := json.Marshal(message)
	if err == nil {
		err = c.Publish(
			"",        // exchange
			queueName, // routing key
			true,      // mandatory
			false,     // immediate
			amqp.Publishing{
				ContentType: "text/plain",
				Body:        []byte(body),
			})
		if err != nil {
			fmt.Printf("Failed to dispatch message: %s\n", err)
		}
	} else {
		fmt.Printf("Failed to marshal message %v (%s)\n", message, err)
	}
	return
}
Example #29
0
func serverMain(ch *amqp.Channel) {

	go msgListener(ch)
	tickCounter := 1
	tickLength := (60000 / bpm) / 4 // 1min divided by bpm divided by 4 microticks

	for {

		if bpm != 0 {
			tickLength = (60000 / bpm) / 4
		}
		timer := time.NewTimer(time.Duration(tickLength) * time.Millisecond)
		beatTick := tickCounter % 32
		beat := (beatTick + 3) / 4
		microTick := tickCounter % 4
		if microTick == 0 {
			microTick = 4
		}

		m := BpmMsg{bpm, microTick, tickLength, beat, tickCounter}

		msg, err := json.Marshal(m)
		if err != nil {
			log.Fatal("Barf!", err)
		}
		err = ch.Publish(
			"bpm", // exchange
			"",    // routing key
			false, // mandatory
			false, // immediate
			amqp.Publishing{
				ContentType: "text/json",
				Body:        msg,
			})
		failOnError(err, "Failed to publish a message")
		log.Printf("Sending msg -- %s", msg)
		tickCounter++

		<-timer.C // pause before next iteration - length of bpm
	}
}
Example #30
0
func handleReq(ch *amqp.Channel, d amqp.Delivery, handler rpcHandler) {
	contentType, body := handler(d.ContentType, d.Body)

	err := ch.Publish(
		"",        // exchange
		d.ReplyTo, // routing key
		false,     // mandatory
		false,     // immediate
		amqp.Publishing{
			ContentType:   contentType,
			CorrelationId: d.CorrelationId,
			Body:          body,
		})

	if err != nil {
		log.Error("Failed to publish a message: " + err.Error())
		return
	}

	d.Ack(false)
}