Ejemplo n.º 1
0
func WatchLocalChanges() {
	channel := q.MustChan()

	go declareExchange(channel)

	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}

	// Process events
	go func() {
		for {
			select {
			case ev := <-watcher.Event:
				// log.Println("event:", ev)
				if !ev.IsAttrib() {
					handleChange(channel, ev)
				}
			case err := <-watcher.Error:
				log.Println("PUBLISHER: error:", err)
			}
		}
		watcher.Close()
	}()

	err = watcher.Watch(rootDir)
	if err != nil {
		log.Fatal(err)
	}
}
Ejemplo n.º 2
0
func ListenForChanges() {
	var err error
	hostname, _ = os.Hostname()

	channel := q.MustChan()

	log.Println("CONSUMER: got Channel, declaring Exchange")
	if err = channel.ExchangeDeclare(
		"changes", // name of the exchange
		"fanout",  // type
		true,      // durable
		true,      // delete when complete
		false,     // internal
		false,     // noWait
		nil,       // arguments
	); err != nil {
		panic(fmt.Errorf("Exchange Declare: %s", err))
	}

	log.Printf("CONSUMER: declaring Queue (%s)", hostname)
	state, err := channel.QueueDeclare(
		hostname,     // name of the queue
		true,         // durable
		false,        // delete when usused
		false,        // exclusive
		false,        // noWait
		amqp.Table{}, // arguments
	)
	if err != nil {
		panic(fmt.Errorf("Queue Declare: %s", err))
	}

	log.Printf("CONSUMER: binding to Exchange (key '%s')", state.Messages, state.Consumers, "")

	if err = channel.QueueBind(
		hostname,  // name of the queue
		"",        // bindingKey
		"changes", // sourceExchange
		false,     // noWait
		nil,       // arguments
	); err != nil {
		panic(fmt.Errorf("Queue Bind: %s", err))
	}

	log.Printf("CONSUMER: Queue bound to Exchange, starting Consume")
	changes, err := channel.Consume(
		hostname, // name
		"",       // consumerTag,
		false,    // auto-ack
		false,    // exclusive
		false,    // noLocal
		false,    // noWait
		nil,      // arguments
	)
	if err != nil {
		panic(fmt.Errorf("Queue Consume: %s", err))
	}

	go handle(changes)
}
Ejemplo n.º 3
0
func RequestFile(change *Change) {
	log.Println("requesting file", change.Path)
	var err error

	channel := q.MustChan()

	queue, err := channel.QueueDeclare(
		"",    // name of the queue
		false, // durable
		true,  // delete when usused
		true,  // exclusive
		false, // noWait
		nil,   // arguments
	)
	if err != nil {
		panic(fmt.Errorf("Queue Declare: %s", err))
	}

	t := &Transfer{
		Checksum:  change.Checksum,
		Path:      change.Path,
		Requestor: hostname,
	}
	req, _ := json.Marshal(t)

	err = channel.Publish(
		"file-requests", // exchange
		"",              // routing key
		false,           // mandatory
		false,           // immediate
		amqp.Publishing{
			ContentType: "application/json",
			Body:        req,
			ReplyTo:     queue.Name,
		})

	go WaitForFile(channel, change.Path, queue.Name)
}
Ejemplo n.º 4
0
func ListenForTransferRequests() {
	channel := q.MustChan()

	if err := channel.ExchangeDeclare(
		"file-requests", // name of the exchange
		"fanout",        // type
		true,            // durable
		false,           // delete when complete
		false,           // internal
		false,           // noWait
		nil,             // arguments
	); err != nil {
		panic(fmt.Errorf("Exchange Declare: %s", err))
	}

	queue, err := channel.QueueDeclare(
		fmt.Sprintf("%s-files", hostname), // name of the queue
		false, // durable
		true,  // delete when usused
		true,  // exclusive
		false, // noWait
		nil,   // arguments
	)
	if err != nil {
		panic(fmt.Errorf("Failed to declare a queue %s", err.Error()))
	}

	if err = channel.QueueBind(
		queue.Name,      // name of the queue
		"",              // bindingKey
		"file-requests", // sourceExchange
		false,           // noWait
		nil,             // arguments
	); err != nil {
		panic(fmt.Errorf("Queue Bind: %s", err))
	}

	err = channel.Qos(
		1,     // prefetch count
		0,     // prefetch size
		false, // global
	)
	if err != nil {
		panic(fmt.Errorf("Failed to set QoS %s", err.Error()))
	}

	msgs, err := channel.Consume(
		queue.Name, // queue
		"",         // consumer
		false,      // auto-ack
		false,      // exclusive
		false,      // no-local
		false,      // no-wait
		nil,        // args
	)
	if err != nil {
		panic(fmt.Errorf("Failed to register a consumer %s", err.Error()))
	}

	log.Println("awaiting file transfer requests...")
	for msg := range msgs {
		msg.Ack(false)
		var transfer Transfer

		if err := json.Unmarshal(msg.Body, &transfer); err != nil {
			log.Println(fmt.Errorf("received a dirty transfer request '%s' %s", string(msg.Body), err.Error()))
			msg.Reject(false)
			continue
		}

		if transfer.Requestor == hostname {
			continue
		}
		log.Println("received a file transfer request", transfer.Path)

		go HandleTransfer(transfer, &msg)
	}
	log.Println("no longer consuming transfers")
}