goploy/task/DeployTask.go

54 lines
1.2 KiB
Go
Raw Normal View History

2021-12-17 17:55:26 +08:00
package task
import (
"container/list"
"github.com/zhenorzz/goploy/config"
"github.com/zhenorzz/goploy/service"
"github.com/zhenorzz/goploy/ws"
2022-01-08 21:35:57 +08:00
"sync"
2021-12-17 17:55:26 +08:00
"sync/atomic"
"time"
)
var deployList = list.New()
var deployTick = time.Tick(time.Millisecond)
func startDeployTask() {
atomic.AddInt32(&counter, 1)
var deployingNumber int32
2022-01-08 21:35:57 +08:00
var wg sync.WaitGroup
2021-12-17 17:55:26 +08:00
go func() {
for {
select {
case <-deployTick:
if atomic.LoadInt32(&deployingNumber) < config.Toml.APP.DeployLimit {
atomic.AddInt32(&deployingNumber, 1)
deployElem := deployList.Front()
if deployElem != nil {
2022-01-08 21:35:57 +08:00
wg.Add(1)
2021-12-17 17:55:26 +08:00
go func() {
deployList.Remove(deployElem).(service.Gsync).Exec()
atomic.AddInt32(&deployingNumber, -1)
2022-01-08 21:35:57 +08:00
wg.Done()
2021-12-17 17:55:26 +08:00
}()
} else {
atomic.AddInt32(&deployingNumber, -1)
}
}
2021-12-18 14:51:59 +08:00
case <-stop:
2022-01-08 21:35:57 +08:00
wg.Wait()
2021-12-17 17:55:26 +08:00
atomic.AddInt32(&counter, -1)
return
}
}
}()
}
func AddDeployTask(gSync service.Gsync) {
ws.GetHub().Data <- &ws.Data{
Type: ws.TypeProject,
Message: ws.ProjectMessage{ProjectID: gSync.Project.ID, ProjectName: gSync.Project.Name, State: ws.TaskWaiting, Message: "Task waiting"},
}
deployList.PushBack(gSync)
}