|
| 1 | +// Copyright 2020 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// relworker is a worker process for managing the release process of Go. |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "flag" |
| 11 | + "log" |
| 12 | + "os" |
| 13 | + "time" |
| 14 | + |
| 15 | + "cloud.google.com/go/pubsub" |
| 16 | + "google.golang.org/grpc/codes" |
| 17 | + "google.golang.org/grpc/status" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + projectID = flag.String("project-id", os.Getenv("PUBSUB_PROJECT_ID"), "Pubsub project ID for communicating with relui. Uses PUBSUB_PROJECT_ID by default.") |
| 22 | + topicID = flag.String("topic-id", "relui-development", "Pubsub topic ID for communicating with relui.") |
| 23 | +) |
| 24 | + |
| 25 | +func main() { |
| 26 | + flag.Parse() |
| 27 | + ctx := context.Background() |
| 28 | + |
| 29 | + sub := getSubscription(ctx, *projectID, *topicID, "relworker") |
| 30 | + log.Fatal(sub.Receive(ctx, func(ctx context.Context, msg *pubsub.Message) { |
| 31 | + log.Println(string(msg.Data)) |
| 32 | + msg.Ack() |
| 33 | + })) |
| 34 | +} |
| 35 | + |
| 36 | +// getSubscription creates and returns a pubsub subscription from the project |
| 37 | +// specified in projectId, which is to be used for communicating with relui. |
| 38 | +// |
| 39 | +// It is safe to call if a subscription already exists. A reference to the |
| 40 | +// subscription will be returned. |
| 41 | +func getSubscription(ctx context.Context, projectID, topicID, subscriptionID string) *pubsub.Subscription { |
| 42 | + client, err := pubsub.NewClient(ctx, projectID) |
| 43 | + if err != nil { |
| 44 | + log.Fatalf("pubsub.NewClient(_, %q) = %v, wanted no error", projectID, err) |
| 45 | + } |
| 46 | + t := client.Topic(topicID) |
| 47 | + // TODO(golang.org/issue/40279): determine if these values are appropriate, move to const/config. |
| 48 | + scfg := pubsub.SubscriptionConfig{Topic: t, AckDeadline: 10 * time.Second, ExpirationPolicy: 24 * time.Hour} |
| 49 | + _, err = client.CreateSubscription(ctx, subscriptionID, scfg) |
| 50 | + if err != nil && status.Code(err) != codes.AlreadyExists { |
| 51 | + log.Fatalf("client.CreateSubscription(_, %q, %v) = _, %q, wanted no error or already exists error", subscriptionID, scfg, err) |
| 52 | + } |
| 53 | + return client.Subscription(subscriptionID) |
| 54 | +} |
0 commit comments