Golang Revel Job spec every 1st monday on every month

767 views Asked by At

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]?

2

There are 2 answers

0
dave On BEST ANSWER

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.

0
peterSO On

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())
}