Upgraded GoCron; web ui viewer for viewing cron jobs
Some checks failed
ci/woodpecker/push/go-lint Pipeline failed
ci/woodpecker/push/publish-docs Pipeline was successful

This commit is contained in:
2025-12-26 22:58:08 -06:00
parent a95f903e72
commit 3a038eeab4
7 changed files with 150 additions and 38 deletions

View File

@@ -169,6 +169,8 @@ func (opts *ConfigOpts) ParseConfigurationFile() *ConfigOpts {
logging.ExitWithMSG("No cron fields detected in any command lists", 1, nil)
}
unmarshalConfigIntoStruct(backyKoanf, "goCron", &opts.GoCron, opts.Logger)
if err := processCmds(opts); err != nil {
logging.ExitWithMSG(err.Error(), 1, &opts.Logger)
}
@@ -425,6 +427,9 @@ func generateFileFetchErrorString(file, fileType string, err error) string {
func validateCommandLists(opts *ConfigOpts) {
var cmdNotFoundSliceErr []error
for cmdListName, cmdList := range opts.CmdConfigLists {
if cmdList.Name == "" {
cmdList.Name = cmdListName
}
// if cron is enabled and cron is not set, delete the list
if opts.cronEnabled && strings.TrimSpace(cmdList.Cron) == "" {
opts.Logger.Debug().Str("cron", "enabled").Str("list", cmdListName).Msg("cron not set, deleting list")

View File

@@ -6,30 +6,73 @@ package backy
import (
"fmt"
"net/http"
"strings"
"time"
"git.andrewnw.xyz/CyberShell/backy/pkg/logging"
"github.com/go-co-op/gocron"
"github.com/go-co-op/gocron-ui/server"
"github.com/go-co-op/gocron/v2"
)
var defaultPort = 8888
func (opts *ConfigOpts) Cron() {
s := gocron.NewScheduler(time.Local)
s.TagsUnique()
s, _ := gocron.NewScheduler(gocron.WithLocation(time.Local))
defer func() { _ = s.Shutdown() }()
opts.Logger.Info().Msg("Starting cron mode...")
s.Start()
cmdLists := opts.CmdConfigLists
for _, config := range cmdLists {
cron := strings.TrimSpace(config.Cron)
if cron != "" {
opts.Logger.Info().Str("Scheduling cron list", config.Name).Str("Time", cron).Send()
_, err := s.CronWithSeconds(cron).Tag(config.Name).Do(func(cron string) {
opts.RunListConfig(cron)
}, cron)
job, err := s.NewJob(
gocron.CronJob(cron, opts.GoCron.UseSeconds),
gocron.NewTask(
func(cronStr string) {
opts.RunListConfig(cronStr)
},
cron,
),
gocron.WithName(config.Name))
if err != nil {
logging.ExitWithMSG(fmt.Sprintf("error: %v", err), 1, &opts.Logger)
}
nextRun, _ := job.NextRun()
opts.Logger.Info().Str("Scheduling cron list", config.Name).Str("Time", cron).Str("Next run", nextRun.String()).Send()
}
}
opts.Logger.Info().Msg("Starting cron mode...")
s.StartBlocking()
// start the web UI server
if opts.GoCron.BindAddress == "" {
if opts.GoCron.Port == 0 {
opts.GoCron.BindAddress = ":8888"
} else {
opts.GoCron.BindAddress = fmt.Sprintf(":%d", opts.GoCron.Port)
}
} else {
if opts.GoCron.Port != 0 {
opts.GoCron.BindAddress = fmt.Sprintf("%s:%d", opts.GoCron.BindAddress, opts.GoCron.Port)
} else {
opts.GoCron.BindAddress = fmt.Sprintf("%s:%d", opts.GoCron.BindAddress, defaultPort)
}
}
// consensus := externalip.DefaultConsensus(nil, nil)
// By default Ipv4 or Ipv6 is returned,
// use the function below to limit yourself to IPv4,
// or pass in `6` instead to limit yourself to IPv6.
// consensus.UseIPProtocol(4)
// Get your IP,
// which is never <nil> when err is <nil>.
// ip, err := consensus.ExternalIP()
// if err == nil {
// fmt.Println(ip.String()) // print IPv4/IPv6 in string format
// }
srv := server.NewServer(s, opts.GoCron.Port)
// srv := server.NewServer(scheduler, 8080, server.WithTitle("My Custom Scheduler")) // with custom title if you want to customize the title of the UI (optional)
opts.Logger.Info().Msgf("GoCron UI available at http://%s", opts.GoCron.BindAddress)
opts.Logger.Fatal().Msg(http.ListenAndServe(opts.GoCron.BindAddress, srv.Router).Error())
select {} // wait forever
}

View File

@@ -170,6 +170,12 @@ type (
Type string `yaml:"type"`
}
GoCronOpts struct {
BindAddress string `yaml:"bindAddress"`
UseSeconds bool `yaml:"useSeconds"`
Port int `yaml:"port"`
}
ConfigOpts struct {
// Cmds holds the commands for a list.
// Key is the name of the command,
@@ -183,6 +189,8 @@ type (
// key is the host.
Hosts map[string]*Host `yaml:"hosts"`
GoCron GoCronOpts `yaml:"goCron:"`
Logger zerolog.Logger
// Global log level