Skip to content

Commit 063a935

Browse files
authored
feat: new queue manager (#36)
* chore: refactor queue manager fully * chore: better queue manager * chore: init to idle
1 parent ebb2ae5 commit 063a935

37 files changed

Lines changed: 959 additions & 742 deletions

.docker/entrypoint.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ if [ -z "$input" ] || [[ $input =~ ([^/]+)/([^:]+):([^/]+) ]]; then
7171
args+=("--port" "${DRUID_PORT}")
7272
fi
7373

74-
if [ ! -z "${DRUID_INIT}" ];
74+
if [ ! -z "${DRUID_IDLE}" ];
7575
then
76-
args+=("--init")
76+
args+=("--idle")
7777
fi
7878

7979
echo "Running druid with args from env: ${args[@]}"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dlv.log
99
.DS_Store
1010

1111
__debug_bin*
12+
debug**
1213

1314
druid-cli-test
1415

.vscode/launch.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,18 @@
161161
"env": {},
162162
"args": [],
163163
"showLog": true
164-
}
164+
},
165+
{
166+
"name": "Attach",
167+
"type": "go",
168+
"request": "attach",
169+
"mode": "remote",
170+
"remotePath": "",
171+
"port":2345,
172+
"host":"127.0.0.1",
173+
"showLog": true,
174+
"trace": "log",
175+
"logOutput": "rpc"
176+
}
165177
]
166178
}

Dockerfile.testing

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ RUN apt-get update && \
1414
apt-get install -y ant && \
1515
apt-get clean;
1616

17-
1817
#/root/go/bin is not in the path
1918
ENV PATH=$PATH:/root/go/bin
2019
ENV PATH=$PATH:/usr/local/go/bin
2120

21+
RUN go install github.com/go-delve/delve/cmd/dlv@latest
22+
2223
# Copy go mod and sum files
2324
COPY go.mod go.sum ./
2425

2526
# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
26-
RUN go mod download
27-
28-
COPY . .
27+
RUN go mod download

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ test-integration:
3939

4040
test-integration-docker:
4141
docker build . -f Dockerfile.testing -t druid-cli-test
42-
docker run --rm druid-cli-test bash -c "go test -v ./test/integration"
42+
docker run -v ./:/app --entrypoint=/bin/bash --rm druid-cli-test -c "go test -v ./test/integration"
43+
docker run -v ./:/app --entrypoint=/bin/bash --rm druid-cli-test -c "go test -v ./test/integration/commands"
44+
45+
test-integration-docker-debug:
46+
docker build . -f Dockerfile.testing -t druid-cli-test
47+
docker run -v ./:/app --entrypoint=/bin/bash --rm -p 2345:2345 -it druid-cli-test -c "dlv --listen=:2345 --headless=true --log=true --log-output=debugger,debuglineerr,gdbwire,lldbout,rpc --accept-multiclient --api-version=2 test ./test/integration/commands"

cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ var RunCmd = &cobra.Command{
5858

5959
command := args[0]
6060

61-
err = queueManager.AddItem(command, false)
61+
err = queueManager.AddTempItem(command)
6262
return err
6363
},
6464
}

cmd/serve.go

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var ignoreVersionCheck bool
2222
var port int
2323
var shutdownWait int
2424
var additionalEndpoints []string
25-
var initScroll bool
25+
var idleScroll bool
2626

2727
var ServeCommand = &cobra.Command{
2828
Use: "serve",
@@ -52,6 +52,12 @@ to interact and monitor the Scroll Application`,
5252
logManager := services.NewLogManager()
5353
consoleService := services.NewConsoleManager(logManager)
5454
processMonitor := services.NewProcessMonitor()
55+
ctx := cmd.Context()
56+
go func() {
57+
<-ctx.Done()
58+
processMonitor.ShutdownPromMetrics()
59+
}()
60+
5561
processManager := services.NewProcessManager(logManager, consoleService, processMonitor)
5662

5763
pluginManager := services.NewPluginManager()
@@ -110,35 +116,26 @@ to interact and monitor the Scroll Application`,
110116

111117
signals.SetupSignals(queueManager, processManager, a, shutdownWait)
112118

113-
currentScroll, lock, err := scrollService.Bootstrap(ignoreVersionCheck)
114-
if err != nil {
115-
return err
116-
}
119+
if !idleScroll {
117120

118-
//normal or first launch?
119-
if len(lock.Statuses) > 0 {
120-
logger.Log().Info("Found lock file, bootstrapping done")
121-
err = scrollService.RenderCwdTemplates()
121+
currentScroll, lock, err := scrollService.Bootstrap(ignoreVersionCheck)
122122
if err != nil {
123123
return err
124124
}
125-
//important to launch plugins, after the templates are rendered, sothat templates can provide for plugins
126-
err = processLauncher.LaunchPlugins()
127125

128-
if err != nil {
129-
return err
130-
}
126+
newScroll := len(lock.Statuses) == 0
131127

132-
logger.Log().Info("Starting queue manager")
133-
go queueManager.Work()
134-
} else if initScroll || true { //TODO: remove true
135-
logger.Log().Info("No lock file found, but init command available. Bootstrapping...")
128+
if newScroll {
129+
logger.Log().Info("No lock file found, but init command available. Bootstrapping...")
136130

137-
logger.Log().Info("Creating lock and bootstrapping files")
138-
//There is an error here. We need to bootstrap the files before we render out the templates in the bootstrap func above
139-
err := scrollService.CreateLockAndBootstrapFiles()
140-
if err != nil {
141-
return err
131+
logger.Log().Info("Creating lock and bootstrapping files")
132+
//There is an error here. We need to bootstrap the files before we render out the templates in the bootstrap func above
133+
err := scrollService.CreateLockAndBootstrapFiles()
134+
if err != nil {
135+
return err
136+
}
137+
} else {
138+
logger.Log().Info("Found lock file, bootstrapping done")
142139
}
143140

144141
logger.Log().Info("Rendering cwd templates")
@@ -158,43 +155,44 @@ to interact and monitor the Scroll Application`,
158155
logger.Log().Info("Starting queue manager")
159156
go queueManager.Work()
160157

161-
logger.Log().Info("Starting scroll.init process")
162-
//start scroll.init process
163-
//initialize if nothing is there
164-
err = queueManager.AddItem(currentScroll.Init, true)
158+
if newScroll {
159+
logger.Log().Info("Starting scroll.init process")
160+
//start scroll.init process
161+
//initialize if nothing is there
162+
err = queueManager.AddAndRememberItem(currentScroll.Init)
163+
if err != nil {
164+
return err
165+
}
166+
167+
logger.Log().Info("Writing new scroll lock")
168+
scrollService.WriteNewScrollLock()
169+
170+
logger.Log().Info("Bootstrapping done")
171+
}
172+
173+
err = queueManager.QueueLockFile()
165174
if err != nil {
166175
return err
167176
}
168177

169-
logger.Log().Info("Writing new scroll lock")
170-
scrollService.WriteNewScrollLock()
178+
//schedule crons
179+
logger.Log().Info("Schedule crons")
171180

172-
logger.Log().Info("Bootstrapping done")
173-
}
181+
cronManager := services.NewCronManager(currentScroll.Cronjobs, queueManager)
182+
err = cronManager.Init()
174183

175-
err = queueManager.QueueLockFile()
176-
if err != nil {
177-
return err
178-
}
179-
180-
//schedule crons
181-
logger.Log().Info("Schedule crons")
182-
183-
cronManager := services.NewCronManager(currentScroll.Cronjobs, queueManager)
184-
err = cronManager.Init()
184+
if err != nil {
185+
return err
186+
}
185187

186-
if err != nil {
187-
return err
188+
logger.Log().Info("Active Scroll",
189+
zap.String("Description", fmt.Sprintf("%s (%s)", currentScroll.Desc, currentScroll.Name)),
190+
zap.String("Scroll Version", currentScroll.Version.String()),
191+
zap.String("cwd", cwd))
188192
}
193+
err = s.Serve(a, port)
189194

190-
logger.Log().Info("Active Scroll",
191-
zap.String("Description", fmt.Sprintf("%s (%s)", currentScroll.Desc, currentScroll.Name)),
192-
zap.String("Scroll Version", currentScroll.Version.String()),
193-
zap.String("cwd", cwd))
194-
195-
s.Serve(a, port)
196-
197-
return nil
195+
return err
198196
},
199197
}
200198

@@ -207,7 +205,7 @@ func init() {
207205

208206
ServeCommand.Flags().StringVarP(&userId, "user-id", "u", "", "Allowed user id")
209207

210-
ServeCommand.Flags().BoolVarP(&initScroll, "init", "", false, "Initialize the scroll if no lock file is present")
208+
ServeCommand.Flags().BoolVarP(&idleScroll, "idle", "", false, "Don't start the queue manager")
211209

212210
ServeCommand.Flags().BoolVarP(&ignoreVersionCheck, "ignore-version-check", "", false, "Ignore version check")
213211

cmd/server/web/server.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,11 @@ func (s *Server) SetAPI(app *fiber.App) *fiber.App {
172172
return app
173173
}
174174

175-
func (s *Server) Serve(app *fiber.App, port int) {
175+
func (s *Server) Serve(app *fiber.App, port int) error {
176176
addr := fmt.Sprintf(":%d", port)
177177
if err := app.Listen(addr); err != nil {
178178
logger.Log().Error("web server error", zap.Error(err))
179+
return err
179180
}
181+
return nil
180182
}

examples/minecraft/.scroll/scroll.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ commands:
2020
procedures:
2121
- mode: stdin
2222
data:
23-
- start
23+
- start.0
2424
- stop
2525
install:
2626
run: once

examples/nginx/.scroll/scroll.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ app_version: latest
55
init: "start"
66
commands:
77
start:
8-
run: restart
8+
# the nginx command detaches the process
9+
#run: restart
910
procedures:
1011
- mode: exec
1112
data:

0 commit comments

Comments
 (0)