feat(controller): update scheduler added
This commit is contained in:
31
controller/pkg/job_runner.go
Normal file
31
controller/pkg/job_runner.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
_ "time/tzdata"
|
||||
|
||||
socketio "github.com/googollee/go-socket.io"
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
func JobScheduler(server *socketio.Server, cronTime string, jobFunc func()) {
|
||||
|
||||
// time zone helper https://nodatime.org/TimeZones
|
||||
loc, err := time.LoadLocation(("America/Chicago"))
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("There was an error getting location: %v", err)
|
||||
server.BroadcastToRoom("/", "logs", "logs", fmt.Sprintf("There was an error getting location: %v", err))
|
||||
}
|
||||
|
||||
crontJob := cron.New(cron.WithLocation(loc)) // cron.WithSeconds()
|
||||
|
||||
// reference for the cron time format https://pkg.go.dev/github.com/robfig/cron@v1.2.0#hdr-CRON_Expression_Format
|
||||
|
||||
// we can use 6 feilds but we need to add this in with cron.WithSeconds into our cron job other wise we can use use the standard 5 fields https://crontab.guru/
|
||||
crontJob.AddFunc(cronTime, jobFunc)
|
||||
|
||||
crontJob.Start()
|
||||
}
|
||||
Reference in New Issue
Block a user