AFAIRE
sdk
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sdk/workers/workers.go

71 lines
1.6 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### sdk ####### Copyright (c) 2021 losyme ###################################################### MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package workers
import (
"sync"
"forge.chapril.org/dune/jw"
"forge.chapril.org/losyme/errors"
"forge.chapril.org/dune/sdk/runner"
)
type Runner interface {
Namespace() string
Model() runner.Model
RunJob(job *jw.Job, logger Logger)
}
type Workers struct {
supervisor *supervisor
dashboard *dashboard
group sync.WaitGroup
}
func New(cfg *Config) (*Workers, error) {
if err := cfg.validate(); err != nil {
return nil, errors.WithMessage(err, "sdk.workers") /////////////////////////////////////////////////////////////
}
dashboard := newDashboard()
pool := newPool(cfg.Host, cfg.Runner, dashboard, cfg.Logger)
supervisor := newSupervisor(cfg.PoolSize, pool)
ws := &Workers{
supervisor: supervisor,
dashboard: dashboard,
}
return ws, nil
}
func (ws *Workers) Start() {
ws.supervisor.start(&ws.group)
}
func (ws *Workers) AddOneWorker() {
ws.supervisor.addOneWorker()
}
func (ws *Workers) StopOneWorker() {
ws.supervisor.stopOneWorker()
}
func (ws *Workers) Dashboard() map[string]*Worker {
return ws.dashboard.data()
}
func (ws *Workers) Stop() {
ws.supervisor.stop()
ws.group.Wait()
}
/*
######################################################################################################## @(°_°)@ #######
*/