Skip to content

Commit 09d5a8f

Browse files
committed
feat: "persistent" run mode
1 parent 5b32743 commit 09d5a8f

File tree

13 files changed

+120
-24
lines changed

13 files changed

+120
-24
lines changed

.vscode/launch.json

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"--cwd",
2828
"${workspaceFolder}/examples/restore-test",
2929
"https://gist.github.com/jlmaccal/ca6b08a307800f80986661075c82c432/raw/dbd5e7a38d0660b7047476de33da142bd79c22ef/test.tgz"
30-
],
30+
]
3131
},
3232
{
3333
"name": "Debug Daemon serve (restore example)",
@@ -46,6 +46,32 @@
4646
"artifacts.druid.gg/druid-team/scroll-lgsm:arkserver",
4747
"--coldstarter"
4848
],
49+
"env": {
50+
"DRUID_PORT_QUERY": "2556",
51+
"DRUID_PORT_MAIN": "7771"
52+
}
53+
},
54+
{
55+
"name": "Debug Daemon serve (restore example no snap)",
56+
"type": "go",
57+
"request": "launch",
58+
"mode": "debug",
59+
"console": "integratedTerminal",
60+
"program": "${workspaceFolder}/main.go",
61+
"args": [
62+
"serve",
63+
"--cwd",
64+
"${workspaceFolder}/examples/ark-restore-t",
65+
"-p",
66+
"9190",
67+
"artifacts.druid.gg/druid-team/scroll-minecraft-spigot:1.21.1",
68+
"--coldstarter",
69+
"--init-snapshot-url", "nil"
70+
],
71+
"env": {
72+
"DRUID_PORT_QUERY": "2556",
73+
"DRUID_PORT_MAIN": "7771"
74+
}
4975
},
5076
{
5177
"name": "Debug Daemon serve (htop example)",
@@ -114,7 +140,8 @@
114140
],
115141
"env": {
116142
"DRUID_PORT_QUERY": "2556",
117-
"DRUID_PORT_MAIN": "7771"
143+
"DRUID_PORT_MAIN": "7771",
144+
"LOG_LEVEL": "debug"
118145
}
119146
},
120147
{

cmd/run.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ var RunCmd = &cobra.Command{
4646
snapshotService := snapshotService.NewSnapshotService()
4747

4848
_, err = initScroll(scrollService, snapshotService, processLauncher, queueManager)
49+
if err != nil {
50+
return fmt.Errorf("error initializing scroll: %w", err)
51+
}
4952

5053
logger.Log().Info("Adding command to queue", zap.String("command", command))
5154
err = queueManager.AddTempItem(command)

cmd/serve.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,14 @@ to interact and monitor the Scroll Application`,
140140

141141
websocketHandler := handler.NewWebsocketHandler(authorizer, scrollService, consoleService)
142142

143-
s := web.NewServer(jwksUrl, scrollHandler, scrollLogHandler, scrollMetricHandler, annotationHandler, processHandler, queueHandler, websocketHandler, portHandler, healthHandler, coldstarterHandler, authorizer, cwd)
143+
signalHandler := signals.NewSignalHandler(ctx, queueManager, processManager, nil, shutdownWait)
144+
daemonHander := handler.NewDaemonHandler(signalHandler)
145+
146+
s := web.NewServer(jwksUrl, scrollHandler, scrollLogHandler, scrollMetricHandler, annotationHandler, processHandler, queueHandler, websocketHandler, portHandler, healthHandler, coldstarterHandler, daemonHander, authorizer, cwd)
144147

145148
a := s.Initialize()
146149

147-
signalHandler := signals.NewSignalHandler(ctx, queueManager, processManager, a, shutdownWait)
150+
signalHandler.SetApp(a)
148151

149152
if watchPorts {
150153
logger.Log().Info("Starting port watcher", zap.Strings("interfaces", watchPortsInterfaces))
@@ -219,7 +222,7 @@ to interact and monitor the Scroll Application`,
219222
}
220223
}
221224

222-
signalHandler.ShutdownRoutine()
225+
signalHandler.ExtendedShutdownRoutine()
223226
}
224227
} else {
225228
logger.Log().Warn("No ports to start, skipping coldstarter")
@@ -248,7 +251,7 @@ to interact and monitor the Scroll Application`,
248251
func init() {
249252
ServeCommand.Flags().IntVarP(&port, "port", "p", 8081, "Port")
250253

251-
ServeCommand.Flags().IntVarP(&shutdownWait, "shutdown-wait", "", 60, "Wait interval how long the process is allowed to shutdown. First normal shutdown, then forced shutdown")
254+
ServeCommand.Flags().IntVarP(&shutdownWait, "shutdown-wait", "", 10, "Wait interval how long the process is allowed to shutdown. First normal shutdown, then forced shutdown")
252255

253256
ServeCommand.Flags().StringVarP(&jwksUrl, "jwks-server", "", "", "JWKS Server to authenticate requests against")
254257

cmd/server/web/server.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Server struct {
3636
portHandler ports.PortHandlerInterface
3737
healthHandler ports.HealthHandlerInterface
3838
coldstarterHandler ports.ColdstarterHandlerInterface
39+
daemonHander ports.SignalHandlerInterface
3940
webdavPath string
4041
}
4142

@@ -51,6 +52,7 @@ func NewServer(
5152
portHandler ports.PortHandlerInterface,
5253
healthHandler ports.HealthHandlerInterface,
5354
coldstarterHandler ports.ColdstarterHandlerInterface,
55+
daemonHander ports.SignalHandlerInterface,
5456
authorizerService ports.AuthorizerServiceInterface,
5557
webdavPath string,
5658
) *Server {
@@ -73,6 +75,7 @@ func NewServer(
7375
healthHandler: healthHandler,
7476
coldstarterHandler: coldstarterHandler,
7577
webdavPath: webdavPath,
78+
daemonHander: daemonHander,
7679
}
7780

7881
if jwlsUrl != "" {
@@ -153,6 +156,8 @@ func (s *Server) SetAPI(app *fiber.App) *fiber.App {
153156

154157
apiRoutes.Get("/health", s.healthHandler.Health).Name("health-authenticated")
155158

159+
apiRoutes.Post("/daemon/stop", s.daemonHander.Stop).Name("daemon.stop")
160+
156161
// Create the WebDAV handler
157162
webdavHandler := &webdav.Handler{
158163
Prefix: "/webdav",
@@ -189,6 +194,10 @@ func (s *Server) SetAPI(app *fiber.App) *fiber.App {
189194
return app
190195
}
191196

197+
func (s *Server) SetDaemonRoute(app *fiber.App, signalHandler ports.SignalHandlerInterface) {
198+
app.Post("/stop", signalHandler.Stop).Name("daemon.stop")
199+
}
200+
192201
func (s *Server) Serve(app *fiber.App, port int) error {
193202
addr := fmt.Sprintf(":%d", port)
194203
if err := app.Listen(addr); err != nil {

examples/minecraft/.scroll/scroll.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ init: "start"
3232
commands:
3333
start:
3434
needs: [install]
35-
run: restart
35+
run: persistent
3636
procedures:
3737
- mode: exec
3838
data:

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
module github.com/highcard-dev/daemon
22

3-
go 1.22.0
4-
5-
toolchain go1.22.8
3+
go 1.23.0
64

75
require (
86
github.com/Masterminds/semver/v3 v3.2.1

internal/core/domain/scroll.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import (
1515
type RunMode string
1616

1717
const (
18-
RunModeAlways RunMode = "always" //default
19-
RunModeOnce RunMode = "once"
20-
RunModeRestart RunMode = "restart"
18+
RunModeAlways RunMode = "always" //default
19+
RunModeOnce RunMode = "once"
20+
RunModeRestart RunMode = "restart"
21+
RunModePersistent RunMode = "persistent" //restarts on failure and on program restart
2122
)
2223

2324
type Cronjob struct {

internal/core/ports/handler_ports.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ type HealthHandlerInterface interface {
5050
type ColdstarterHandlerInterface interface {
5151
Finish(ctx *fiber.Ctx) error
5252
}
53+
54+
type SignalHandlerInterface interface {
55+
Stop(ctx *fiber.Ctx) error
56+
}

internal/core/services/process_manager.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ func (po *ProcessManager) Run(commandName string, command []string, dir string)
140140
process.Cmd = exec.Command(name, args...)
141141
process.Cmd.Dir = dir
142142

143+
//process.Cmd.SysProcAttr = &syscall.SysProcAttr{
144+
// Setpgid: true,
145+
//}
146+
143147
stdoutReader, err := process.Cmd.StdoutPipe()
144148
if err != nil {
145149
cmdDone()
@@ -171,7 +175,9 @@ func (po *ProcessManager) Run(commandName string, command []string, dir string)
171175
defer wg.Done()
172176
scanner := bufio.NewScanner(stdoutReader)
173177
for scanner.Scan() {
174-
combinedChannel <- scanner.Text() + "\n"
178+
text := scanner.Text()
179+
logger.Log().Debug(text)
180+
combinedChannel <- text + "\n"
175181
}
176182
}()
177183

@@ -181,7 +187,9 @@ func (po *ProcessManager) Run(commandName string, command []string, dir string)
181187
defer wg.Done()
182188
scanner := bufio.NewScanner(stderrReader)
183189
for scanner.Scan() {
184-
combinedChannel <- scanner.Text() + "\n"
190+
text := scanner.Text()
191+
logger.Log().Debug(text)
192+
combinedChannel <- text + "\n"
185193
}
186194

187195
}()

internal/core/services/process_monitor.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ func (po *ProcessMonitor) StartMonitoring() {
8989
}
9090

9191
func (po *ProcessMonitor) RefreshMetrics() {
92+
po.mu.Lock()
93+
defer po.mu.Unlock()
9294
for name, process := range po.processes {
9395

9496
_, err := po.GetProcessMetric(name, process)

0 commit comments

Comments
 (0)