-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstate.go
164 lines (153 loc) · 3.34 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* 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"
"log/slog"
"sync/atomic"
"time"
"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),
}
var schedules = map[string]*atomic.Pointer[time.Time]{
"Y9": {},
"Y10": {},
"Y11": {},
"Y12": {},
}
func loadStateAndSchedule() error {
for yeargroup := range states {
var state uint32
var schedule time.Time
err := db.QueryRow(
context.Background(),
"SELECT state, schedule FROM states WHERE yeargroup = $1",
yeargroup,
).Scan(&state, &schedule)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
state = 0
_, err := db.Exec(
context.Background(),
"INSERT INTO states(yeargroup, state, schedule) VALUES ($1, $2, $3)",
yeargroup,
state,
time.Time{},
)
if err != nil {
return wrapError(errUnexpectedDBError, err)
}
} else {
return wrapError(errUnexpectedDBError, err)
}
}
_state, ok := states[yeargroup]
if !ok {
return errNoSuchYearGroup
}
_schedule, ok := schedules[yeargroup]
if !ok {
return errNoSuchYearGroup
}
atomic.StoreUint32(_state, state)
_schedule.Store(&schedule)
}
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 saveScheduleValue(ctx context.Context, yeargroup string, newSchedule *time.Time) error {
_, err := db.Exec(
ctx,
"UPDATE states SET schedule = $2 WHERE yeargroup = $1",
yeargroup,
*newSchedule,
)
if err != nil {
return wrapError(errUnexpectedDBError, err)
}
return nil
}
func setSchedule(ctx context.Context, yeargroup string, newSchedule *time.Time) error {
_schedule, ok := schedules[yeargroup]
if !ok {
return errNoSuchYearGroup
}
_schedule.Store(newSchedule)
return saveScheduleValue(ctx, yeargroup, newSchedule)
}
func pollState() {
for {
time.Sleep(time.Second)
for yeargroup, _state := range states {
if atomic.LoadUint32(_state) == 3 {
_schedule, ok := schedules[yeargroup]
if !ok {
panic(errNoSuchYearGroup)
}
schedule := _schedule.Load()
if time.Now().After(*schedule) {
err := setState(context.Background(), yeargroup, 2)
if err != nil {
slog.Error("schedule setting failed", "yeargroup", yeargroup)
}
}
}
}
}
}
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
}
case 3:
// TODO XXX: Implement this!
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
}