mirror of
https://github.com/ditatompel/xmr-remote-nodes.git
synced 2025-01-08 05:52:10 +07:00
chore: Make SQL queries in more readable
This commit is contained in:
parent
d99f8583c1
commit
fae32d9a74
2 changed files with 135 additions and 84 deletions
|
@ -44,7 +44,8 @@ func MigrateDb(db *DB) error {
|
|||
}
|
||||
|
||||
func getSchemaVersion(db *DB) int {
|
||||
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS tbl_schema_ver (
|
||||
_, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS tbl_schema_ver (
|
||||
version INT(5) UNSIGNED NOT NULL
|
||||
)`)
|
||||
if err != nil {
|
||||
|
@ -68,7 +69,8 @@ func v1(db *DB) error {
|
|||
slog.Debug("[DB] Migrating database schema version 1")
|
||||
// table: tbl_admin
|
||||
slog.Debug("[DB] Creating table: tbl_admin")
|
||||
_, err := db.Exec(`CREATE TABLE tbl_admin (
|
||||
_, err := db.Exec(`
|
||||
CREATE TABLE tbl_admin (
|
||||
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
username VARCHAR(255) NOT NULL,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
|
@ -87,7 +89,8 @@ func v1(db *DB) error {
|
|||
|
||||
// table: tbl_cron
|
||||
slog.Debug("[DB] Creating table: tbl_cron")
|
||||
_, err = db.Exec(`CREATE TABLE tbl_cron (
|
||||
_, err = db.Exec(`
|
||||
CREATE TABLE tbl_cron (
|
||||
id INT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
title VARCHAR(255) NOT NULL DEFAULT '',
|
||||
slug VARCHAR(255) NOT NULL DEFAULT '',
|
||||
|
@ -104,15 +107,26 @@ func v1(db *DB) error {
|
|||
return err
|
||||
}
|
||||
slog.Debug("[DB] Adding default cron jobs to table: tbl_cron")
|
||||
_, err = db.Exec(`INSERT INTO tbl_cron (title, slug, description, run_every)
|
||||
VALUES ('Delete old probe logs', 'delete_old_probe_logs', 'Delete old probe log from the database',120);`)
|
||||
_, err = db.Exec(`
|
||||
INSERT INTO tbl_cron (
|
||||
title,
|
||||
slug,
|
||||
description,
|
||||
run_every
|
||||
) VALUES (
|
||||
'Delete old probe logs',
|
||||
'delete_old_probe_logs',
|
||||
'Delete old probe log from the database',
|
||||
120
|
||||
);`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// table: tbl_node
|
||||
slog.Debug("[DB] Creating table: tbl_node")
|
||||
_, err = db.Exec(`CREATE TABLE tbl_node (
|
||||
_, err = db.Exec(`
|
||||
CREATE TABLE tbl_node (
|
||||
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
protocol VARCHAR(6) NOT NULL DEFAULT 'http' COMMENT 'http | https',
|
||||
hostname VARCHAR(255) NOT NULL,
|
||||
|
@ -147,7 +161,8 @@ func v1(db *DB) error {
|
|||
|
||||
// table: tbl_prober
|
||||
slog.Debug("[DB] Creating table: tbl_prober")
|
||||
_, err = db.Exec(`CREATE TABLE tbl_prober (
|
||||
_, err = db.Exec(`
|
||||
CREATE TABLE tbl_prober (
|
||||
id INT(9) UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
api_key VARCHAR(36) NOT NULL,
|
||||
|
@ -166,7 +181,8 @@ func v1(db *DB) error {
|
|||
|
||||
// table: tbl_probe_log
|
||||
slog.Debug("[DB] Creating table: tbl_probe_log")
|
||||
_, err = db.Exec(`CREATE TABLE tbl_probe_log (
|
||||
_, err = db.Exec(`
|
||||
CREATE TABLE tbl_probe_log (
|
||||
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
node_id INT(11) UNSIGNED NOT NULL DEFAULT 0,
|
||||
prober_id INT(9) UNSIGNED NOT NULL DEFAULT 0,
|
||||
|
|
|
@ -69,24 +69,52 @@ func (repo *CronRepo) RunCronProcess() {
|
|||
}
|
||||
|
||||
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`
|
||||
|
||||
var tasks []Cron
|
||||
err := repo.db.Select(&tasks, query)
|
||||
err := repo.db.Select(&tasks, `
|
||||
SELECT
|
||||
id,
|
||||
title,
|
||||
slug,
|
||||
description,
|
||||
run_every,
|
||||
last_run,
|
||||
next_run,
|
||||
run_time,
|
||||
cron_state,
|
||||
is_enabled
|
||||
FROM
|
||||
tbl_cron`)
|
||||
return tasks, err
|
||||
}
|
||||
|
||||
func (repo *CronRepo) queueList() ([]Cron, error) {
|
||||
tasks := []Cron{}
|
||||
query := `SELECT id, run_every, last_run, slug, next_run, cron_state FROM tbl_cron
|
||||
WHERE is_enabled = ? AND next_run <= ?`
|
||||
query := `
|
||||
SELECT
|
||||
id,
|
||||
run_every,
|
||||
last_run,
|
||||
slug,
|
||||
next_run,
|
||||
cron_state
|
||||
FROM
|
||||
tbl_cron
|
||||
WHERE
|
||||
is_enabled = ?
|
||||
AND next_run <= ?`
|
||||
err := repo.db.Select(&tasks, query, 1, time.Now().Unix())
|
||||
|
||||
return tasks, err
|
||||
}
|
||||
|
||||
func (repo *CronRepo) preRunTask(id int, lastRunTs int64) {
|
||||
query := `UPDATE tbl_cron SET cron_state = ?, last_run = ? WHERE id = ?`
|
||||
query := `
|
||||
UPDATE tbl_cron
|
||||
SET
|
||||
cron_state = ?,
|
||||
last_run = ?
|
||||
WHERE
|
||||
id = ?`
|
||||
row, err := repo.db.Query(query, 1, lastRunTs, id)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintf("[CRON] Failed to update pre cron state: %s", err))
|
||||
|
@ -95,7 +123,14 @@ func (repo *CronRepo) preRunTask(id int, lastRunTs int64) {
|
|||
}
|
||||
|
||||
func (repo *CronRepo) postRunTask(id int, nextRun int64, runtime float64) {
|
||||
query := `UPDATE tbl_cron SET cron_state = ?, next_run = ?, run_time = ? WHERE id = ?`
|
||||
query := `
|
||||
UPDATE tbl_cron
|
||||
SET
|
||||
cron_state = ?,
|
||||
next_run = ?,
|
||||
run_time = ?
|
||||
WHERE
|
||||
id = ?`
|
||||
row, err := repo.db.Query(query, 0, nextRun, runtime, id)
|
||||
if err != nil {
|
||||
slog.Error(fmt.Sprintf("[CRON] Failed to update post cron state: %s", err))
|
||||
|
|
Loading…
Reference in a new issue