Skip to content

Commit

Permalink
feat: queue manager
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcStdt committed Jul 19, 2024
1 parent 34d8e4c commit dc16779
Show file tree
Hide file tree
Showing 25 changed files with 584 additions and 444 deletions.
16 changes: 8 additions & 8 deletions .docker/druid-install-command.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#!/bin/bash

#check if first argument is there, set CHANNEL to it, otherwise default to latest
if [ -z "$1" ]; then
CHANNEL="$CHANNEL/download"
if [ -z "$CHANNEL" ]; then
URL_PATH="latest/download"
else
CHANNEL=download/$1
URL_PATH=download/$CHANNEL
fi

wget -O /app/resources/druid https://github.com/highcard-dev/druid-cli/releases/$CHANNEL/druid
wget -O /app/resources/druid_rcon https://github.com/highcard-dev/druid-cli/releases/$CHANNEL/druid_rcon
wget -O /app/resources/druid_rcon_web https://github.com/highcard-dev/druid-cli/releases/$CHANNEL/druid_rcon_web
wget -O /app/resources/entrypoint.sh https://github.com/highcard-dev/druid-cli/releases/$CHANNEL/entrypoint.sh
wget -O /app/resources/druid https://github.com/highcard-dev/druid-cli/releases/$URL_PATH/druid
wget -O /app/resources/druid_rcon https://github.com/highcard-dev/druid-cli/releases/$URL_PATH/druid_rcon
wget -O /app/resources/druid_rcon_web https://github.com/highcard-dev/druid-cli/releases/$URL_PATH/druid_rcon_web
wget -O /app/resources/entrypoint.sh https://github.com/highcard-dev/druid-cli/releases/$URL_PATH/entrypoint.sh
chmod +x /app/resources/druid /app/resources/druid_rcon /app/resources/druid_rcon_web

# Modify the PATH variable to prioritize /app/resources
export PATH=/app/resources:$PATH

bash /app/resources/entrypoint.sh
bash /app/resources/entrypoint.sh $@
12 changes: 11 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@
"trace": "verbose",
"debugAdapter": "legacy"

}
},
{
"name": "Test Current File",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${file}",
"env": {},
"args": [],
"showLog": true
}
]
}
9 changes: 4 additions & 5 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"strings"

"github.com/highcard-dev/daemon/internal/core/services"
"github.com/highcard-dev/daemon/internal/core/services/registry"
Expand Down Expand Up @@ -33,6 +32,8 @@ var RunCmd = &cobra.Command{
}
processLauncher := services.NewProcedureLauncher(client, processManager, services.NewPluginManager(), consoleService, logManager, scrollService)

queueManager := services.NewQueueManager(scrollService, processLauncher)

if !scrollService.LockExists() {
scrollService.WriteNewScrollLock()
logger.Log().Info("Lock file created")
Expand All @@ -55,11 +56,9 @@ var RunCmd = &cobra.Command{
return err
}

parts := strings.Split(args[0], ".")

command := strings.TrimPrefix(args[0], parts[0]+".")
command := args[0]

err = processLauncher.RunNew(command, parts[0], false)
err = queueManager.AddItem(command, false)
return err
},
}
Expand Down
27 changes: 17 additions & 10 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ to interact and monitor the Scroll Application`,

processLauncher := services.NewProcedureLauncher(client, processManager, pluginManager, consoleService, logManager, scrollService)

scrollHandler := handler.NewScrollHandler(scrollService, pluginManager, processLauncher)
queueManager := services.NewQueueManager(scrollService, processLauncher)

scrollHandler := handler.NewScrollHandler(scrollService, pluginManager, processLauncher, queueManager)
processHandler := handler.NewProcessHandler(processManager)
scrollLogHandler := handler.NewScrollLogHandler(scrollService, logManager, processManager)
scrollMetricHandler := handler.NewScrollMetricHandler(scrollService, processMonitor)
Expand All @@ -105,7 +107,7 @@ to interact and monitor the Scroll Application`,

a := s.Initialize()

signals.SetupSignals(processLauncher, processManager, a, shutdownWait)
signals.SetupSignals(queueManager, processManager, a, shutdownWait)

currentScroll, lock, err := scrollService.Bootstrap(ignoreVersionCheck)
if err != nil {
Expand All @@ -122,12 +124,6 @@ to interact and monitor the Scroll Application`,
//important to launch plugins, after the templates are rendered, sothat templates can provide for plugins
err = processLauncher.LaunchPlugins()

if err != nil {
return err
}
//run if something is there
err = processLauncher.StartLockfile(lock)

if err != nil {
return err
}
Expand All @@ -152,17 +148,28 @@ to interact and monitor the Scroll Application`,
}
//start scroll.init process
//initialize if nothing is there
err = processLauncher.Initalize(lock)
err = queueManager.AddItem(currentScroll.Init, false)
if err != nil {
return err
}

scrollService.WriteNewScrollLock()

logger.Log().Info("Bootstrapping done")
}

err = queueManager.QueueLockFile()
if err != nil {
return err
}

//run if something is there
go queueManager.Work()

//schedule crons
logger.Log().Info("Schedule crons")

cronManager := services.NewCronManager(currentScroll.Cronjobs, processLauncher)
cronManager := services.NewCronManager(currentScroll.Cronjobs, queueManager)
err = cronManager.Init()

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/minecraft/.scroll/scroll-lock.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"statuses":{"main.install":"done","main.start":"running"},"scroll_version":"0.0.1","scroll_name":"registry-1.docker.io/highcard/scroll-minecraft-spigot"}
{"statuses":{"install":"done","start":"running"},"scroll_version":"0.0.1","scroll_name":"registry-1.docker.io/highcard/scroll-minecraft-spigot"}
70 changes: 34 additions & 36 deletions examples/minecraft/.scroll/scroll.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,37 @@ name: registry-1.docker.io/highcard/scroll-minecraft-spigot
desc: Minecraft Spigot
version: 0.0.1
app_version: 1.20.4
init: "main.start"
processes:
main:
commands:
start:
needs: [main.install]
run: restart
procedures:
- mode: exec
data:
- java
- -Xmx1024M
- -Xms1024M
- -jar
- spigot.jar
- nogui
stop:
procedures:
- mode: stdin
data:
- main.start
- stop
install:
run: once
procedures:
- mode: exec
data:
- wget
- -O
- spigot.jar
- https://s3.eu-central-1.wasabisys.com/druid-scroll-artifacts/minecraft/spigot/spigot-1.20.4.jar
- mode: exec
data:
- bash
- -c
- echo eula=true > eula.txt
init: "start"
commands:
start:
needs: [install]
run: restart
procedures:
- mode: exec
data:
- java
- -Xmx1024M
- -Xms1024M
- -jar
- spigot.jar
- nogui
stop:
procedures:
- mode: stdin
data:
- start
- stop
install:
run: once
procedures:
- mode: exec
data:
- wget
- -O
- spigot.jar
- https://s3.eu-central-1.wasabisys.com/druid-scroll-artifacts/minecraft/spigot/spigot-1.20.4.jar
- mode: exec
data:
- bash
- -c
- echo eula=true > eula.txt
6 changes: 5 additions & 1 deletion examples/nginx/.scroll/scroll-lock.json
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{"statuses":{"main.start":"running"},"scroll_version":"0.0.1","scroll_name":"registry-1.docker.io/highcard/scroll-nginx"}
{
"statuses": { "start": "running" },
"scroll_version": "0.0.1",
"scroll_name": "registry-1.docker.io/highcard/scroll-nginx"
}
32 changes: 15 additions & 17 deletions examples/nginx/.scroll/scroll.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ name: registry-1.docker.io/highcard/scroll-nginx
desc: Nginx server
version: 0.0.1
app_version: latest
init: "main.start"
processes:
main:
commands:
start:
run: restart
procedures:
- mode: exec
data:
- nginx
stop:
procedures:
- mode: exec
data:
- nginx
- -s
- stop
init: "start"
commands:
start:
run: restart
procedures:
- mode: exec
data:
- nginx
stop:
procedures:
- mode: exec
data:
- nginx
- -s
- stop
20 changes: 8 additions & 12 deletions internal/core/domain/scroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ type Cronjob struct {
}

type File struct {
Name string `yaml:"name" json:"name"`
Desc string `yaml:"desc" json:"desc"`
Version *semver.Version `yaml:"version" json:"version"`
AppVersion string `yaml:"app_version" json:"app_version"` //don't make this a semver, it's not allways
Init string `yaml:"init" json:"init"`
Processes map[string]*ProcessCommand `yaml:"processes" json:"processes"`
Plugins map[string]map[string]string `yaml:"plugins" json:"plugins"`
Cronjobs []*Cronjob `yaml:"cronjobs" json:"cronjobs"`
Name string `yaml:"name" json:"name"`
Desc string `yaml:"desc" json:"desc"`
Version *semver.Version `yaml:"version" json:"version"`
AppVersion string `yaml:"app_version" json:"app_version"` //don't make this a semver, it's not allways
Init string `yaml:"init" json:"init"`
Commands map[string]*CommandInstructionSet `yaml:"commands" json:"commands"`
Plugins map[string]map[string]string `yaml:"plugins" json:"plugins"`
Cronjobs []*Cronjob `yaml:"cronjobs" json:"cronjobs"`
} // @name ScrollFile

type Scroll struct {
Expand All @@ -60,10 +60,6 @@ type CommandInstructionSet struct {
Run RunMode `yaml:"run,omitempty" json:"run,omitempty"`
} // @name CommandInstructionSet

type ProcessCommand struct {
Commands map[string]CommandInstructionSet `yaml:"commands" json:"commands"`
} // @name ProcessCommand

var ErrScrollDoesNotExist = fmt.Errorf("scroll does not exist")

func NewScroll(scrollDir string) (*Scroll, error) {
Expand Down
8 changes: 4 additions & 4 deletions internal/core/domain/scroll_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func WriteNewScrollLock(path string) *ScrollLock {
return lock
}

func (scrollLock *ScrollLock) GetStatus(process string, command string) ScrollLockStatus {
return scrollLock.Statuses[process+"."+command]
func (scrollLock *ScrollLock) GetStatus(command string) ScrollLockStatus {
return scrollLock.Statuses[command]
}

func (scrollLock *ScrollLock) SetStatus(process string, command string, status ScrollLockStatus) {
scrollLock.Statuses[process+"."+command] = status
func (scrollLock *ScrollLock) SetStatus(command string, status ScrollLockStatus) {
scrollLock.Statuses[command] = status
scrollLock.Write()
}
17 changes: 11 additions & 6 deletions internal/core/ports/services_ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ type ScrollServiceInterface interface {
GetCwd() string
WriteNewScrollLock() *domain.ScrollLock
GetLock() (*domain.ScrollLock, error)
GetCommand(cmd string, processId string) (*domain.CommandInstructionSet, error)
GetCommand(cmd string) (*domain.CommandInstructionSet, error)
}

type ProcedureLauchnerInterface interface {
RunNew(commandId string, processId string, changeStatus bool) error
RunProcedure(*domain.Procedure, string, string) (string, *int, error)
LaunchPlugins() error
RunProcedure(*domain.Procedure, string) (string, *int, error)
Run(cmd string) error
}

type PluginManagerInterface interface {
Expand All @@ -48,9 +49,9 @@ type LogManagerInterface interface {

type ProcessManagerInterface interface {
GetRunningProcesses() map[string]*domain.Process
GetRunningProcess(process string, commandName string) *domain.Process
Run(process string, commandName string, command []string, dir string) (*int, error)
RunTty(process string, comandName string, command []string, dir string) (*int, error)
GetRunningProcess(commandName string) *domain.Process
Run(commandName string, command []string, dir string) (*int, error)
RunTty(comandName string, command []string, dir string) (*int, error)
WriteStdin(process *domain.Process, data string) error
}

Expand Down Expand Up @@ -94,3 +95,7 @@ type OciRegistryInterface interface {
type CronManagerInterface interface {
Init()
}

type QueueManagerInterface interface {
AddItem(cmd string, changeStatus bool) error
}
19 changes: 7 additions & 12 deletions internal/core/services/cron_manager.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package services

import (
"strings"
"time"

"github.com/go-co-op/gocron"
Expand All @@ -12,14 +11,14 @@ import (
)

type CronManager struct {
crons []*domain.Cronjob
processLauncher ports.ProcedureLauchnerInterface
crons []*domain.Cronjob
queueManager ports.QueueManagerInterface
}

func NewCronManager(cronjobs []*domain.Cronjob, processLauncher ports.ProcedureLauchnerInterface) *CronManager {
func NewCronManager(cronjobs []*domain.Cronjob, queueManager ports.QueueManagerInterface) *CronManager {
return &CronManager{
crons: cronjobs,
processLauncher: processLauncher,
crons: cronjobs,
queueManager: queueManager,
}
}

Expand All @@ -30,13 +29,9 @@ func (c *CronManager) Init() error {
_, err := scheduler.Cron(cron.Schedule).Do(func() {
logger.Log().Info("Cronjob started", zap.String("name", cron.Name))

//parse cron.Command e.g. main.start
//split by dot
parts := strings.Split(cron.Command, ".")
process := parts[0]
command := parts[1]
//run cron.Command e.g. main.start

err := c.processLauncher.RunNew(command, process, false)
err := c.queueManager.AddItem(cron.Command, false)

if err != nil {
logger.Log().Error("error running cronjob", zap.String("name", cron.Name), zap.Error(err))
Expand Down
Loading

0 comments on commit dc16779

Please sign in to comment.