<p>Hi, a go newbie here. I wanted to schedule some tasks in my go program at specific times. It's pretty straightforward to do it if you have to schedule at regular intervals (every 30 seconds, every 15 seconds etc.) . How about if i needed to execute tasks at times such as 1st minute , 3rd minute, 8th minute. If anyone could help me or point me in the right direction. </p>
<p>Thanks!!</p>
<hr/>**评论:**<br/><br/>colek42: <pre><p>Sounds like a cron job. This may work, or serve an an example. <a href="https://godoc.org/github.com/robfig/cron" rel="nofollow">https://godoc.org/github.com/robfig/cron</a></p></pre>RwKroon: <pre><p>I can recommend that one</p></pre>UniverseCity: <pre><p>Do you know the exact times ahead of time? Or do you just want something like an exponential backoff? It sounds like you're describing the latter, in which case you could have a goroutine with a loop in which you do some work, call <code>time.Sleep(n)</code>, and increment <code>n</code> on each iteration. </p></pre>SchoolPit6: <pre><p>I do know the exact times before hand </p></pre>UniverseCity: <pre><p>Where do you want to run this? You could write a small go service to use <a href="https://cloud.google.com/appengine/docs/standard/go/config/cron" rel="nofollow">AppEngine Cron</a> that'll fire at specific times. </p></pre>SchoolPit6: <pre><p>Will be running on my desktop itself. So can’t use Appengine. </p></pre>therealfakemoot: <pre><p>A cursor google search for "golang cron" yielded <a href="https://godoc.org/github.com/robfig/cron" rel="nofollow">this</a> which seems reasonable enough. Read some documentation on the crontab specification, it allows you to create pretty complex time based patterns for running jobs.</p>
<p>Alternatively, you could roll your own solution. </p>
<blockquote>
<p>if i needed to execute tasks at times such as 1st minute , 3rd minute, 8th minute</p>
</blockquote>
<p>You may be interested to learn about <a href="https://en.wikipedia.org/wiki/Modular_arithmetic" rel="nofollow">modular arithmetic</a>. Golang has a builtin library for fetching the current clock time. Find the hour/minute/day/whatever, modulo it by the interval you want, if the result == 0, run the task.</p>
<p>I'd strongly recommend leveraging an existing scheduling framework, though, to avoid introducing 'novice' level bugs or issues involving timezone/leap second/minute/hour/day/year handling.</p></pre>WikiTextBot: <pre><p><strong>Modular arithmetic</strong></p>
<p>In mathematics, modular arithmetic is a system of arithmetic for integers, where numbers "wrap around" upon reaching a certain value—the modulus (plural moduli). The modern approach to modular arithmetic was developed by Carl Friedrich Gauss in his book Disquisitiones Arithmeticae, published in 1801.</p>
<p>A familiar use of modular arithmetic is in the 12-hour clock, in which the day is divided into two 12-hour periods. If the time is 7:00 now, then 8 hours later it will be 3:00.</p>
<hr/>
<p><sup>[</sup> <a href="https://www.reddit.com/message/compose?to=kittens_from_space" rel="nofollow"><sup>PM</sup></a> <sup>|</sup> <a href="https://reddit.com/message/compose?to=WikiTextBot&message=Excludeme&subject=Excludeme" rel="nofollow"><sup>Exclude</sup> <sup>me</sup></a> <sup>|</sup> <a href="https://np.reddit.com/r/golang/about/banned" rel="nofollow"><sup>Exclude</sup> <sup>from</sup> <sup>subreddit</sup></a> <sup>|</sup> <a href="https://np.reddit.com/r/WikiTextBot/wiki/index" rel="nofollow"><sup>FAQ</sup> <sup>/</sup> <sup>Information</sup></a> <sup>|</sup> <a href="https://github.com/kittenswolf/WikiTextBot" rel="nofollow"><sup>Source</sup></a> <sup>|</sup> <a href="https://www.reddit.com/r/WikiTextBot/wiki/donate" rel="nofollow"><sup>Donate</sup></a> <sup>]</sup>
<sup>Downvote</sup> <sup>to</sup> <sup>remove</sup> <sup>|</sup> <sup>v0.28</sup></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传