update.go 2.7 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9
package updater

import (
	"fmt"
	"sort"
	"strings"
	"sync"
	"time"

黄孟柱 已提交
10
	log "github.com/eolinker/goku-api-gateway/goku-log"
E
eoLinker API Management 已提交
11

黄孟柱 已提交
12 13
	gateway_manager "github.com/eolinker/goku-api-gateway/goku-node/manager/gateway-manager"
	"github.com/eolinker/goku-api-gateway/server/dao"
E
eoLinker API Management 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
)

var (
	isInit       = false
	handlers     = make(map[string]*updateHandler)
	ch           = make(chan bool, 1)
	periodCh     = make(chan int, 1)
	locker       sync.Mutex
	handlerExecs = make([]*updateHandlerExec, 0)
)

type UpdateHandleFunc func()

func init() {
	Add(func() {
		gateway_manager.LoadGatewayConfig()
		updatePeriod(gateway_manager.GetUpdatePeriod())
	}, 2, "goku_gateway")
}

type updateHandler struct {
	tables         []string
	UpdateHandlers []UpdateHandleFunc
	last           time.Time
}

func updatePeriod(period int) {
	periodCh <- period
}

func Add(handler UpdateHandleFunc, priority int, tables ...string) {
	sort.Strings(tables)
	key := strings.Join(tables, ":")
	locker.Lock()
	defer locker.Unlock()
	hfs, has := handlers[key]
	if !has {
		hfs = &updateHandler{
			tables: tables,
		}
	}
	hfs.UpdateHandlers = append(hfs.UpdateHandlers, handler)
	if !has {
		handlerExec := &updateHandlerExec{
			name:          key,
			priority:      priority,
			updateHandler: hfs,
		}
		handlerExecs = append(handlerExecs, handlerExec)
	}
	handlers[key] = hfs
}

func Update() {
	ch <- true
}

func InitUpdate() {
	locker.Lock()
	defer locker.Unlock()
	if isInit {
		return
	}
	isInit = true
	// 排序
	log.Debug("update sort handler")
	sort.Sort(handlerSlice(handlerExecs))
	log.Debug("update sort handler done")
	doFirst()
	go doUpdate(gateway_manager.GetUpdatePeriod())
}
func doFirst() {
	log.Debug("update doFirst")
	handlerExecsTmp := handlerExecs
	for _, handler := range handlerExecsTmp {

		if handler != nil {
			log.Debug("update ", handler.name, handler.tables)
			handler.last = time.Now()
			for _, fn := range handler.UpdateHandlers {
				fn()
			}
			log.Debug("update done")
		}
	}
}
func doUpdate(sec int) {
	fmt.Println(sec)
	period := time.Duration(sec) * time.Second
	t := time.NewTimer(period)
	for {
		select {
		case <-t.C:
		case <-ch:
		case p := <-periodCh:
			{
				period = time.Duration(p) * time.Second
				continue
			}
		}
		updates()
		t.Reset(period)
	}
}

func updates() {
	handlerExecsTmp := handlerExecs
	for _, handler := range handlerExecsTmp {
		if handler != nil {
			if u, nt := CheckUpdate(handler.last, handler.tables...); u {
				handler.last = nt
				for _, fn := range handler.UpdateHandlers {
					fn()
				}
			}
		}
	}
}

func CheckUpdate(last time.Time, tables ...string) (bool, time.Time) {
	t, err := dao.GetLastUpdateOfApi(tables...)
	if err != nil {
		return false, last
	}
	if t.After(last) {
		return true, t
	}
	return false, last
}