英文:
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 <= 7 {
fmt.Println("Hello, playground")
}
}
func init() {
revel.OnAppStart(func() {
jobs.Schedule("0 0 * * 1", 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 (
"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())
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论