-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkarajo.go
202 lines (165 loc) · 4.28 KB
/
karajo.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
200
201
202
// SPDX-FileCopyrightText: 2021 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
// Package karajo implement HTTP workers and manager similar to cron but
// works only on HTTP.
//
// karajo has the web user interface (WUI) for monitoring the jobs that run
// on port 31937 by default and can be configurable.
//
// A single instance of karajo is configured through code or configuration
// file using ini file format.
//
// For more information see the README file in this repository.
package karajo
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"net/http"
"time"
liberrors "git.sr.ht/~shulhan/pakakeh.go/lib/errors"
libhttp "git.sr.ht/~shulhan/pakakeh.go/lib/http"
"git.sr.ht/~shulhan/pakakeh.go/lib/memfs"
"git.sr.ht/~shulhan/pakakeh.go/lib/mlog"
)
// Version of this library and program.
var Version = `0.9.3`
// timeNow return the current time in UTC rounded to second.
// During testing the variable will be replaced to provide static,
// predictable time.
var timeNow = func() time.Time {
return time.Now().Round(time.Second).UTC()
}
var (
memfsWww *memfs.MemFS
errUnauthorized = liberrors.E{
Code: http.StatusUnauthorized,
Message: `empty or invalid signature`,
}
)
// Karajo HTTP server and jobs manager.
type Karajo struct {
// HTTPd the HTTP server that Karajo use.
// One can register additional endpoints here.
HTTPd *libhttp.Server
env *Env
sm *sessionManager
// jobq is the channel that limit the number of job running at the
// same time.
// This limit can be overwritten by MaxJobRunning.
jobq chan struct{}
// logq is used to collect all job log once they finished.
logq chan *JobLog
}
// Sign generate hex string of HMAC + SHA256 of payload using the secret.
func Sign(payload, secret []byte) (sign string) {
var signer = hmac.New(sha256.New, secret)
_, _ = signer.Write(payload)
var bsign = signer.Sum(nil)
sign = hex.EncodeToString(bsign)
return sign
}
// New create and initialize Karajo from configuration file.
func New(env *Env) (k *Karajo, err error) {
var logp = `New`
err = env.init()
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
k = &Karajo{
env: env,
sm: newSessionManager(),
jobq: make(chan struct{}, env.MaxJobRunning),
logq: make(chan *JobLog),
}
mlog.SetPrefix(env.Name + `:`)
err = k.initMemfs()
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
err = k.initHTTPd()
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
return k, nil
}
// initMemfs initialize the memory file system for serving the WUI and public
// directory.
func (k *Karajo) initMemfs() (err error) {
var logp = `initMemfs`
if memfsWww == nil {
return fmt.Errorf(`%s: empty embedded www`, logp)
}
memfsWww.Opts.TryDirect = k.env.IsDevelopment
if len(k.env.DirPublic) == 0 {
return nil
}
var (
opts = memfs.Options{
Root: k.env.DirPublic,
TryDirect: true,
}
memfsPublic *memfs.MemFS
)
memfsPublic, err = memfs.New(&opts)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
memfsWww.Merge(memfsPublic)
return nil
}
// Start all the jobs and the HTTP server.
func (k *Karajo) Start() (err error) {
var (
jobHTTP *JobHTTP
job *JobExec
)
mlog.Outf(`started the karajo server at http://%s/karajo`, k.HTTPd.Addr)
if len(k.env.notif) > 0 {
go k.workerNotification()
}
for _, job = range k.env.ExecJobs {
go job.Start(k.jobq, k.logq)
<-k.jobq
}
for _, jobHTTP = range k.env.HTTPJobs {
go jobHTTP.Start(k.jobq, k.logq)
<-k.jobq
}
return k.HTTPd.Start()
}
// Stop all the jobs and the HTTP server.
func (k *Karajo) Stop() (err error) {
var (
jobHTTP *JobHTTP
job *JobExec
)
for _, jobHTTP = range k.env.HTTPJobs {
jobHTTP.Stop()
}
for _, job = range k.env.ExecJobs {
job.Stop()
}
return k.HTTPd.Stop(5 * time.Second)
}
// workerNotification receive JobLog from JobExec and JobHTTP everytime
// their started, running, success, failed, or paused.
func (k *Karajo) workerNotification() {
var (
jlog *JobLog
clientNotif notifClient
notifName string
logNotifName string
)
for jlog = range k.logq {
for _, logNotifName = range jlog.listNotif {
for notifName, clientNotif = range k.env.notif {
if logNotifName != notifName {
continue
}
go clientNotif.Send(jlog)
}
}
}
}