diff --git a/cmd/server/admin.go b/cmd/server/admin.go index 71ec906..5fcf365 100644 --- a/cmd/server/admin.go +++ b/cmd/server/admin.go @@ -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) diff --git a/cmd/server/cron.go b/cmd/server/cron.go new file mode 100644 index 0000000..f1dde84 --- /dev/null +++ b/cmd/server/cron.go @@ -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() + }, +} diff --git a/handler/response.go b/handler/response.go index f29247f..aae10e0 100644 --- a/handler/response.go +++ b/handler/response.go @@ -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, - }) -} diff --git a/handler/routes.go b/handler/routes.go index c0cbaf6..cf0591f 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -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) diff --git a/internal/repo/cron.go b/internal/repo/cron.go index 94ceee7..82ac724 100644 --- a/internal/repo/cron.go +++ b/internal/repo/cron.go @@ -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 { - 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 + var tasks []Cron + err := repo.db.Select(&tasks, query) + return tasks, err } func (repo *CronRepo) queueList() ([]Cron, error) {