xmr-remote-nodes/cmd/server/cron.go

45 lines
931 B
Go
Raw Normal View History

package server
import (
"fmt"
"os"
"text/tabwriter"
"time"
2024-05-22 19:24:06 +00:00
"xmr-remote-nodes/internal/cron"
"xmr-remote-nodes/internal/database"
"github.com/spf13/cobra"
)
var cronCmd = &cobra.Command{
Use: "cron",
Short: "Print cron tasks",
Long: `Print list of regular cron tasks running on the server.`,
Run: func(cmd *cobra.Command, args []string) {
if err := database.ConnectDB(); err != nil {
panic(err)
}
2024-05-22 19:24:06 +00:00
crons, err := cron.New().Crons()
if err != nil {
fmt.Println(err)
return
}
if len(crons) == 0 {
fmt.Println("No crons found")
return
}
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
fmt.Fprintf(w, "ID\t| Name\t| Run Every\t| Last Run\t| Took Time\n")
for _, cron := range crons {
2024-05-22 19:24:06 +00:00
fmt.Fprintf(w, "%d\t| %s\t| %ds\t| %s\t| %f\n",
cron.ID,
cron.Title,
cron.RunEvery,
time.Unix(cron.LastRun, 0).Format(time.RFC3339),
cron.RunTime,
)
}
w.Flush()
},
}