mirror of
https://gitea.com/gitea/act_runner.git
synced 2025-06-22 14:28:00 +02:00
chore(poller): Add poller package
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
52
poller/poller.go
Normal file
52
poller/poller.go
Normal file
@ -0,0 +1,52 @@
|
||||
package poller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.com/gitea/act_runner/client"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func New(cli client.Client) *Poller {
|
||||
return &Poller{
|
||||
Client: cli,
|
||||
routineGroup: newRoutineGroup(),
|
||||
}
|
||||
}
|
||||
|
||||
type Poller struct {
|
||||
Client client.Client
|
||||
|
||||
routineGroup *routineGroup
|
||||
}
|
||||
|
||||
func (p *Poller) Poll(ctx context.Context, n int) {
|
||||
for i := 0; i < n; i++ {
|
||||
func(i int) {
|
||||
p.routineGroup.Run(func() {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Infof("stopped the runner: %d", i+1)
|
||||
return
|
||||
default:
|
||||
if ctx.Err() != nil {
|
||||
log.Infof("stopping the runner: %d", i+1)
|
||||
return
|
||||
}
|
||||
if err := p.poll(ctx, i+1); err != nil {
|
||||
log.WithError(err).Error("poll error")
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}(i)
|
||||
}
|
||||
p.routineGroup.Wait()
|
||||
}
|
||||
|
||||
func (p *Poller) poll(ctx context.Context, thread int) error {
|
||||
log.WithField("thread", thread).Info("poller: request stage from remote server")
|
||||
|
||||
return nil
|
||||
}
|
24
poller/thread.go
Normal file
24
poller/thread.go
Normal file
@ -0,0 +1,24 @@
|
||||
package poller
|
||||
|
||||
import "sync"
|
||||
|
||||
type routineGroup struct {
|
||||
waitGroup sync.WaitGroup
|
||||
}
|
||||
|
||||
func newRoutineGroup() *routineGroup {
|
||||
return new(routineGroup)
|
||||
}
|
||||
|
||||
func (g *routineGroup) Run(fn func()) {
|
||||
g.waitGroup.Add(1)
|
||||
|
||||
go func() {
|
||||
defer g.waitGroup.Done()
|
||||
fn()
|
||||
}()
|
||||
}
|
||||
|
||||
func (g *routineGroup) Wait() {
|
||||
g.waitGroup.Wait()
|
||||
}
|
Reference in New Issue
Block a user