Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinZonda committed May 5, 2023
1 parent 2022f7c commit bbf2c29
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,17 @@ type Scheduler struct {
}

func NewScheduler(tokens []string) *Scheduler {
sche := &Scheduler{
startedDaemon: false,
stopCh: make(chan bool),
}
var gpts []*Client
var clis []*Client
for _, token := range tokens {
gpts = append(gpts, NewClient(token))
}
if len(gpts) == 0 {
panic("no gpt3 token")
clis = append(clis, NewClient(token))
}
sche.workableCh = make(chan *Client, len(gpts))
sche.brokenCh = make(chan *Client, len(gpts))
sche.outOfServiceCh = make(chan *Client, len(gpts))
for _, gpt := range gpts {
sche.workableCh <- gpt
}
//sche.statistic()
return sche
return NewSchedulerFromClients(clis)
}

func NewSchedulerFromClients(clients []*Client) *Scheduler {
if len(clients) == 0 {
panic("no gpt token")
}
sche := &Scheduler{
startedDaemon: false,
}
Expand All @@ -53,8 +42,9 @@ func (s *Scheduler) StartDaemon() {
if s.startedDaemon {
return
}
go s.daemon()
s.startedDaemon = true
go s.daemon()
s.Statistic()
}

func (s *Scheduler) Dispose() {
Expand All @@ -75,18 +65,18 @@ func (s *Scheduler) GetWorkable() int {
}

func (s *Scheduler) daemon() {
select {
case <-s.stopCh:
return
default:
gpt := <-s.outOfServiceCh
time.Sleep(time.Minute)
gpt.Status = OK
s.workableCh <- gpt
fmt.Println("[GPT SCHEDULER]", gpt.Identity, "-> OK")
s.Statistic()
for {
select {
case <-s.stopCh:
return
case gpt := <-s.outOfServiceCh:
time.Sleep(time.Minute)
gpt.Status = OK
s.workableCh <- gpt
fmt.Println("[GPT SCHEDULER]", gpt.Identity, "-> OK")
s.Statistic()
}
}

}

func (s *Scheduler) Statistic() {
Expand Down

0 comments on commit bbf2c29

Please # to comment.