Ejemplo n.º 1
0
func createSubscription(ctx context.Context, argv []string) {
	checkArgs(argv, 3)
	sub := argv[1]
	topic := argv[2]
	err := pubsub.CreateSub(ctx, sub, topic, 60*time.Second, "")
	if err != nil {
		log.Fatalf("CreateSub failed, %v", err)
	}
	fmt.Printf("Subscription %s was created.\n", sub)
}
Ejemplo n.º 2
0
// Subscribe prepares the peer to consume messages.
func (c *Peer) Subscribe() error {
	// Subscription names must start with a lowercase letter, end with a
	// lowercase letter or number, and contain only lowercase letters, numbers,
	// dashes, underscores or periods.
	c.subscription = strings.ToLower(fmt.Sprintf("x%sx", broker.GenerateName()))
	exists, err := pubsub.SubExists(c.context, c.subscription)
	if err != nil {
		return err
	}

	if exists {
		return fmt.Errorf("Subscription %s already exists", c.subscription)
	}

	if err := pubsub.CreateSub(c.context, c.subscription, topic, 0, ""); err != nil {
		return err
	}

	go c.ack()

	go func() {
		// TODO: Can we avoid using atomic flag?
		for atomic.LoadInt32(&c.stopped) != stopped {
			messages, err := pubsub.PullWait(c.context, c.subscription, bufferSize)
			if err != nil {
				// Timed out.
				continue
			}

			ids := make([]string, len(messages))
			for i, message := range messages {
				ids[i] = message.AckID
				c.messages <- message.Data
			}
			c.acks <- ids
		}
	}()
	return nil
}
Ejemplo n.º 3
0
func main() {
	var err error

	pubSubCtx, err = bookshelf.PubSubCtx()
	if err != nil {
		log.Fatal(err)
	}

	booksClient, err = books.New(http.DefaultClient)
	if err != nil {
		log.Fatalf("could not access Google Books API: %v", err)
	}

	// ignore returned errors, which will be "already exists". If they're fatal
	// errors, then following calls (e.g. in the subscribe function) will also fail.
	pubsub.CreateTopic(pubSubCtx, bookshelf.PubSubTopic)
	pubsub.CreateSub(pubSubCtx, subName, bookshelf.PubSubTopic, 0, "")

	// Start worker goroutine.
	go subscribe()

	// [START http]
	// Publish a count of processed requests to the server homepage.
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		countMu.Lock()
		defer countMu.Unlock()
		fmt.Fprintf(w, "This worker has processed %d books.", count)
	})

	port := "8080"
	if p := os.Getenv("PORT"); p != "" {
		port = p
	}
	log.Fatal(http.ListenAndServe(":"+port, nil))
	// [END http]
}
Ejemplo n.º 4
0
func consumePubSubMsgs(projectId string, topicName string) <-chan []byte {
	msgC := make(chan []byte, 100)
	subName := topicName + "-2loggly"
	ackDeadline := time.Second * 20

	client, err := google.DefaultClient(context.Background(), pubsub.ScopePubSub)
	if err != nil {
		logFatal(LogData{
			"event": "pubsub.error.auth",
			"error": err.Error(),
		})
	}
	ctx := cloud.NewContext(projectId, client)

	isSub, err := pubsub.SubExists(ctx, subName)
	if err != nil {
		logFatal(LogData{
			"event": "pubsub.error.subexists",
			"error": err.Error(),
		})
	}
	if !isSub {
		err = pubsub.CreateSub(ctx, subName, topicName, ackDeadline, "")
		if err != nil {
			logFatal(LogData{
				"event": "pubsub.error.createsub",
				"error": err.Error(),
			})
		}
	}

	go func() {
		var lastAckErr error
		var lastDecodeErr error

		for {
			msgs, err := pubsub.PullWait(ctx, subName, 20)
			if err != nil {
				logInfo(LogData{
					"event": "pubsub.error.pull",
					"error": err.Error(),
				})
				time.Sleep(time.Second)
				continue
			}

			lastAckErr = nil
			lastDecodeErr = nil

			for _, m := range msgs {
				jsonBytes, err := msgToLogJson(m.Data)
				if err != nil {
					lastDecodeErr = err
				} else {
					msgC <- jsonBytes
				}

				err = pubsub.Ack(ctx, subName, m.AckID)
				if err != nil {
					lastAckErr = err
				}
			}

			// protect against flood of logs from this application
			// when acks/decodes fail but adding rollup & sleep delay
			if lastAckErr != nil {
				logInfo(LogData{
					"event": "pubsub.error.ack",
					"error": lastAckErr.Error(),
				})
				time.Sleep(time.Second)
			}
			if lastDecodeErr != nil {
				logInfo(LogData{
					"event": "pubsub.error.decode",
					"error": lastDecodeErr.Error(),
				})
				time.Sleep(time.Second)
			}
		}
	}()

	return msgC
}
Ejemplo n.º 5
0
func (s *pubSubServiceImpl) CreatePullSub(sub string, topic string) error {
	return pubsub.CreateSub(s.ctx, sub, topic, 0, "")
}