123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- package importer
- import (
- "encoding/json"
- "os"
- "path/filepath"
- "sync"
- "time"
- "git.wecise.com/wecise/cgimport/odbc"
- "git.wecise.com/wecise/util/rc"
- )
- type ImportStatus struct {
- RecordsCount int64
- }
- type CGIStatus struct {
- filepath string
- //
- TotalUseTime time.Duration
- ImportStatus map[string]*ImportStatus
- //
- mutex sync.RWMutex
- rc *rc.RoutinesController
- lasterror error
- lastsavetime time.Time
- waitdone chan any
- }
- func NewCGIStatus() *CGIStatus {
- return &CGIStatus{
- filepath: mcfg.GetString("cgi.statusfile", "/opt/matrix/var/cgimport/"+odbc.Keyspace+".status.txt"),
- ImportStatus: map[string]*ImportStatus{},
- rc: rc.NewRoutinesController("", 1),
- waitdone: make(chan any, 1),
- }
- }
- func (cgistatus *CGIStatus) Load() error {
- cgistatusbs, e := os.ReadFile(cgistatus.filepath)
- if e != nil && !os.IsNotExist(e) {
- return e
- }
- if len(cgistatusbs) > 0 {
- e = json.Unmarshal(cgistatusbs, &cgistatus)
- if e != nil {
- logger.Warn(e)
- }
- }
- return nil
- }
- func (cgistatus *CGIStatus) WaitSaveDone() {
- cgistatus.waitdone <- 1
- cgistatus.rc.WaitDone()
- }
- func (cgistatus *CGIStatus) Save() (err error) {
- cgistatus.rc.CallLast2Only(func() {
- if !cgistatus.lastsavetime.Equal(time.Time{}) {
- interval := time.Since(cgistatus.lastsavetime)
- if interval < 1*time.Second {
- t := time.NewTimer(1*time.Second - interval)
- select {
- case <-t.C:
- case v := <-cgistatus.waitdone:
- cgistatus.waitdone <- v
- }
- }
- }
- cgistatus.mutex.RLock()
- cgistatusbs, e := json.MarshalIndent(cgistatus, "", " ")
- cgistatus.mutex.RUnlock()
- if e != nil {
- cgistatus.lasterror = e
- return
- }
- e = os.MkdirAll(filepath.Dir(cgistatus.filepath), os.ModePerm)
- if e != nil {
- cgistatus.lasterror = e
- return
- }
- e = os.WriteFile(cgistatus.filepath, cgistatusbs, os.ModePerm)
- if e != nil {
- cgistatus.lasterror = e
- return
- }
- cgistatus.lastsavetime = time.Now()
- // fmt.Println(cgistatus.lastsavetime)
- })
- return cgistatus.lasterror
- }
|