-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.go
112 lines (100 loc) · 2.66 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"context"
"database/sql"
"log"
"os"
"strconv"
"time"
_ "github.com/lib/pq"
)
func main() {
taskInstanceCount, err := strconv.Atoi(os.Getenv("TASK_INSTANCES_COUNT"))
if err != nil {
panic("Error parsing TASK_INSTANCES_COUNT")
}
actionType := os.Args[1]
if actionType == "insert" {
addTaskInstances(taskInstanceCount)
}
if actionType == "update" {
updateTaskInstances(taskInstanceCount)
// keep the worker running indefinitely
exit := make(chan string)
for {
select {
case <-exit:
os.Exit(0)
}
}
}
}
type taskInstanceState string
const (
queued taskInstanceState = "queued"
procesed taskInstanceState = "processed"
)
func addTaskInstances(numberOfTaskInstancesToAdd int) {
log.Printf("Adding %v task instances", numberOfTaskInstancesToAdd)
db := getDB()
defer db.Close()
for i := 0; i < numberOfTaskInstancesToAdd; i++ {
log.Printf("Inserting %v of %v", (i + 1), numberOfTaskInstancesToAdd)
insert, err := db.Query("INSERT INTO task_instance (state) VALUES ($1)", queued)
insert.Close()
if err != nil {
panic(err.Error())
}
}
}
type taskInstance struct {
id int
state string
}
func updateTaskInstances(numberOfTaskInstancesToUpdate int) {
log.Printf("Updating %v task instances", numberOfTaskInstancesToUpdate)
db := getDB()
defer db.Close()
for i := 0; i < numberOfTaskInstancesToUpdate; i++ {
ctx := context.Background()
tx, err := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted})
if err != nil {
log.Fatalf("Error creating transaction %v", err)
}
var taskInstance taskInstance
err = tx.QueryRowContext(ctx, "SELECT id, state FROM task_instance WHERE state = $1 LIMIT 1 FOR UPDATE", queued).Scan(&taskInstance.id, &taskInstance.state)
if err != nil {
tx.Rollback()
log.Fatal(err)
time.Sleep(2 * time.Second)
}
log.Printf("Updating task instance id %v", taskInstance.id)
updateStmt, err := tx.PrepareContext(ctx, "UPDATE task_instance SET state = $1 WHERE id = $2")
if err != nil {
tx.Rollback()
log.Fatalf("Rolling back transaction %v", err)
panic(err.Error())
}
_, err = updateStmt.ExecContext(ctx, procesed, taskInstance.id)
if err != nil {
tx.Rollback()
log.Fatalf("Rolling back transaction %v", err)
}
updateStmt.Close()
err = tx.Commit()
if err != nil {
log.Fatalf("Error committing transaction! Error is %v", err)
} else {
log.Printf("Transaction committed for id %v", taskInstance.id)
}
}
}
func getDB() *sql.DB {
db, err := sql.Open("postgres", os.Getenv("CONNECTION_STRING"))
db.SetMaxOpenConns(5)
// if there is an error opening the connection, handle it
if err != nil {
panic(err.Error())
}
return db
}