AppEngine logging, how to do it without a request context?

blov · · 494 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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&#39;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&#39;t. You should do <em>all</em> your work in response to a request. Always. That&#39;s pretty much the point of AppEngine and if you don&#39;t comply with this rule it&#39;s going to bite you in the long run. Same goes for spawning goroutines - they won&#39;t survive your request, so don&#39;t spawn any goroutines that need to live longer.</p> <p>If you need that kind of initialization (you probably don&#39;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&#39;s been a while since I&#39;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&#39;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

494 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传