Closed acrosson closed 7 years ago
I can reproduce it also locally.
You are not passing a valid AppEngine context but a context.Background() to NewClient. Try:
ctx := appengine.NewContext(r)
client, err := pubsub.NewClient(ctx, "app-id")
if err != nil {
log.Fatalf("pubsub Client initialize failed %v", err)
}
We should return an error rather than panic'ing without any context about the issue.
@rakyll
I've tried using the appengine.NewContext(r), and it works (sent message on app engine, received locally on worker) so long as I don't send the request through a goroutine. I'm using gin for the app router, and I've tried copying the gin context but it's still throwing an error.
Example call:
r.GET("/_ah/health", func(c *gin.Context) {
cCp := c.Copy()
SendMsg(cCp)
c.JSON(200, gin.H{
"message": "ok",
})
})
func SendMsg(c *gin.Context) {
logging.Log(c, "Sending Message ...")
b, _ := json.Marshal("gopher")
ctx := appengine.NewContext(c.Request)
client, err := pubsub.NewClient(ctx, "app-id")
if err != nil {
logging.Error(c, fmt.Sprintf("%v", err))
}
topic := client.Topic("events")
ids, err := topic.Publish(ctx, &pubsub.Message{Data: b})
if err != nil {
logging.Error(c, fmt.Sprintf("%v", err))
}
logging.Log(c, fmt.Sprintf("%v", ids))
}
So I guess I have two questions:
RE question 1:
The context I want to use is appengine.BackgroundContext()
but this seems to be only exposed on "flex" environments.
worker.go:30: undefined: "google.golang.org/appengine".BackgroundContext
BackgroundContext Function: https://github.com/golang/appengine/blob/b2b053be966fce2c050a81415eb3c36238908778/appengine_vm.go
I'm going to try to update my worker app.yaml file for the flex environment and see what happens.
/cc @jba
I had been using context.Background() for gitkit, and it was working fine for the last 6 months. But, I deployed today, and this issue broke my app even though I didn't update any of my deps. :\
This really more sounds like AE is not giving clear error messages for using context.Background
. I'll try to fix that up. However, there isn't anything PubSub specific here, other than the observation that App Engine Standard doesn't let you have long-standing TCP connections open, and it is more likely one would try to have a long-standing connection with PubSub.
The basic book example uses context.Background()
. How are we supposed to subscribe to topics outside of a web request?
@drewwells Has your question been answered?
Looks like better errors are being presented. It seems like the getting started should address how to use Pub/Sub (especially Sub) in the context of AppEngine. The context.Context is tied to a web request, so it makes Sub impractical for that application.
I agree, docs should be improved.
Right now, for Pub/Sub, I suggest using the google.golang.org/api/pubsub/v1
package for publishing, and configure the subscription to push to the GAE app (instead of pulling messages).
Some sample code for pub/sub on GAE standard: https://gist.github.com/broady/c79a65cb49d7b6a56448b3345a23b3d6
can i use spring mvc with pubsub ?
I'm getting an invalid memory address when I launch my go app on App Engine. In the main function I attempt to initialize a pubsub client, but it throws an invalid memory address error.
I'm able to get pubsub working locally, I just seem to be running into issues when I deploy it. The error log from GAE is below.
Any suggestions? Thx.
Error log: