Queue doesn't work in production - google-app-engine

When I'm testing my website locally everything works. But in production, I get the following error: Couldn't lease a task: API error 1 (taskqueue: UNKNOWN_QUEUE)
I'm pretty sure my code is correct since it works locally. My best guess is that there is something wrong with the queue.yaml file, but it's dead simple:
queue:
- name: daemonQueue
mode: pull
What could I be doing wrong?
EDIT:
Turns out enqueueing fails as well: TickTask enqueue error: Failed to insert task: API error 1 (taskqueue: UNKNOWN_QUEUE)
Here is how I'm enqueueing the task.
// Add the task to the queue.
func EnqueueWithName(c sessions.Context, task interface{}, tag string, name string) (err error) {
buffer := new(bytes.Buffer)
err = gob.NewEncoder(buffer).Encode(task)
if err != nil {
return
}
newTask := &taskqueue.Task{
Method: "PULL",
Payload: buffer.Bytes(),
Tag: tag,
Name: name}
newTask, err = taskqueue.Add(c, newTask, "daemonQueue")
return err
}

You don't have the queue configured in production. Make sure you are deploying your whole app directory so queue.yaml get uploaded. Point goapp deploy or appcfg.py at the directory holding app.yaml and queue.yaml, not at app.yaml directly.

Related

GAE Go "The request failed because the instance could not start successfully"

I've been developing with Google App Engine Go for awhile, but I haven't touched it in a year. I tried updating my application from "go1" (1.9?) to "go111", and I'm currently getting some weird errors without any explanation as to what's going on.
The errors include:
The request failed because the instance could not start successfully
Container called exit(1).
500 Internal server error
etc.
None of these point me to any specific line in my code where something would go wrong, nor explain anything more meaningful...
I'm guessing the error is stemming from me upgrading between the golang versions. I had to change the app package into main, add a main function to the application, update the appengine package to a newer version, update the gsuite app, add a cloud compilation widget thingy, change app.yaml script from go to auto, etc.
All in all, I'm lost. A Similar SE question yielded no good answers. Someone else suggested app.yaml might be at fault, so here is mine:
runtime: go111
handlers:
- url: /static
static_dir: static
- url: /res
static_dir: res
- url: /update
script: auto
secure: always
login: admin
- url: /.*
script: auto
secure: always
login: optional
Debug console log is very unhelpful:
And the main file looks essentially like:
package main
import (
"fmt"
"google.golang.org/appengine"
"html/template"
"log"
"net/http"
)
var MainTemplate *template.Template
func main() {
http.HandleFunc("/", hello)
var err error
MainTemplate, err = template.ParseFiles("html/main.html")
if err != nil {
log.Printf("ERROR! %v\n", err.Error())
panic(err)
}
log.Printf("Hello world!\n")
}
func hello(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
//................................//
MainTemplate.Execute(w, nil)
}
Anything else it could be?
Okay, after some help from a few comments, here are a few issues that I had to fix to get my code to work:
I converted my init() function into main() without adding a way to listen to requests. The code just ran through the main() function and exited without an error, hence the problems with debugging.
appengine.NewContext(r) is deprecated apparently, so I had to switch those statements to r.Context(). MEANWHILE, Appengine Datastore is still using golang.org/x/net/context and not just context, so if you want to use things like RunInTransaction(), DON'T update your imports, context casts into golang.org/x/net/context just fine
If you follow the official examples provided by Google, you will most likely run into errors like textPayload: "2019/10/20 22:32:46 http: panic serving 127.0.0.1:16051: not an App Engine context. Instead, your code needs to look like the below example.
Since the former app package is now the main package, make sure that any files that are referenced in app.yaml (favicon.ico for example) are in a proper position in relation to the main package (I had to move mine to a different folder to avoid errors popping up every request...).
package main
import (
"google.golang.org/appengine"
"html/template"
"net/http"
)
var MainTemplate *template.Template
func init() {
http.HandleFunc("/", hello)
MainTemplate, _= template.ParseFiles("html/main.html")
}
func main() {
appengine.Main()
}
func hello(w http.ResponseWriter, r *http.Request) {
c := r.Context()
//................................//
MainTemplate.Execute(w, nil)
}
This ought to work. appengine.Main() apparently connects you to all the appengine functionality needed to use the context for datastore / memcache / whatever operations.
Thank you to the commenters that helped me get over the first hump!
Your code is missing the part to start a listener. Add this to your code:
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))

Context value changed when passed out of main.go on Google App Engine

There's a problem where I don't know why a context.Context was changed once I pass it to a different package on Google App Engine.
The following code works fine when running on App Engine:
package main
import (
"net/http"
"log"
"google.golang.org/appengine"
)
func main() {
http.HandleFunc("/", myHandler)
appengine.Main()
}
func myHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
account, err := appengine.ServiceAccount(ctx)
if err != nil {
log.Println("[myHandler] error:", err)
} else {
log.Println("[myHandler] ServiceAccount:", account)
}
w.Write([]byte("ok"))
}
I could retrieve the ServiceAccount successfully when accessing /, and everything was good.
However, when I passed the context from main.go to another package, the function call didn't work. The following was added to main.go:
import (
// other stuff
"github.com/adlerhsieh/q_context/handlers"
)
func main() {
http.HandleFunc("/", myHandler)
appengine.Main()
}
func myHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
account, err := appengine.ServiceAccount(ctx)
if err != nil {
log.Println("[myHandler] error:", err)
} else {
log.Println("[myHandler] ServiceAccount:", account)
}
handlers.AnotherFunc(ctx) // <--- added this
w.Write([]byte("ok"))
}
Another package:
package handlers
import (
"log"
"context"
"google.golang.org/appengine"
)
func AnotherFunc(ctx context.Context) {
account, err := appengine.ServiceAccount(ctx)
if err != nil {
log.Println("[AnotherFunc] error:", err)
} else {
log.Println("[AnotherFunc] ServiceAccount:", account)
}
}
When I ran it on App Engine, the log said:
2019/09/04 09:36:30 [myHandler] ServiceAccount: myaccount#gmail.com
2019/09/04 09:36:30 [AnotherFunc] error: not an App Engine context
The function calls are the same, but just in different packages. I dug in the package itself and found that it uses the key here (which leads to here) to setup the context. And here to check whether that value was setup properly. However, that value seem to be modified/changed so that the second function call couldn't get it. Even if I omitted the first function call and went straight to the second one, it still has the same error.
Any idea why context object was modified when passing to another package?
The following is my app.yaml:
runtime: go111
service: default
instance_class: F1
automatic_scaling:
min_idle_instances: 0
max_idle_instances: automatic
min_pending_latency: automatic
max_pending_latency: automatic
max_concurrent_requests: 30
handlers:
- url: /.*
script: auto
login: admin
nobuild_files:
- vendor
env_variables:
ENV: 'dev'
GO111MODULE: 'off'
Here is the GitHub repo link.
Thank you!
It turns out that my code actually worked. It's because of some other operation error.
However, I'll just post the issue that actually caused it so it can help those who have the same issue.
With the new go111 runtime, it treats packages from non-root directory or its subdirectories as a different type of package. This caused the problem with "not an App Engine context". I'll just call it an "outcast" package for now (cause I'm not entirely sure why's that).
For example:
- appengine
- main.go
- handlers
- handlers.go <-- this is a regular package
- appengine
- main.go
- handlers
- handlers.go < -- this is an outcast package
An outcast package would have issues handling context.Context generated from App Engine, as pointed out in my question.
The mechanism of App Engine knowing that the context is created from App Engine, is using a built-in value that can only be retrieved from its internal package (with an un-exported pointer-string key). When passing the context to an outcast package, we can no longer retrieve the value from the context. It's still a mystery for me that why the value disappeared, but it's probably because of some Go compiling mechanism.
The solution would be moving the main.go to the top-level directory in the project, so that there would be no outcast package anywhere.

How to log Stackdriver log messages correlated by trace id using stdout Go 1.11

I'm using Google App Engine Standard Environment with the Go 1.11 runtime. The documentation for Go 1.11 says "Write your application logs using stdout for output and stderr for errors". The migration from Go 1.9 guide also suggests not calling the Google Cloud Logging library directly but instead logging via stdout.
https://cloud.google.com/appengine/docs/standard/go111/writing-application-logs
With this in mind, I've written a small HTTP Service (code below) to experiment logging to Stackdriver using JSON output to stdout.
When I print plain text messages they appear as expected in the Logs Viewer panel under textPayload. When I pass a JSON string they appear under jsonPayload. So far, so good.
So, I added a severity field to the output string and Stackdriver Log Viewer successfully categorizes the message according to the levelled logging NOTICE, WARNING etc.
https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry
The docs say to set the trace identifier to correlate log entries with the originating request log. The trace ID is extracted from the X-Cloud-Trace-Context header set by the container.
Simulate it locally using curl -v -H 'X-Cloud-Trace-Context: 1ad1e4f50427b51eadc9b36064d40cc2/8196282844182683029;o=1' http://localhost:8080/
However, this does not cause the messages to be threaded by request, but instead the trace property appears in the jsonPayload object in the logs. (See below).
Notice that severity has been interpreted as expected and does not appear in the jsonPayload. I had expected the same to happen for trace, but instead it appears to be unprocessed.
How can I achieve nested messages within the original request log message? (This must be done using stdout on Go 1.11 as I do not wish to log directly with the Google Cloud logging package).
What exactly is GAE doing to parse the stdout stream from my running process? (In the setup guide for VMs on GCE there is something about installing an agent program to act as a conduit to Stackdriver logging- is this what GAE has installed?)
app.yaml file looks like this:
runtime: go111
handlers:
- url: /.*
script: auto
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
)
var projectID = "glowing-market-234808"
func parseXCloudTraceContext(t string) (traceID, spanID, traceTrue string) {
slash := strings.Index(t, "/")
semi := strings.Index(t, ";")
equal := strings.Index(t, "=")
return t[0:slash], t[slash+1 : semi], t[equal+1:]
}
func sayHello(w http.ResponseWriter, r *http.Request) {
xTrace := r.Header.Get("X-Cloud-Trace-Context")
traceID, spanID, _ := parseXCloudTraceContext(xTrace)
trace := fmt.Sprintf("projects/%s/traces/%s", projectID, traceID)
warning := fmt.Sprintf(`{"trace":"%s","spanId":"%s", "severity":"WARNING","name":"Andy","age":45}`, trace, spanID)
fmt.Fprintf(os.Stdout, "%s\n", warning)
message := "Hello"
w.Write([]byte(message))
}
func main() {
http.HandleFunc("/", sayHello)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
Output shown in the Log viewer:
...,
jsonPayload: {
age: 45
name: "Andy"
spanId: "13076979521824861986"
trace: "projects/glowing-market-234808/traces/e880a38fb5f726216f94548a76a6e474"
},
severity: "WARNING",
...
I have solved this by adjusting the program to use logging.googleapis.com/trace in place of trace and logging.googleapis.com/spanId in place of spanId.
warning := fmt.Sprintf(`{"logging.googleapis.com/trace":"%s","logging.googleapis.com/spanId":"%s", "severity":"WARNING","name":"Andy","age":45}`, trace, spanID)
fmt.Fprintf(os.Stdout, "%s\n", warning)
It seems that GAE is using the logging agent google-fluentd (a modified version of the fluentd log data colletor.)
See this link for a full explanation.
https://cloud.google.com/logging/docs/agent/configuration#special-fields
[update] June 25th, 2019: I've written a Logrus plugin that will help to thread log entries by HTTP request. It's available under on GitHub https://github.com/andyfusniak/stackdriver-gae-logrus-plugin.
[update]] April 3rd, 2020: I've since switched to using Cloud Run and the Logrus plugin appears to work fine with this platform also.

MEAN RESTful application error handling server crashes

I've just followed a tutorial for creating a simple RESTful api using the MEAN stack from the heroku webpage. What I did was just cloning the repo that contains the sample code, added my mongodb_uri from mLab and then run the app locally (npm start).
It works perfectly, but when I try to make an invalid entry (not providing name and last name to a contact) the express server crashes and the entry is made on my database (which is inconsistent).
I've opened an issue on the github repo but I got no answers, I think that there must be something wrong with the error handling but I don't know what it might be.
Here it is what I get when the server crashes:
ERROR: Invalid user input
/Users/nanop/Desktop/mean-contactlist/node_modules/mongodb/lib/utils.js:98
process.nextTick(function() { throw err; });
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
at ServerResponse.header (/Users/nanop/Desktop/mean-contactlist/node_modules/express/lib/response.js:719:10)
at ServerResponse.send (/Users/nanop/Desktop/mean-contactlist/node_modules/express/lib/response.js:164:12)
at ServerResponse.json (/Users/nanop/Desktop/mean-contactlist/node_modules/express/lib/response.js:250:15)
at /Users/nanop/Desktop/mean-contactlist/server.js:72:23
at /Users/nanop/Desktop/mean-contactlist/node_modules/mongodb/lib/collection.js:421:18
at handleCallback (/Users/nanop/Desktop/mean-contactlist/node_modules/mongodb/lib/utils.js:96:12)
at /Users/nanop/Desktop/mean-contactlist/node_modules/mongodb/lib/collection.js:726:5
at /Users/nanop/Desktop/mean-contactlist/node_modules/mongodb-core/lib/connection/pool.js:428:18
at nextTickCallbackWith0Args (node.js:433:9)
And finally this is the handleError method defined which I think it's ok:
function handleError(res, reason, message, code) {
console.log("ERROR: " + reason);
res.status(code || 500).json({"error": message});
}
This is the repo I refer to: https://github.com/chrisckchang/mean-contactlist

Problems with Go Appengine endpoint tests

I am trying to create an endpoint test when using appengine. Unfortunately, the tests keep failing because of the lack of a schema (and host) within the url used when creating the test *Request struct. When running appengine tests a server is spawned for that specific test that runs on a semi-random port number, which makes it seemingly impossible to define the full url to perform the test on.
The official docs on running tests like this are very sparse and only give half of an example, so I am left scratching my head on how to get this to work.
This is the error that I get from the marked line within the code snippet
Error: Received unexpected error "Post /auth: unsupported protocol scheme \"\""
Test Code
func TestEndpoints_Auth(t *testing.T) {
// input data
account := Account{
AuthProvider: "facebook",
AuthProviderId: "123345456",
}
b, _ := json.Marshal(&account)
reader := bytes.NewReader(b)
// test server
inst, err := aetest.NewInstance(nil)
if !assert.NoError(t, err) { return }
defer inst.Close()
// request
client := http.Client{}
req, err := inst.NewRequest("POST", "/auth", reader)
if !assert.NoError(t, err) { return }
req.Header.Add(AppAuthToken, "foobar")
resp, err := client.Do(req)
if !assert.NoError(t, err) { return } // <=== Where the error occurs
// tests
if !assert.Nil(t, err) { return }
assert.Equal(t, http.StatusCreated, resp.StatusCode)
}
Logs
[GIN-debug] POST /auth --> bitbucket.org/chrisolsen/chriscamp.(*endpoints).Auth-fm (5 handlers)
[GIN-debug] GET /accounts/me --> bitbucket.org/chrisolsen/chriscamp.(*endpoints).GetMyAccount-fm (7 handlers)
INFO 2016-04-22 13:23:39,278 devappserver2.py:769] Skipping SDK update check.
WARNING 2016-04-22 13:23:39,278 devappserver2.py:785] DEFAULT_VERSION_HOSTNAME will not be set correctly with --port=0
WARNING 2016-04-22 13:23:39,345 simple_search_stub.py:1126] Could not read search indexes from c:\users\chris\appdata\local\temp\appengine.testapp\search_indexes
INFO 2016-04-22 13:23:39,354 api_server.py:205] Starting API server at: http://localhost:54461
INFO 2016-04-22 13:23:41,043 dispatcher.py:197] Starting module "default" running at: http://localhost:54462
INFO 2016-04-22 13:23:41,046 admin_server.py:116] Starting admin server at: http://localhost:54466
I was really hoping to perform api blackbox tests, but that seems to be undoable with appengine. Instead I am now performing the tests on the endpoint directly.
req, _ := inst.NewRequest("POST", "/auth", reader)
req.Header.Add(AppAuthToken, "foobar")
resp := httptest.NewRecorder()
handlePostAuth(resp, req)
assert.Equal(t, http.StatusCreated, resp.Code)

Resources