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.

68 lines
1.6 KiB

/*
------------------------------------------------------------------------------------------------------------------------
####### sdk ####### Copyright (c) 2021-2022 losyme ################################################# MIT License #######
------------------------------------------------------------------------------------------------------------------------
*/
package workers
import (
"forge.chapril.org/dune/sdk"
"forge.chapril.org/losyme/errors"
"forge.chapril.org/losyme/zombie"
)
const _maxPoolSize = 20
type Logger interface {
zombie.Logger
Info(msg string, kv ...interface{})
Warning(msg string, kv ...interface{})
Error(msg string, kv ...interface{})
}
type Config struct {
PoolSize int `cfg:"pool_size"`
Model sdk.Model
Runner sdk.Runner
Logger Logger
}
func (c *Config) validate() error {
if c.PoolSize < 0 {
c.PoolSize = 0
} else if c.PoolSize > _maxPoolSize {
c.PoolSize = _maxPoolSize
}
if c.Model == nil {
return errors.New("field 'Model' cannot be nil") ///////////////////////////////////////////////////////////////
}
if c.Runner == nil {
return errors.New("field 'Runner' cannot be nil") //////////////////////////////////////////////////////////////
}
return nil
}
func (c *Config) BuildWorkers() (*Workers, error) {
if err := c.validate(); err != nil {
return nil, err
}
state := newState()
pool := newPool(c, state)
supervisor := newSupervisor(c.PoolSize, c.Logger, pool)
ws := &Workers{
state: state,
supervisor: supervisor,
}
return ws, nil
}
/*
######################################################################################################## @(°_°)@ #######
*/