-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (163 loc) · 4.84 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"context"
"database/sql"
_ "embed"
"fmt"
"net/http"
"os"
"strconv"
"time"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/relay"
log "github.com/sirupsen/logrus"
_ "github.com/mattn/go-sqlite3" // sqlite driver
)
type ProcessState string
const (
RUNNING ProcessState = "RUNNING"
CANCELING ProcessState = "CANCELING"
FINISHED ProcessState = "FINISHED"
CANCELED ProcessState = "CANCELED"
)
type Process struct {
Id pid
Status ProcessState
}
type resolver struct{ *manager }
func (r *resolver) Process() *resolver { return r }
func (r *resolver) All(ctx context.Context) ([]Process, error) { return r.List(ctx) }
func (r *resolver) New(ctx context.Context) (Process, error) { return r.Spawn(ctx) }
func (r *resolver) Stop(ctx context.Context, args struct{ Pid pid }) (Process, error) {
return r.Interupt(ctx, args.Pid)
}
//go:embed schema.graphql
var schema string
func main() {
store := mustNewStore()
defer store.Close()
http.Handle("/", playground.Handler("Playground", "/query"))
http.Handle("/query", &relay.Handler{
Schema: graphql.MustParseSchema(
schema,
&resolver{&manager{store}},
graphql.UseFieldResolvers(),
)})
log.Info("server on localhost:8080...")
if err := http.ListenAndServe(":8080", nil); err != http.ErrServerClosed {
log.WithError(err).Errorf("server closed due to unexpected error")
}
}
type store struct {
db *sql.DB
}
//go:embed migration.sql
var migration string
func mustNewStore() *store {
if _, err := os.Create("./data.db"); err != nil {
log.WithError(err).Fatal("failed to create sqlite file")
}
conn, err := sql.Open("sqlite3", "./data.db")
if err != nil {
log.WithError(err).Fatal("failed to establish connection")
}
if _, err := conn.Exec(migration); err != nil {
log.WithError(err).Fatal("failed to run migration")
}
return &store{conn}
}
func (s *store) Close() error { return s.db.Close() }
func (s *store) New(ctx context.Context) (process Process, err error) {
return process, s.db.QueryRowContext(ctx, "INSERT INTO processes(status) VALUES($1) RETURNING id, status", RUNNING).Scan(&process.Id, &process.Status)
}
func (s *store) Update(ctx context.Context, update Process) (process Process, err error) {
return process, s.db.QueryRowContext(ctx, "UPDATE processes SET status=$1 WHERE id=$2 RETURNING id, status", update.Status, update.Id).Scan(&process.Id, &process.Status)
}
func (s *store) Get(ctx context.Context, pid pid) (process Process, err error) {
return process, s.db.QueryRowContext(ctx, "SELECT id, status FROM processes WHERE id=$1", pid).Scan(&process.Id, &process.Status)
}
func (s *store) List(ctx context.Context) ([]Process, error) {
rows, err := s.db.QueryContext(ctx, "SELECT id, status FROM processes")
if err != nil {
return nil, err
}
var processes []Process
for rows.Next() {
var process Process
if err := rows.Scan(&process.Id, &process.Status); err != nil {
return nil, err
}
processes = append(processes, process)
}
return processes, nil
}
type manager struct {
*store
}
func (m *manager) Spawn(ctx context.Context) (p Process, err error) {
p, err = m.store.New(ctx)
if err != nil {
return
}
go func(pid pid) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func(ctx context.Context, finished context.CancelFunc) {
time.Sleep(time.Second * 5)
_, err := m.Update(ctx, Process{Id: p.Id, Status: FINISHED})
if err != nil && err != context.Canceled {
log.WithError(err).Fatal("couldn't update process state")
}
finished()
}(ctx, cancel)
t := time.NewTicker(2 * time.Second)
for {
select {
case <-ctx.Done():
return
case <-t.C:
process, err := m.store.Get(ctx, pid)
if err == context.Canceled {
return
} else if err != nil {
log.WithError(err).Fatal("couldn't poll for process status")
}
if process.Status == CANCELING {
cancel()
if _, err := m.Update(context.Background(), Process{Id: p.Id, Status: CANCELED}); err != nil {
log.WithError(err).Fatal("couldn't update cancelling process to cancelled")
}
return
}
}
}
}(p.Id)
return p, nil
}
func (m *manager) Interupt(ctx context.Context, pid pid) (process Process, err error) {
return m.Update(ctx, Process{Id: pid, Status: CANCELING})
}
type pid int
func (p pid) MarshalJSON() ([]byte, error) {
return strconv.AppendQuote(nil, fmt.Sprintf("%d", p)), nil
}
func (p *pid) UnmarshalGraphQL(input interface{}) error {
var err error
switch input := input.(type) {
case string:
uuid, err := strconv.Atoi(input)
if err != nil {
return err
}
*p = pid(uuid)
case int32:
*p = pid(input)
default:
err = fmt.Errorf("wrong type for ID: %T", input)
}
return err
}
func (pid) ImplementsGraphQLType(name string) bool {
return name == "ID"
}