Skip to content

Commit fac8f1e

Browse files
toothrotcagedmantis
andcommitted
cmd/relui: create pubsub topic on start
This will create and connect to a pubsub topic for communicating with relui workers on application start. If the topic already exists, it will just get a reference to the topic. For golang/go#40279 Co-authored-by: Carlos Amedee <[email protected]> Change-Id: Ic173212cd15562b9d1a1cc601d307d5ee1a4e811 Reviewed-on: https://go-review.googlesource.com/c/build/+/257237 Trust: Alexander Rakoczy <[email protected]> Run-TryBot: Alexander Rakoczy <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 3b519a6 commit fac8f1e

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

cmd/relui/main.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,25 @@
66
package main
77

88
import (
9+
"context"
910
"flag"
1011
"io/ioutil"
1112
"log"
1213
"net/http"
1314
"os"
1415
"path/filepath"
1516

17+
"cloud.google.com/go/pubsub"
1618
"github.com/golang/protobuf/proto"
1719
reluipb "golang.org/x/build/cmd/relui/protos"
20+
"google.golang.org/grpc/codes"
21+
"google.golang.org/grpc/status"
1822
)
1923

2024
var (
2125
devDataDir = flag.String("dev-data-directory", defaultDevDataDir(), "Development-only directory to use for storage of application state.")
26+
projectID = flag.String("project-id", os.Getenv("PUBSUB_PROJECT_ID"), "Pubsub project ID for communicating with workers. Uses PUBSUB_PROJECT_ID if unset.")
27+
topicID = flag.String("topic-id", "relui-development", "Pubsub topic ID for communicating with workers.")
2228
)
2329

2430
func main() {
@@ -27,7 +33,12 @@ func main() {
2733
if err := fs.load(); err != nil {
2834
log.Fatalf("Error loading state from %q: %v", *devDataDir, err)
2935
}
30-
s := &server{store: fs, configs: loadWorkflowConfig("./workflows")}
36+
ctx := context.Background()
37+
s := &server{
38+
configs: loadWorkflowConfig("./workflows"),
39+
store: fs,
40+
topic: getTopic(ctx),
41+
}
3142
http.Handle("/workflows/create", http.HandlerFunc(s.createWorkflowHandler))
3243
http.Handle("/workflows/new", http.HandlerFunc(s.newWorkflowHandler))
3344
http.Handle("/", fileServerHandler(relativeFile("./static"), http.HandlerFunc(s.homeHandler)))
@@ -39,6 +50,22 @@ func main() {
3950
log.Fatal(http.ListenAndServe(":"+port, http.DefaultServeMux))
4051
}
4152

53+
// getTopic creates and returns a pubsub topic from the project specified in projectId, which is to be used for
54+
// communicating with relui workers.
55+
//
56+
// It is safe to call if a topic already exists. A reference to the topic will be returned.
57+
func getTopic(ctx context.Context) *pubsub.Topic {
58+
client, err := pubsub.NewClient(ctx, *projectID)
59+
if err != nil {
60+
log.Fatalf("pubsub.NewClient(_, %q) = %v, wanted no error", *projectID, err)
61+
}
62+
_, err = client.CreateTopic(ctx, *topicID)
63+
if err != nil && status.Code(err) != codes.AlreadyExists {
64+
log.Fatalf("client.CreateTopic(_, %q) = %v, wanted no error", *topicID, err)
65+
}
66+
return client.Topic(*topicID)
67+
}
68+
4269
// loadWorkflowConfig loads Workflow configuration files from dir. It expects all files to be in textproto format.
4370
func loadWorkflowConfig(dir string) []*reluipb.Workflow {
4471
fs, err := filepath.Glob(filepath.Join(relativeFile(dir), "*.textpb"))

cmd/relui/web.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"path"
1616
"path/filepath"
1717

18+
"cloud.google.com/go/pubsub"
1819
"github.com/golang/protobuf/proto"
1920
reluipb "golang.org/x/build/cmd/relui/protos"
2021
)
@@ -55,6 +56,9 @@ type server struct {
5556

5657
// store is for persisting application state.
5758
store store
59+
60+
// topic is for communicating with relui workers.
61+
topic *pubsub.Topic
5862
}
5963

6064
type homeResponse struct {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
cloud.google.com/go v0.54.0
77
cloud.google.com/go/bigquery v1.4.0
88
cloud.google.com/go/datastore v1.1.0
9+
cloud.google.com/go/pubsub v1.2.0
910
cloud.google.com/go/storage v1.6.0
1011
github.com/NYTimes/gziphandler v1.1.1
1112
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 // indirect

0 commit comments

Comments
 (0)