Files
lst/controller/pkg/job_runner.go

32 lines
911 B
Go

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