reid from https://github.com/michuanhaohao/reid-strong-baseline
zhangmeng
2020-02-25 0786441ed1828c411a16d6648baee753a02a3ddb
common/reboot.go
@@ -1,64 +1,64 @@
package common
import (
   "context"
   "os"
   "sync"
   "time"
    "context"
    "os"
    "sync"
    "time"
)
// Disturber stop
type Disturber struct {
   mtx    sync.Mutex
   live   bool
   until  int
   maxTry int
    mtx    sync.Mutex
    live   bool
    until  int
    maxTry int
}
// NewDisturber new
func NewDisturber(maxTry int) *Disturber {
   return &Disturber{
      live:   true,
      until:  0,
      maxTry: maxTry,
   }
    return &Disturber{
        live:   true,
        until:  0,
        maxTry: maxTry,
    }
}
// Prevent prevent
func (d *Disturber) Prevent() {
   d.mtx.Lock()
   defer d.mtx.Unlock()
   d.live = true
    d.mtx.Lock()
    defer d.mtx.Unlock()
    d.live = true
}
// MaybeReboot reboot
func (d *Disturber) MaybeReboot(ctx context.Context, fn func(...interface{})) {
   d.live = true
   for {
      select {
      case <-ctx.Done():
         return
      default:
         d.mtx.Lock()
         running := d.live
         d.mtx.Unlock()
    d.live = true
    for {
        select {
        case <-ctx.Done():
            return
        default:
            d.mtx.Lock()
            running := d.live
            d.mtx.Unlock()
         if running {
            d.until = 0
            if running {
                d.until = 0
            d.mtx.Lock()
            d.live = false
            d.mtx.Unlock()
                d.mtx.Lock()
                d.live = false
                d.mtx.Unlock()
         } else {
            d.until++
            fn("!!!!!!No Running: ", d.until)
            if d.until > d.maxTry {
               fn("!!!!!!Too Long Running, Reboot: ", d.maxTry)
               os.Exit(0)
            }
         }
         time.Sleep(time.Second)
      }
   }
            } else {
                d.until++
                fn("!!!!!!No Running: ", d.until)
                if d.until > d.maxTry {
                    fn("!!!!!!Too Long Running, Reboot: ", d.maxTry)
                    os.Exit(0)
                }
            }
            time.Sleep(time.Second)
        }
    }
}