mirror of
https://gitee.com/goploy/goploy.git
synced 2024-12-04 21:21:40 +08:00
48 lines
682 B
Go
48 lines
682 B
Go
package task
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
var counter int32
|
|
var stop = make(chan struct{})
|
|
|
|
func Init() {
|
|
atomic.AddInt32(&counter, 1)
|
|
go ticker()
|
|
}
|
|
|
|
func ticker() {
|
|
defer atomic.AddInt32(&counter, -1)
|
|
// create ticker
|
|
minute := time.Tick(time.Minute)
|
|
second := time.Tick(time.Second)
|
|
for {
|
|
select {
|
|
case <-second:
|
|
monitorTask()
|
|
case <-minute:
|
|
projectTask()
|
|
case <-stop:
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func Shutdown(ctx context.Context) error {
|
|
close(stop)
|
|
ticker := time.NewTicker(10 * time.Millisecond)
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-ticker.C:
|
|
if atomic.LoadInt32(&counter) == 0 {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
}
|