refactor: Use cronRepo struct instead of interface

No need to use interface when calling `cron.New()`.
This commit is contained in:
Cristian Ditaputratama 2024-07-07 02:57:36 +07:00
parent 9a44cd9546
commit ef553dab9e
Signed by: ditatompel
GPG key ID: 31D3D06D77950979

View file

@ -9,12 +9,7 @@ import (
"github.com/ditatompel/xmr-remote-nodes/internal/database" "github.com/ditatompel/xmr-remote-nodes/internal/database"
) )
type CronRepository interface { type cronRepo struct {
RunCronProcess(chan struct{})
Crons() ([]Cron, error)
}
type CronRepo struct {
db *database.DB db *database.DB
} }
@ -33,11 +28,11 @@ type Cron struct {
var rerunTimeout = 300 var rerunTimeout = 300
func New() CronRepository { func New() *cronRepo {
return &CronRepo{db: database.GetDB()} return &cronRepo{db: database.GetDB()}
} }
func (r *CronRepo) RunCronProcess(c chan struct{}) { func (r *cronRepo) RunCronProcess(c chan struct{}) {
for { for {
select { select {
case <-time.After(60 * time.Second): case <-time.After(60 * time.Second):
@ -73,7 +68,7 @@ func (r *CronRepo) RunCronProcess(c chan struct{}) {
} }
} }
func (r *CronRepo) Crons() ([]Cron, error) { func (r *cronRepo) Crons() ([]Cron, error) {
var tasks []Cron var tasks []Cron
err := r.db.Select(&tasks, ` err := r.db.Select(&tasks, `
SELECT SELECT
@ -92,7 +87,7 @@ func (r *CronRepo) Crons() ([]Cron, error) {
return tasks, err return tasks, err
} }
func (r *CronRepo) queueList() ([]Cron, error) { func (r *cronRepo) queueList() ([]Cron, error) {
tasks := []Cron{} tasks := []Cron{}
query := ` query := `
SELECT SELECT
@ -112,7 +107,7 @@ func (r *CronRepo) queueList() ([]Cron, error) {
return tasks, err return tasks, err
} }
func (r *CronRepo) preRunTask(id int, lastRunTs int64) { func (r *cronRepo) preRunTask(id int, lastRunTs int64) {
query := ` query := `
UPDATE tbl_cron UPDATE tbl_cron
SET SET
@ -127,7 +122,7 @@ func (r *CronRepo) preRunTask(id int, lastRunTs int64) {
defer row.Close() defer row.Close()
} }
func (r *CronRepo) postRunTask(id int, nextRun int64, runtime float64) { func (r *cronRepo) postRunTask(id int, nextRun int64, runtime float64) {
query := ` query := `
UPDATE tbl_cron UPDATE tbl_cron
SET SET
@ -143,7 +138,7 @@ func (r *CronRepo) postRunTask(id int, nextRun int64, runtime float64) {
defer row.Close() defer row.Close()
} }
func (r *CronRepo) execCron(slug string) { func (r *cronRepo) execCron(slug string) {
switch slug { switch slug {
case "delete_old_probe_logs": case "delete_old_probe_logs":
slog.Info(fmt.Sprintf("[CRON] Start running task: %s", slug)) slog.Info(fmt.Sprintf("[CRON] Start running task: %s", slug))
@ -154,7 +149,7 @@ func (r *CronRepo) execCron(slug string) {
} }
} }
func (r *CronRepo) deleteOldProbeLogs() { func (r *cronRepo) deleteOldProbeLogs() {
// for now, we only delete stats older than 1 month +2 days // for now, we only delete stats older than 1 month +2 days
startTs := time.Now().AddDate(0, -1, -2).Unix() startTs := time.Now().AddDate(0, -1, -2).Unix()
query := `DELETE FROM tbl_probe_log WHERE date_checked < ?` query := `DELETE FROM tbl_probe_log WHERE date_checked < ?`
@ -164,7 +159,7 @@ func (r *CronRepo) deleteOldProbeLogs() {
} }
} }
func (r *CronRepo) calculateMajorityFee() { func (r *cronRepo) calculateMajorityFee() {
netTypes := [3]string{"mainnet", "stagenet", "testnet"} netTypes := [3]string{"mainnet", "stagenet", "testnet"}
for _, net := range netTypes { for _, net := range netTypes {
row, err := r.db.Query(` row, err := r.db.Query(`