Ability to list cron from CLI #2

Since `tbl_cron` will contain a few rows (for now just 1),
cron list function call is not using any arguments.

This commit also remove cron http handler and endpoint.
This commit is contained in:
ditatompel 2024-05-18 20:13:00 +07:00
parent 120ba51c87
commit 30b37b922f
No known key found for this signature in database
GPG key ID: 31D3D06D77950979
5 changed files with 52 additions and 99 deletions

View file

@ -41,6 +41,7 @@ var AdminCmd = &cobra.Command{
func init() {
cmd.Root.AddCommand(serveCmd)
cmd.Root.AddCommand(importCmd)
cmd.Root.AddCommand(cronCmd)
cmd.Root.AddCommand(probersCmd)
probersCmd.AddCommand(listProbersCmd)
probersCmd.AddCommand(addProbersCmd)

45
cmd/server/cron.go Normal file
View file

@ -0,0 +1,45 @@
package server
import (
"fmt"
"os"
"text/tabwriter"
"time"
"xmr-remote-nodes/internal/database"
"xmr-remote-nodes/internal/repo"
"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)
}
cronRepo := repo.NewCron(database.GetDB())
crons, err := cronRepo.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 {
fmt.Fprintf(w, "%d\t| %s\t| %d\t| %s\t| %f\n",
cron.Id,
cron.Title,
cron.RunEvery,
time.Unix(cron.LastRun, 0).Format(time.RFC3339),
cron.RunTime,
)
}
w.Flush()
},
}

View file

@ -256,32 +256,3 @@ func ProcessJob(c *fiber.Ctx) error {
"data": nil,
})
}
func Crons(c *fiber.Ctx) error {
cronRepo := repo.NewCron(database.GetDB())
query := repo.CronQueryParams{
RowsPerPage: c.QueryInt("limit", 10),
Page: c.QueryInt("page", 1),
SortBy: c.Query("sort_by", "id"),
SortDirection: c.Query("sort_direction", "desc"),
Title: c.Query("title"),
Description: c.Query("description"),
IsEnabled: c.QueryInt("is_enabled", -1),
CronState: c.QueryInt("cron_state", -1),
}
crons, err := cronRepo.Crons(query)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"status": "error",
"message": err.Error(),
"data": nil,
})
}
return c.JSON(fiber.Map{
"status": "ok",
"message": "Success",
"data": crons,
})
}

View file

@ -12,7 +12,6 @@ func AppRoute(app *fiber.App) {
func V1Api(app *fiber.App) {
v1 := app.Group("/api/v1")
v1.Get("/crons", CookieProtected, Crons)
v1.Get("/nodes", MoneroNodes)
v1.Post("/nodes", AddNode)
v1.Get("/nodes/id/:id", MoneroNode)

View file

@ -4,15 +4,13 @@ import (
"fmt"
"log/slog"
"math"
"slices"
"strings"
"time"
"xmr-remote-nodes/internal/database"
)
type CronRepository interface {
RunCronProcess()
Crons(q CronQueryParams) (CronTasks, error)
Crons() ([]Cron, error)
}
type CronRepo struct {
@ -70,73 +68,12 @@ func (repo *CronRepo) RunCronProcess() {
}
}
type CronQueryParams struct {
Title string
Description string
IsEnabled int
CronState int
RowsPerPage int
Page int
SortBy string
SortDirection string
}
func (repo *CronRepo) Crons() ([]Cron, error) {
query := `SELECT id, title, slug, description, run_every, last_run, next_run, run_time, cron_state, is_enabled FROM tbl_cron`
type CronTasks struct {
TotalRows int `json:"total_rows"`
RowsPerPage int `json:"rows_per_page"`
Items []*Cron `json:"items"`
}
func (repo *CronRepo) Crons(q CronQueryParams) (CronTasks, error) {
queryParams := []interface{}{}
whereQueries := []string{}
where := ""
if q.Title != "" {
whereQueries = append(whereQueries, "title LIKE ?")
queryParams = append(queryParams, "%"+q.Title+"%")
}
if q.Description != "" {
whereQueries = append(whereQueries, "description LIKE ?")
queryParams = append(queryParams, "%"+q.Description+"%")
}
if q.IsEnabled != -1 {
whereQueries = append(whereQueries, "is_enabled = ?")
queryParams = append(queryParams, q.IsEnabled)
}
if q.CronState != -1 {
whereQueries = append(whereQueries, "cron_state = ?")
queryParams = append(queryParams, q.CronState)
}
if len(whereQueries) > 0 {
where = "WHERE " + strings.Join(whereQueries, " AND ")
}
tasks := CronTasks{}
queryTotalRows := fmt.Sprintf("SELECT COUNT(id) FROM tbl_cron %s", where)
err := repo.db.QueryRow(queryTotalRows, queryParams...).Scan(&tasks.TotalRows)
if err != nil {
var tasks []Cron
err := repo.db.Select(&tasks, query)
return tasks, err
}
queryParams = append(queryParams, q.RowsPerPage, (q.Page-1)*q.RowsPerPage)
allowedSort := []string{"id", "run_every", "last_run", "next_run", "run_time"}
sortBy := "id"
if slices.Contains(allowedSort, q.SortBy) {
sortBy = q.SortBy
}
sortDirection := "DESC"
if q.SortDirection == "asc" {
sortDirection = "ASC"
}
query := fmt.Sprintf("SELECT id, title, slug, description, run_every, last_run, next_run, run_time, cron_state, is_enabled FROM tbl_cron %s ORDER BY %s %s LIMIT ? OFFSET ?", where, sortBy, sortDirection)
err = repo.db.Select(&tasks.Items, query, queryParams...)
if err != nil {
return tasks, err
}
tasks.RowsPerPage = q.RowsPerPage
return tasks, nil
}
func (repo *CronRepo) queueList() ([]Cron, error) {