※This is an enhanced version※
JobRunner is framework for performing work asynchronously, outside of the request flow. It comes with cron to schedule and queue job functions for processing at specified time.
It includes a live monitoring of current schedule and state of active jobs that can be outputed as JSON and it support get job execution history by go channel.
go get github.com/magicLian/jobrunner
All jobs are processed outside of the request flow
- Support time location in scheduling job
- Support user-defined job name
- Support task status real-time query
- Support to obtain task execution history records through Go
channel
jobrunner.Schedule("* */5 * * * *", DoSomething{}, "DoSomethingJob") // every 5min do something
jobrunner.Schedule("@every 1h30m10s", ReminderEmails{}, "ReminderEmailsJob")
jobrunner.Schedule("@midnight", DataStats{}, "DataStatsJob") // every midnight do this..
jobrunner.Every(16*time.Minute, CleanS3{}, "CleanS3Job") // evey 16 min clean...
jobrunner.In(10*time.Second, WelcomeEmail{}, "WelcomeEmailJob") // one time job. starts after 10sec
jobrunner.Now(NowDo{}, "NowJob") // do the job as soon as it's triggered
package main
import "github.com/magicLian/jobrunner"
func main() {
if err := os.Setenv("ZONEINFO", "/tzdata/data.zip"); err != nil {
panic(err)
}
loc,err := time.loadLocation("Asia/Tokyo")
if err != nil {
panic(err)
}
jobrunner.Start(loc, false)
jobrunner.Schedule("@every 5s", ReminderEmails{}, "reminderEmails")
}
// Job Specific Functions
type ReminderEmails struct {
// filtered
}
// ReminderEmails.Run() will get triggered automatically.
func (e ReminderEmails) Run() {
// Queries the DB
// Sends some email
fmt.Printf("Every 5 sec send reminder emails \n")
}
package main
import (
"fmt"
"time"
"github.com/gin-gonic/gin"
"github.com/magicLian/jobrunner"
)
func main() {
routes := gin.Default()
// Resource to return the JSON data
routes.GET("/jobrunner/json", JobJson)
jobrunner.Start(nil, false)
jobrunner.Every(10*time.Minute, DoSomeThing{}, "DoSomeThing")
routes.Run(":8080")
}
type DoSomeThing struct {
}
func (d DoSomeThing) Run() {
fmt.Printf("Start to do something")
}
func JobJson(c *gin.Context) {
// returns a map[string]interface{} that can be marshalled as JSON
c.JSON(200, jobrunner.StatusJson())
}
func main() {
...
jobrunner.Start(nil, true)
go getCronExecutionResults()
jobrunner.Every(10*time.Minute, DoSomeThing{}, "DoSomeThing")
...
}
func getCronExecutionResults(){
for {
select {
case record := <-jobrunner.JobsExecutionStatusChan:
fmt.Printf("Job exection status result:[%v]", record)
}
}
}
- revel jobs module - Origin of JobRunner
- robfig cron v3 - github.com/robfig/cron/v3
MIT