Hello,
I was trying to deploy an API using PubSub with AppEngine but I got a "not an App Engine context" error, it's related to the following code:
package main
import ( "log"
)
var ( ctx context.Context pubsubClient *pubsub.Client)
func InitPubSub () { ctx = context.Background()
psClient, err := pubsub.NewClient(ctx, "myproject-645411") if err != nil { log.Println("(init pub sub) error while creating new pubsub client:", err)
} else { pubsubClient = psClient
}}So I was looking at the BackgroundContext func from the appengine package but it says that it only works with AppEngine flexible environment (standard environment seems more appropriate to my app): https://godoc.org/google.golang.org/appengine#BackgroundContext
Do you know if there's another function I can use? Or should I create and close a client for each request?
Thanks!
psClient, err := pubsub.NewClient(ctx, "myproject-645411")panic: not an App Engine context
goroutine 1 [running]:
panic(0x17db1c0, 0xc010526280)
go/src/runtime/panic.go:481 +0x3e6
google.golang.org/appengine/internal.fullyQualifiedAppID(0x2a6f03567698, 0xc01040f5e8, 0x0, 0x0)
google.golang.org/appengine/internal/identity_classic.go:54 +0x8b
google.golang.org/appengine/internal.FullyQualifiedAppID(0x2a6f03567698, 0xc01040f5e8, 0x0, 0x0)
google.golang.org/appengine/internal/api_common.go:77 +0xe2
google.golang.org/appengine/internal.AppID(0x2a6f03567698, 0xc01040f5e8, 0x0, 0x0)
google.golang.org/appengine/internal/identity.go:13 +0x37
google.golang.org/appengine.AppID(0x2a6f03567698, 0xc01040f5e8, 0x0, 0x0)
google.golang.org/appengine/identity.go:20 +0x37
golang.org/x/oauth2/google.FindDefaultCredentials(0x2a6f03567698, 0xc01040f5e8, 0xc0105f3f60, 0x2, 0x2, 0xc0105f3f60, 0x0, 0x0)
golang.org/x/oauth2/google/default.go:86 +0x5a8
golang.org/x/oauth2/google.DefaultTokenSource(0x2a6f03567698, 0xc01040f5e8, 0xc0105f3f60, 0x2, 0x2, 0x0, 0x0, 0x0, 0x0)
golang.org/x/oauth2/google/default.go:43 +0x6b
google.golang.org/api/transport.DialGRPC(0x2a6f03567698, 0xc01040f5e8, 0xc01053de80, 0x3, 0x4, 0x1, 0x0, 0x0)
google.golang.org/api/transport/dial.go:149 +0x379
cloud.google.com/go/pubsub/apiv1.NewPublisherClient(0x2a6f03567698, 0xc01040f5e8, 0xc010581d30, 0x1, 0x1, 0x2a6f03567920, 0x0, 0x0)
cloud.google.com/go/pubsub/apiv1/publisher_client.go:122 +0x1cc
cloud.google.com/go/pubsub.newPubSubService(0x2a6f03567698, 0xc01040f5e8, 0xc010581d30, 0x1, 0x1, 0x0, 0x0, 0x0)
cloud.google.com/go/pubsub/service.go:91 +0x79
cloud.google.com/go/pubsub.NewClient(0x2a6f03567698, 0xc01040f5e8, 0x1995f20, 0xd, 0x0, 0x0, 0x0, 0xc01061c1e0, 0x0, 0x0)
cloud.google.com/go/pubsub/pubsub.go:68 +0x495
main.InitPubSub()
pubsub.go:18 +0xbb
main.main()
main.go:14 +0x1c
If not, I'll follow the guide you linked me and provide you with the stacktrace asap.
Thanks again for your help.
var (
ctx context.Context
bigQueryClient *bigquery.Client
)
func InitBigQuery() {
ctx = context.Background()
bqClient, err := bigquery.NewClient(ctx, "voodoo-165916")
if err != nil {
log.Println("(init big query) error while creating new bigquery client:", err)
} else {
bigQueryClient = bqClient
InitUploader()
}
}package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"fmt"
"google.golang.org/appengine"
)
func main () {
// BigQuery
InitBigQuery()
defer bigQueryClient.Close()
http.HandleFunc("/", HandlerBase)
http.HandleFunc("/user", HandlerUser)
http.HandleFunc("/event", HandlerEvent)
appengine.Main()
}