【发布时间】:2015-08-04 00:51:18
【问题描述】:
我正在 Google App Engine 上编写一个应用程序,并且我已经能够编写在本地运行并将数据放入本地数据存储区的代码。但是,当我将代码部署到谷歌服务器时,没有数据放入数据存储区。我的主要指标是:一,当我进入开发者控制台时,我被告知没有条目,二,当我运行获取数据的页面时,没有返回任何内容。
我是否需要在 app.yaml 或开发者控制台中定义新的 Kinds 才能在生产数据存储中工作?
这是我在数据存储中的 put 和 get 方法。同样,在测试环境中工作,在生产环境中不工作,一切都编译得很好。
package tweetdata
import (
"net/url"
"time"
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"github.com/ChimeraCoder/anaconda"
)
const linkTweetKind string = "LinkTweet"
const tweetKey string = "Tweets"
const tweetKeyID string = "default_tweetstore"
//LinkTweet contains the address extracted from a tweet and the original tweet
type LinkTweet struct {
Address *url.URL
Tweet anaconda.Tweet
}
//StoreTweet is a struct used for storing a tweet in the datastore
type StoreTweet struct {
Address string
Text string
TweetID int64
CreatedTime time.Time
Retweets int
Favorites int
}
//TweetScore is a struct that shows the relative score of an address based on
// it's populatrity
type TweetScore struct {
Address *url.URL
score int
lastActive time.Time
}
//WriteLinkTweet writes a given Tweet to the datastore
func WriteLinkTweet(tweet LinkTweet, c context.Context) error {
log.Infof(c, "Putting Tweet into datastore: %v", tweet.Tweet.Id)
key := datastore.NewIncompleteKey(c, linkTweetKind, getTweetKey(c))
created, _ := tweet.Tweet.CreatedAtTime()
store := &StoreTweet{Address: tweet.Address.String(),
Text: tweet.Tweet.Text,
TweetID: tweet.Tweet.Id,
CreatedTime: created,
Retweets: tweet.Tweet.RetweetCount,
Favorites: tweet.Tweet.FavoriteCount,
}
err := datastore.RunInTransaction(c, func(c context.Context) error {
_, err := datastore.Put(c, key, store)
log.Errorf(c, "Failed to write LinkTweet to datastore. %v", err.Error())
return err
}, nil)
return err
}
//GetAllNewTweets queries the datastore and gets all tweets created since the last
// time given
func GetAllNewTweets(since time.Time, c context.Context) []StoreTweet {
q := datastore.NewQuery(linkTweetKind).Ancestor(getTweetKey(c)).Filter("CreatedTime >=", since)
out := make([]StoreTweet, 0, 15)
q.GetAll(c, &out)
return out
}
// guestbookKey returns the key used for all guestbook entries.
func getTweetKey(c context.Context) *datastore.Key {
// The string "default_guestbook" here could be varied to have multiple guestbooks.
return datastore.NewKey(c, tweetKey, tweetKeyID, 0, nil)
}
【问题讨论】:
-
发布您用于(尝试)将实体写入数据存储区的代码。另外,您看到(或未看到)什么让您相信没有任何东西可以得救?
-
任何非零
err的日志记录? -
对于您的日志记录,您需要使用来自有效请求上下文(appengine.Context 中的文档)的日志记录函数 - Debugf、Infof、Errorf、Warningf、Criticalf 来登录 App Engine .在您的开发服务器中,您将在终端输出中看到这些日志。在生产 App Engine 中,这些日志存储在您的项目的 Google Cloud Console 中的 Monitoring > Logs 下。
-
发布 getTweetKey 或将父级设置为 nil 并重试。
-
由于 Google App Engine 没有提供标准的 http.Transport,因此有必要告诉 Anaconda 使用不同的客户端上下文。我假设您正在这样做,否则它可能也无法在开发服务器上运行。
标签: google-app-engine go google-cloud-datastore