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
tiles.sqlite
tiles.sqlite-journal
mapserver.json

View File

@ -3,32 +3,86 @@ package app
import (
"encoding/json"
"io/ioutil"
"mapserver/coords"
"os"
"sync"
)
type Config struct {
Port int `json:"port"`
EnableInitialRendering bool `json:"enableinitialrendering"`
EnableIncrementalUpdate bool `json:"enableincrementalupdate"`
Webdev bool `json:"webdev"`
WebApi WebApiConfig `json:"webapi"`
Port int `json:"port"`
EnableInitialRendering bool `json:"enableinitialrendering"`
EnableIncrementalUpdate bool `json:"enableincrementalupdate"`
Webdev bool `json:"webdev"`
WebApi *WebApiConfig `json:"webapi"`
RenderState *RenderStateType `json:"renderstate"`
}
type WebApiConfig struct {
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) {
webapi := WebApiConfig{
EnableMapblock: false,
}
rstate := RenderStateType{
InitialRun: true,
LastX: coords.MinCoord,
LastY: coords.MinCoord,
LastZ: coords.MinCoord,
LastMtime: 0,
}
cfg := Config{
Port: 8080,
EnableInitialRendering: true,
EnableIncrementalUpdate: true,
Webdev: false,
WebApi: webapi,
WebApi: &webapi,
RenderState: &rstate,
}
info, err := os.Stat(filename)

View File

@ -4,7 +4,6 @@ import (
"github.com/sirupsen/logrus"
"mapserver/app"
"mapserver/coords"
"time"
)
func Job(ctx *app.App) {
@ -12,7 +11,9 @@ func Job(ctx *app.App) {
fields := logrus.Fields{}
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 {
newlastcoords, mblist, err := ctx.BlockAccessor.FindLegacyMapBlocks(lastcoords, 1000)
@ -23,22 +24,29 @@ func Job(ctx *app.App) {
lastcoords = *newlastcoords
if len(mblist) == 1 {
if len(mblist) <= 1 {
logrus.Info("Initial rendering complete")
rstate.InitialRun = false
ctx.Config.Save()
break
}
//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{
"count": len(mblist),
"X": lastcoords.X,
"Y": lastcoords.Y,
"Z": lastcoords.Z,
"X": lastcoords.X,
"Y": lastcoords.Y,
"Z": lastcoords.Z,
}
logrus.WithFields(fields).Info("Initial rendering")
time.Sleep(100 * time.Millisecond)
}
}

17
main.go
View File

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