-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_task.go
97 lines (77 loc) · 2.16 KB
/
scanner_task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package scanner
import (
"fmt"
"time"
"gorm.io/gorm"
)
type ScannerTask struct {
ID uint `gorm:"primarykey;autoIncrement"`
CreatedAt time.Time `gorm:"type:timestamp(5)"`
UpdatedAt time.Time `gorm:"type:timestamp(5)"`
Name string `gorm:"type:varchar(256);unique;not null"`
NextBlock uint64 `gorm:"type:numeric(20,0)"`
JobToken string `gorm:"type:string"`
}
func (ScannerTask) TableName() string {
return "scanner_tasks"
}
// GetScannerTaskByName returns a task with given name
func GetScannerTaskByName(db *gorm.DB, taskName string) (*ScannerTask, error) {
model := &ScannerTask{}
result := db.
Where("name = ?", taskName).
Take(model)
if result.Error == gorm.ErrRecordNotFound {
return nil, nil
} else if result.Error != nil {
return nil, result.Error
}
return model, nil
}
// CreateScannerTaskByName inserts a new task into the database.
func CreateScannerTaskByName(db *gorm.DB, taskName string, nextBlock uint64) (*ScannerTask, error) {
now := time.Now()
model := &ScannerTask{
Name: taskName,
NextBlock: nextBlock,
CreatedAt: now,
UpdatedAt: now,
}
result := db.Create(model)
if result.Error != nil {
return nil, result.Error
}
return model, nil
}
// UpdateNextBlock updates the next block to proceed with atomicity ensured.
func UpdateNextBlock(db *gorm.DB, m *ScannerTask, nextBlock uint64, newJobToken string) error {
updatedAt := time.Now()
result := db.
Model(&ScannerTask{}).
Where("id = ?", m.ID).
Where("name = ?", m.Name).
Where("next_block = ?", m.NextBlock).
Where("job_token = ?", m.JobToken).
UpdateColumns(&ScannerTask{
UpdatedAt: updatedAt,
NextBlock: nextBlock,
JobToken: newJobToken,
})
if result.Error != nil {
return result.Error
} else if result.RowsAffected != 1 {
return fmt.Errorf("rowAffected(%v) is not equal to 1", result.RowsAffected)
}
m.UpdatedAt = updatedAt
m.NextBlock = nextBlock
m.JobToken = newJobToken
return nil
}
// RemoveTask removes the task from the database.
func RemoveTask(db *gorm.DB, taskName string) error {
result := db.Where("name = ?", taskName).Delete(&ScannerTask{})
if result.Error != nil {
return result.Error
}
return nil
}