<p>Hey all, </p>
<p>I've got a binary that syncs data from one source to another. I'd like this app to run continuously as I have jobs scheduled to sync the data every so often. To do this I'm using the <a href="https://github.com/roylee0704/gron" rel="nofollow">https://github.com/roylee0704/gron</a> package. The problem is, that once the initial sync of data is completed, main closes, and application quits. </p>
<p>What's the correct way to leave the application running until the process is killed? </p>
<p>Thanks!</p>
<hr/>**评论:**<br/><br/>tcrypt: <pre><p>I like to model it so the long running process is a struct with <code>Start()</code> and <code>Stop()</code> methods. In cmd/main.go you call them with a <code>signal.Notify</code> in between:</p>
<pre><code>// Setup and start
syncher := NewSyncer()
syncher.Start()
// Wait for signal
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
// Shutdown cleanly
syncher.Stop()
</code></pre></pre>qaisjp: <pre><p>This is the correct answer.</p></pre>hell_0n_wheel: <pre><blockquote>
<p>The problem is, that once the initial sync of data is completed, main closes, and application quits.</p>
</blockquote>
<p>Generally, that's the expected behavior of anything you're going to control with <code>cron</code>. If your application never quit, why would you need to schedule jobs?</p>
<p>If you really, absolutely, truly must keep your application up at all times:</p>
<ul>
<li>put an endless event loop in main(), or have it wait at some sort of barrier (such as a Group)</li>
<li>launch your application with runit or some other service manager</li>
</ul></pre>md2perpe: <pre><p>Try to just import <code>sync</code> and add the two following lines at the end of <code>main</code>:</p>
<pre><code>var wg sync.WorkGroup
wg.Add(1)
wg.Wait()
</code></pre></pre>lesmond: <pre><p>An elegant solution is just to add a select{} to the end of main</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传