Golang Revel工作规范:每个月的第一个星期一。

huangapple go评论64阅读模式
英文:

Golang Revel Job spec every 1st monday on every month

问题

我正在使用golang revel,我需要在每个月的第一个星期一运行一个作业,对应的quartz cron规范如下:
0 0 0 ? 1/1 MON#1

但是robfig/cron不接受这样的规范,因此revel/jobs也不行。
有人知道如何解决这个问题吗?(使用revel jobs)

英文:

I'm using golang revel and I need a job to be run every first monday of every month, a quartz cron spec for that would look like this:
0 0 0 ? 1/1 MON#1

But robfig/cron doesn't accept a spec like that, hence neither revel/jobs.
Anyone knows how can I solve that [using revel jobs]?

答案1

得分: 2

对我来说,最简单的解决方案可能是这样的:

func (e SomeStruct) Run() {
    t := time.Now().Local()
    day_num, _ := t.Day()
    if day_num <= 7 {
        fmt.Println("Hello, playground")
    }
}

func init() {
    revel.OnAppStart(func() {
        jobs.Schedule("0 0 * * 1", SomeStruct{})
    })
}

在这个解决方案中,你只需要在每个星期一运行任务,但在任务本身中,先检查是否是第一个星期一,然后再执行任何操作。可能有更好的方法(对Revel不太熟悉),但是通过浏览他们的任务工作方式,这种方法可以工作,并且不会有性能问题。

英文:

To me, the easiest solution would be something like this:

func (e SomeStruct) Run() {
    t := time.Now().Local()
    day_num, _ := t.Day()
    if  day_num &lt;= 7 {
      fmt.Println(&quot;Hello, playground&quot;)
    }
}

func init() {
    revel.OnAppStart(func() {
    jobs.Schedule(&quot;0 0 * * 1&quot;,  SomeStruct{})
})

Where you simply run the job EVERY monday, but in the job itself, check if it's the FIRST monday before you actually do anything. There may be a better way (not very familiar with Revel), but glancing through how their jobs work this would work and it's not like it will be a performance issue.

答案2

得分: 2

要检查每个月的第一个星期一,你可以使用以下代码:

package main

import (
	"fmt"
	"time"
)

func IsFirstMonday() bool {
	t := time.Now().Local()
	if d := t.Day(); 1 <= d && d <= 7 {
		if wd := t.Weekday(); wd == time.Monday {
			return true
		}
	}
	return false
}

func main() {
	fmt.Println(IsFirstMonday())
}

这段代码使用Go语言编写,它通过获取当前时间并检查日期和星期几来确定是否为每个月的第一个星期一。如果是第一个星期一,则函数IsFirstMonday()返回true,否则返回false。在main()函数中,它打印出IsFirstMonday()的结果。

英文:

To check for the first Monday in the month,

package main

import (
	&quot;fmt&quot;
	&quot;time&quot;
)

func IsFirstMonday() bool {
	t := time.Now().Local()
	if d := t.Day(); 1 &lt;= d &amp;&amp; d &lt;= 7 {
		if wd := t.Weekday(); wd == time.Monday {
			return true
		}
	}
	return false
}

func main() {
	fmt.Println(IsFirstMonday())
}

huangapple
  • 本文由 发表于 2014年11月8日 11:14:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/26813341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定