Skip to content

Commit

Permalink
feat(cmd/utils/cron): Add simple cron util
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejsika committed Nov 24, 2024
1 parent ee6c7f8 commit e1c46d2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/sikalabs/tergum/cmd/root"
_ "github.com/sikalabs/tergum/cmd/server"
_ "github.com/sikalabs/tergum/cmd/utils"
_ "github.com/sikalabs/tergum/cmd/utils/cron"
_ "github.com/sikalabs/tergum/cmd/utils/pause"
_ "github.com/sikalabs/tergum/cmd/version"
"github.com/spf13/cobra"
Expand Down
53 changes: 53 additions & 0 deletions cmd/utils/cron/cron.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cron

import (
"log"
"os"
"os/exec"
"strings"

robfig_cron "github.com/robfig/cron/v3"

parentcmd "github.com/sikalabs/tergum/cmd/utils"
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "cron",
Short: "cron <cron-expression> <command> [args...]",
Args: cobra.MinimumNArgs(2),
Run: func(c *cobra.Command, args []string) {
cron(args)
},
}

func init() {
parentcmd.Cmd.AddCommand(Cmd)
}

func cron(args []string) {
cronExpression := args[0]
command := args[1]
args = args[2:]

c := robfig_cron.New()
_, err := c.AddFunc(cronExpression, func() {
log.Printf("Executing command: %s %s", command, strings.Join(args, " "))
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Printf("Error executing command: %v", err)
}
})
if err != nil {
log.Fatalf("Failed to add cron job: %v", err)
}

log.Printf("Cron scheduler started with expression: %s, command: %s %s", cronExpression, command, strings.Join(args, " "))
c.Start()

// Keep the program running
select {}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/olekukonko/tablewriter v0.0.5
github.com/ondrejsika/gosendmail v0.4.0
github.com/ondrejsika/notion-backup v0.1.0
github.com/robfig/cron/v3 v3.0.1
github.com/rs/zerolog v1.25.0
github.com/spf13/cobra v1.2.1
gopkg.in/yaml.v2 v2.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
Expand Down

0 comments on commit e1c46d2

Please sign in to comment.