mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2025-01-09 20:39:57 +00:00
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:
parent
120ba51c87
commit
30b37b922f
5 changed files with 52 additions and 99 deletions
|
@ -41,6 +41,7 @@ var AdminCmd = &cobra.Command{
|
||||||
func init() {
|
func init() {
|
||||||
cmd.Root.AddCommand(serveCmd)
|
cmd.Root.AddCommand(serveCmd)
|
||||||
cmd.Root.AddCommand(importCmd)
|
cmd.Root.AddCommand(importCmd)
|
||||||
|
cmd.Root.AddCommand(cronCmd)
|
||||||
cmd.Root.AddCommand(probersCmd)
|
cmd.Root.AddCommand(probersCmd)
|
||||||
probersCmd.AddCommand(listProbersCmd)
|
probersCmd.AddCommand(listProbersCmd)
|
||||||
probersCmd.AddCommand(addProbersCmd)
|
probersCmd.AddCommand(addProbersCmd)
|
||||||
|
|
45
cmd/server/cron.go
Normal file
45
cmd/server/cron.go
Normal 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()
|
||||||
|
},
|
||||||
|
}
|
|
@ -256,32 +256,3 @@ func ProcessJob(c *fiber.Ctx) error {
|
||||||
"data": nil,
|
"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,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
|
@ -12,7 +12,6 @@ func AppRoute(app *fiber.App) {
|
||||||
func V1Api(app *fiber.App) {
|
func V1Api(app *fiber.App) {
|
||||||
v1 := app.Group("/api/v1")
|
v1 := app.Group("/api/v1")
|
||||||
|
|
||||||
v1.Get("/crons", CookieProtected, Crons)
|
|
||||||
v1.Get("/nodes", MoneroNodes)
|
v1.Get("/nodes", MoneroNodes)
|
||||||
v1.Post("/nodes", AddNode)
|
v1.Post("/nodes", AddNode)
|
||||||
v1.Get("/nodes/id/:id", MoneroNode)
|
v1.Get("/nodes/id/:id", MoneroNode)
|
||||||
|
|
|
@ -4,15 +4,13 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"math"
|
"math"
|
||||||
"slices"
|
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
"xmr-remote-nodes/internal/database"
|
"xmr-remote-nodes/internal/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CronRepository interface {
|
type CronRepository interface {
|
||||||
RunCronProcess()
|
RunCronProcess()
|
||||||
Crons(q CronQueryParams) (CronTasks, error)
|
Crons() ([]Cron, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type CronRepo struct {
|
type CronRepo struct {
|
||||||
|
@ -70,73 +68,12 @@ func (repo *CronRepo) RunCronProcess() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type CronQueryParams struct {
|
func (repo *CronRepo) Crons() ([]Cron, error) {
|
||||||
Title string
|
query := `SELECT id, title, slug, description, run_every, last_run, next_run, run_time, cron_state, is_enabled FROM tbl_cron`
|
||||||
Description string
|
|
||||||
IsEnabled int
|
|
||||||
CronState int
|
|
||||||
RowsPerPage int
|
|
||||||
Page int
|
|
||||||
SortBy string
|
|
||||||
SortDirection string
|
|
||||||
}
|
|
||||||
|
|
||||||
type CronTasks struct {
|
var tasks []Cron
|
||||||
TotalRows int `json:"total_rows"`
|
err := repo.db.Select(&tasks, query)
|
||||||
RowsPerPage int `json:"rows_per_page"`
|
return tasks, err
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repo *CronRepo) queueList() ([]Cron, error) {
|
func (repo *CronRepo) queueList() ([]Cron, error) {
|
||||||
|
|
Loading…
Reference in a new issue