Exemple #1
0
// Start will start the message broker and prepare it for testing.
func (c *Broker) Start(host, port string) (interface{}, error) {
	ctx, err := newContext(c.ProjectID, c.JSONKey)
	if err != nil {
		return "", err
	}

	exists, err := pubsub.TopicExists(ctx, topic)
	if err != nil {
		log.Printf("Failed to check Cloud Pub/Sub topic: %s", err.Error())
		return "", err
	}

	if exists {
		if err := pubsub.DeleteTopic(ctx, topic); err != nil {
			log.Printf("Failed to delete Cloud Pub/Sub topic: %s", err.Error())
			return "", err
		}
	}

	if err := pubsub.CreateTopic(ctx, topic); err != nil {
		log.Printf("Failed to create Cloud Pub/Sub topic: %s", err.Error())
		return "", err
	}

	log.Println("Created Cloud Pub/Sub topic")

	return "", nil
}
Exemple #2
0
func createTopic(ctx context.Context, argv []string) {
	checkArgs(argv, 2)
	topic := argv[1]
	err := pubsub.CreateTopic(ctx, topic)
	if err != nil {
		log.Fatalf("CreateTopic failed, %v", err)
	}
	fmt.Printf("Topic %s was created.\n", topic)
}
Exemple #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]
}
Exemple #4
0
func (s *pubSubServiceImpl) CreateTopic(topic string) error {
	return pubsub.CreateTopic(s.ctx, topic)
}