doutizha7526 2018-06-01 21:32
浏览 69

将App Engine写入Google Datastore挂起

I'm writing a simple App Engine program using Go. I need to write some data to Google DataStore. The program consistently hangs when I attempt to .Put the data into storage. This has bedeviled me this entire week.

At some point, I managed to write the data to DataStore, which I realized only once I discovered how to view the uploaded data within Cloud Console. I've changed the code a lot since then, and now I can no longer write any data. It hangs every time.

Logs show nothing helpful. Only a notice that the process has timed out. No logs written from the program, only from the system.

Process terminated because the request deadline was exceeded. (Error code 123)

I've tried changing import statements, using this link, but it failed to compile, so I went back to this link which compiles without issue.

I considered the possibility that I had somehow badly misconfigured (disfigured, perhaps?) my development environment, so I went to a clean machine and carefully went through a new install. It still hangs.

My app.yaml file:

runtime: go
    api_version: go1

handlers:
    - url: /.*
      script: _go_app

An index.yaml file I added:

indexes:

    - kind: Bacon
        properties:
    - name: YCode
        direction: asc
    - name: URL
    - name: Owner
    - name: Location

Here is my code:

package main

    import (
        "fmt"
        "log"
        "net/http"
        "google.golang.org/appengine/datastore"
        "google.golang.org/appengine" 
    )

     type Bacon struct {
        YCode string
        URL string
        Owner string
        Location string
    } 

    func main() {
        http.HandleFunc("/", indexHandler)
        appengine.Main() 
    }

    func indexHandler(w http.ResponseWriter, r *http.Request) {

        ctx := appengine.NewContext(r)
        projectID := "fake-for-discussion-thread-787987"

        client, err := datastore.NewClient(ctx, projectID)
        if err != nil {
            log.Fatalf("Failed to create client: %v", err)
            return
        } 

        kind := "Bacon"
        name := "3"
        baconKey := datastore.NameKey(kind, name, nil)

        //Make bacon
        bacon := Bacon{
            YCode: "1",
            URL: "http://www.safeway.com",
            Owner: "Bob",
            Location: "Deli",
        }

        // I've confirmed that this is where it hangs every time.  
        // It doesn't even capture my silly fatal log entry. 
        // It just times out and finally sends a 500 Server Error.
        if _, err := client.Put(ctx, baconKey, &bacon); err != nil {
            log.Fatalf("Failed to save my Bacon: %v", err)
        }

        client.Close()

    }

Many thanks for any help you can offer!

  • 写回答

1条回答 默认 最新

  • douwei2713 2018-06-02 16:20
    关注

    Okay! So this really drove me up a wall, but I managed to crack it this morning. I had so much difficulty finding online documentation that I want to make sure I put in my (now working) code so others may be spared the pain.

    Notably, I've started to suspect that the Go documentation/examples can send you down the wrong path if you're deploying to App Engine. App Engine doesn't like some of the core Go libraries, etc. That can become very confusing when deploying to App Engine.

    Also note that I put a lot of my lessons-learned in the comments from my initial question. I'm making this entry so I can properly post my fixed App Engine-specific code.

        //Modified to work on App Engine. May not work right locally, etc.
    
        package main
    
        import (
            "fmt"
            "log"
            "net/http"
            "google.golang.org/appengine/datastore"
            "google.golang.org/appengine" 
        )
    
         type Bacon struct {
            YCode string
            URL string
            Owner string
            Location string
        } 
    
        func main() {
            http.HandleFunc("/", indexHandler)
            appengine.Main() 
        }
    
        func indexHandler(w http.ResponseWriter, r *http.Request) {
    
            //This gives a context that makes App Engine happy      
            ctx := appengine.NewContext(r)
    
            //NOT NEEDED (Apparently, App Engine knows who you are. Creepy.)    
            //projectID := "fake-for-discussion-thread-787987" 
    
            //NOT NEEDED (And may actually cause you great woe!)
            //client, err := datastore.NewClient(ctx, projectID)
            //if err != nil {
            //  log.Fatalf("Failed to create client: %v", err)
            //  return
            //} 
    
            //Turns out that these aren't needed, either. 
            //kind := "Bacon"
            //name := "3"
    
            //Note the changes from my original code, including the
            //change from NameKey to NewIncompleteKey and the
            //hard-coding of Bacon in the arguments. Alternatively, you might
            //use a variable, depending on your needs. I just need bacon.
            //This causes the entity you create in Google DataStore 
            //to have a "Kind" of "Bacon"
            baconKey := datastore.NewIncompleteKey(ctx, "Bacon", nil)
    
            //Make bacon
            bacon := Bacon{
                YCode: "1",
                URL: "http://www.safeway.com",
                Owner: "Bob",
                Location: "Deli",
            }
    
            // Note changes here. No more client, just a call to datastore.Put.
            // Now it works!  One caveat: I'm still trying to figure out why it writes 
            // my data twice. But, hey!  At least it's writing!
            if _, err := datastore.Put(ctx, baconKey, &bacon); err != nil {
                log.Fatalf("Failed to save my Bacon: %v", err)
            }
    
            //No client, so no need to close.
            //client.Close()
    
        }
    

    Cheers!

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教