<p>When my appengine app starts, I want to check to make sure the database has the correct tables and build them if not. How do i log this if there is no request coming in since it happens in the init() function? All appengine libraries require a context, but I dont have an incoming request to make the context out of.</p>
<hr/>**评论:**<br/><br/>sdevoid: <pre><p>I am not very familiar with the appengine library, but the log functions take a standard <code>golang.org/x/net/context.Context</code> object. So you can just use the <code>Background()</code> <a href="https://godoc.org/golang.org/x/net/context#Background" rel="nofollow">function</a> to get one. If you look at the <a href="https://github.com/golang/appengine/blob/master/appengine.go#L28" rel="nofollow">source</a> of the <code>NewContext(req)</code> function this is exactly what it does.</p>
<p>Digging into the appengine's log implementation, it appears that there are some context-bound settings that you would have to apply if you wanted to override the log function. <a href="https://github.com/golang/appengine/blob/master/internal/api_common.go#L75" rel="nofollow">[1]</a> Although I would first try to get it working without digging into those details.</p></pre>TheMerovius: <pre><p>Don't. You should do <em>all</em> your work in response to a request. Always. That's pretty much the point of AppEngine and if you don't comply with this rule it's going to bite you in the long run. Same goes for spawning goroutines - they won't survive your request, so don't spawn any goroutines that need to live longer.</p>
<p>If you need that kind of initialization (you probably don't. Just use datastore), you need to add an extra request or something to set it up.</p>
<p>I like AppEngine for that reason - you <em>need</em> to write your App in a way that scales and is stateless, which is a good thing for frontends.</p></pre>russross: <pre><p>It's been a while since I've used app engine, but as I recall an instance is sent a special request at startup time. You should do your setup in a request handler for a special URL instead of doing it in an init function. App engine is set up so that you must always have a Context object to do anything.</p>
<p>See this page for an overview; the special request will be an empty GET request to /_ah/start</p>
<p><a href="https://cloud.google.com/appengine/docs/go/an-overview-of-app-engine" rel="nofollow">https://cloud.google.com/appengine/docs/go/an-overview-of-app-engine</a></p></pre>russross: <pre><p>One other note: if I remember right, your instance will not be given any normal requests until the /_ah/start handler returns, so you don't need to worry about race conditions between startup and normal handling within a single instance. However, multiple instances could be spinning up at the same time, so you should take care for race conditions between instances in your initialization code.</p></pre>madman2233: <pre><p>Awesome, this is what i was looking for. I can move almost everything out of init and into the handler for /_ah/start.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传