-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstate.go
103 lines (96 loc) · 1.99 KB
/
state.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
/*
* Handle the unified global state
*
* Copyright (C) 2024 Runxi Yu <https://runxiyu.org>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package main
import (
"context"
"errors"
"sync/atomic"
"github.com/jackc/pgx/v5"
)
/*
* The uint32 should be accessed atomically
* 0: Student access is disabled
* 1: Student have read-only access
* 2: Student can choose courses
*/
var states = map[string]*uint32{
"Y9": new(uint32),
"Y10": new(uint32),
"Y11": new(uint32),
"Y12": new(uint32),
}
func loadState() error {
for yeargroup := range states {
var state uint32
err := db.QueryRow(
context.Background(),
"SELECT state FROM states WHERE yeargroup = $1",
yeargroup,
).Scan(&state)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
state = 0
_, err := db.Exec(
context.Background(),
"INSERT INTO states(yeargroup, state) VALUES ($1, $2)",
yeargroup,
state,
)
if err != nil {
return wrapError(errUnexpectedDBError, err)
}
} else {
return wrapError(errUnexpectedDBError, err)
}
}
_state, ok := states[yeargroup]
if !ok {
return errNoSuchYearGroup
}
atomic.StoreUint32(_state, state)
}
return nil
}
func saveStateValue(ctx context.Context, yeargroup string, newState uint32) error {
_, err := db.Exec(
ctx,
"UPDATE states SET state = $2 WHERE yeargroup = $1",
yeargroup,
newState,
)
if err != nil {
return wrapError(errUnexpectedDBError, err)
}
return nil
}
func setState(ctx context.Context, yeargroup string, newState uint32) error {
switch newState {
case 0:
case 1:
err := propagate(yeargroup, "STOP") /* TODO: propagate by year group */
if err != nil {
return err
}
case 2:
err := propagate(yeargroup, "START")
if err != nil {
return err
}
default:
return errInvalidState
}
err := saveStateValue(ctx, yeargroup, newState)
if err != nil {
return err
}
_state, ok := states[yeargroup]
if !ok {
return errNoSuchYearGroup
}
atomic.StoreUint32(_state, newState)
return nil
}