diff --git a/.includeControls b/.includeControls index ef4852a..f0e984b 100644 --- a/.includeControls +++ b/.includeControls @@ -1,5 +1,6 @@ lstWrapper/publish controller/lst_ctl.exe +controller/.env-example scripts/update-controller-bumpBuild.ps1 scripts/update-controller-server.ps1 scripts/update-controller-zip.ps1 diff --git a/controller/go.mod b/controller/go.mod index b1a4046..6266302 100644 --- a/controller/go.mod +++ b/controller/go.mod @@ -31,6 +31,8 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/robfig/cron v1.2.0 // indirect + github.com/robfig/cron/v3 v3.0.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect golang.org/x/arch v0.8.0 // indirect diff --git a/controller/go.sum b/controller/go.sum index dbfe423..ea0ffb7 100644 --- a/controller/go.sum +++ b/controller/go.sum @@ -55,6 +55,10 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ= +github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k= +github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E= +github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/controller/internal/bot/bot.go b/controller/internal/bot/bot.go new file mode 100644 index 0000000..1365f35 --- /dev/null +++ b/controller/internal/bot/bot.go @@ -0,0 +1,5 @@ +package bot + +func main() { + +} diff --git a/controller/main.go b/controller/main.go index 78fe743..3960067 100644 --- a/controller/main.go +++ b/controller/main.go @@ -6,11 +6,13 @@ import ( "net/http" "os" "path/filepath" + "strings" "time" "github.com/gin-gonic/gin" socketio "github.com/googollee/go-socket.io" "github.com/joho/godotenv" + "lst.net/pkg" ) func main() { @@ -90,13 +92,42 @@ func main() { registerUpdateChannel(server) // Broadcast logs to room - go func() { - for i := 0; ; i++ { - time.Sleep(2 * time.Second) - msg := fmt.Sprintf("Log line %d @ %s", i, time.Now().Format(time.RFC3339)) - server.BroadcastToRoom("/", "logs", "logs", msg) + // go func() { + // for i := 0; ; i++ { + // time.Sleep(2 * time.Second) + // msg := fmt.Sprintf("Log line %d @ %s", i, time.Now().Format(time.RFC3339)) + // server.BroadcastToRoom("/", "logs", "logs", msg) + // } + // }() + // go pkg.JobScheduler(server, "0 9 * * *", func() { + // fmt.Println("This is the time checker") + // server.BroadcastToRoom("/", "logs", "logs", "Just ran at 9:00 for example") + // }) + + // schedule the update checker + + go pkg.JobScheduler(server, "0 8 * * 1-4", func() { + host, err := os.Hostname() + if err != nil { + server.BroadcastToRoom("/", "update", "updateLogs", "Could not retrieve hostname") + return } - }() + + if strings.Contains(host, "VMS") || strings.Contains(host, "vms") { + fmt.Println("On a server ok to start the update job") + server.BroadcastToRoom("/", "update", "updateLogs", "On a server ok to start the update job") + updates := UpdateApp(server) + + fmt.Println("📦 Starting update loop") + for msg := range updates { // this will block until the channel closes + fmt.Println(msg) + server.BroadcastToRoom("/", "logs", "logs", msg) + } + + fmt.Println("✅ Update process finished") + } + + }) // Broadcast errors to room go func() { diff --git a/controller/pkg/job_runner.go b/controller/pkg/job_runner.go new file mode 100644 index 0000000..f6926c3 --- /dev/null +++ b/controller/pkg/job_runner.go @@ -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() +}