1
0
forked from MTSR/mapserver

save state in json

This commit is contained in:
NatureFreshMilk 2019-01-21 10:53:58 +01:00
parent de18a1d36c
commit 4194a16ee1
7 changed files with 87 additions and 35 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ output
map.sqlite map.sqlite
tiles.sqlite tiles.sqlite
tiles.sqlite-journal tiles.sqlite-journal
mapserver.json

View File

@ -3,32 +3,86 @@ package app
import ( import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"mapserver/coords"
"os" "os"
"sync"
) )
type Config struct { type Config struct {
Port int `json:"port"` Port int `json:"port"`
EnableInitialRendering bool `json:"enableinitialrendering"` EnableInitialRendering bool `json:"enableinitialrendering"`
EnableIncrementalUpdate bool `json:"enableincrementalupdate"` EnableIncrementalUpdate bool `json:"enableincrementalupdate"`
Webdev bool `json:"webdev"` Webdev bool `json:"webdev"`
WebApi WebApiConfig `json:"webapi"` WebApi *WebApiConfig `json:"webapi"`
RenderState *RenderStateType `json:"renderstate"`
} }
type WebApiConfig struct { type WebApiConfig struct {
EnableMapblock bool `json:"enablemapblock"` EnableMapblock bool `json:"enablemapblock"`
} }
type RenderStateType struct {
//Initial rendering flag (true=still active)
InitialRun bool `json:"initialrun"`
//Last initial rendering coords
LastX int `json:"lastx"`
LastY int `json:"lasty"`
LastZ int `json:"lastz"`
//Last mtime of incremental rendering
LastMtime int64 `json:"lastmtime"`
}
var lock sync.Mutex
const ConfigFile = "mapserver.json"
func (cfg *Config) Save() error {
return WriteConfig(ConfigFile, cfg)
}
func WriteConfig(filename string, cfg *Config) error {
lock.Lock()
defer lock.Unlock()
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
str, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
f.Write(str)
return nil
}
func ParseConfig(filename string) (*Config, error) { func ParseConfig(filename string) (*Config, error) {
webapi := WebApiConfig{ webapi := WebApiConfig{
EnableMapblock: false, EnableMapblock: false,
} }
rstate := RenderStateType{
InitialRun: true,
LastX: coords.MinCoord,
LastY: coords.MinCoord,
LastZ: coords.MinCoord,
LastMtime: 0,
}
cfg := Config{ cfg := Config{
Port: 8080, Port: 8080,
EnableInitialRendering: true, EnableInitialRendering: true,
EnableIncrementalUpdate: true, EnableIncrementalUpdate: true,
Webdev: false, Webdev: false,
WebApi: webapi, WebApi: &webapi,
RenderState: &rstate,
} }
info, err := os.Stat(filename) info, err := os.Stat(filename)

View File

@ -4,7 +4,6 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"mapserver/app" "mapserver/app"
"mapserver/coords" "mapserver/coords"
"time"
) )
func Job(ctx *app.App) { func Job(ctx *app.App) {
@ -12,7 +11,9 @@ func Job(ctx *app.App) {
fields := logrus.Fields{} fields := logrus.Fields{}
logrus.WithFields(fields).Info("Starting initial rendering") logrus.WithFields(fields).Info("Starting initial rendering")
lastcoords := coords.NewMapBlockCoords(coords.MinCoord, coords.MinCoord, coords.MinCoord) rstate := ctx.Config.RenderState
lastcoords := coords.NewMapBlockCoords(rstate.LastX, rstate.LastY, rstate.LastZ)
for true { for true {
newlastcoords, mblist, err := ctx.BlockAccessor.FindLegacyMapBlocks(lastcoords, 1000) newlastcoords, mblist, err := ctx.BlockAccessor.FindLegacyMapBlocks(lastcoords, 1000)
@ -23,22 +24,29 @@ func Job(ctx *app.App) {
lastcoords = *newlastcoords lastcoords = *newlastcoords
if len(mblist) == 1 { if len(mblist) <= 1 {
logrus.Info("Initial rendering complete") logrus.Info("Initial rendering complete")
rstate.InitialRun = false
ctx.Config.Save()
break break
} }
//for _, mb := range mblist { //for _, mb := range mblist {
//} //}
//Save current positions of initial run
rstate.LastX = lastcoords.X
rstate.LastY = lastcoords.Y
rstate.LastZ = lastcoords.Z
ctx.Config.Save()
fields = logrus.Fields{ fields = logrus.Fields{
"count": len(mblist), "count": len(mblist),
"X": lastcoords.X, "X": lastcoords.X,
"Y": lastcoords.Y, "Y": lastcoords.Y,
"Z": lastcoords.Z, "Z": lastcoords.Z,
} }
logrus.WithFields(fields).Info("Initial rendering") logrus.WithFields(fields).Info("Initial rendering")
time.Sleep(100 * time.Millisecond)
} }
} }

17
main.go
View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"mapserver/app" "mapserver/app"
@ -34,19 +33,15 @@ func main() {
} }
//parse Config //parse Config
cfg, err := app.ParseConfig("mapserver.json") cfg, err := app.ParseConfig(app.ConfigFile)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if p.Dumpconfig { //write back config with all values
str, err := json.MarshalIndent(cfg, "", " ") err = cfg.Save()
if err != nil { if err != nil {
panic(err) panic(err)
}
fmt.Println(string(str))
return
} }
//setup app context //setup app context
@ -58,7 +53,7 @@ func main() {
} }
//run initial rendering //run initial rendering
if ctx.Config.EnableInitialRendering { if ctx.Config.EnableInitialRendering && ctx.Config.RenderState.InitialRun {
go initialrenderer.Job(ctx) go initialrenderer.Job(ctx)
} }

View File

@ -1,9 +0,0 @@
{
"port": 8080,
"enableinitialrendering": true,
"enableincrementalupdate": false,
"webdev": true,
"webapi": {
"enablemapblock": true
}
}

View File

@ -16,7 +16,6 @@ func Parse() ParamsType {
flag.BoolVar(&(params.Help), "help", false, "Show help") flag.BoolVar(&(params.Help), "help", false, "Show help")
flag.BoolVar(&(params.Version), "version", false, "Show version") flag.BoolVar(&(params.Version), "version", false, "Show version")
flag.BoolVar(&(params.Dumpconfig), "dumpconfig", false, "dump mapserver.json and exit")
flag.BoolVar(&(params.Debug), "debug", false, "enable debug log") flag.BoolVar(&(params.Debug), "debug", false, "enable debug log")
flag.Parse() flag.Parse()

View File

@ -28,6 +28,10 @@ func Job(ctx *app.App) {
} }
} }
rstate := ctx.Config.RenderState
rstate.LastMtime = t
ctx.Config.Save()
fields = logrus.Fields{ fields = logrus.Fields{
"count": len(mblist), "count": len(mblist),
"time": t, "time": t,