/
config.go
53 lines (48 loc) · 1.73 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package ellie
const (
DEFAULT_WORKER_COUNT = 5
DEFAULT_WORK_INTERVAL = 5 // in seconds
)
// appConfig is the configuration object to use within the actual module.
var AppConfig *Config
// Config contains the base configuration for the work queue.
type Config struct {
// NumWorkers specifies the maximum number of active workers to run at any
// given time.
NumWorkers int
// WorkInterval is the time it takes for a worker to sleep before it
// checks the task queue for more work to do.
WorkInterval int
// ScheduledTasks is the default queue used to decide what is available
// for the workers to consume.
ScheduledTasks TaskQueue
// CancelledTasks is a queue which is checked before a task is executed to
// see if the task has been cancelled.
CancelledTasks TaskDequeue
// NewTasks is a signal channel to express that a new task has been pushed
// to the ScheduledTasks queue.
NewTasks chan bool
// WorkerPool in a channel to wait for a worker when a job comes in and
// we send workers back into it when they are done.
WorkerPool chan *Worker
// FinishedTasks is a channel which cleans up after a task has finished.
FinishedTasks chan *Task
}
// Configure sets up the base application confiuration options.
func Configure(numWorkers, workInterval int) *Config {
config := &Config{
NumWorkers: numWorkers,
WorkInterval: workInterval,
ScheduledTasks: NewQueue(),
CancelledTasks: NewDequeue(),
NewTasks: make(chan bool, 100),
FinishedTasks: make(chan *Task, 100),
WorkerPool: make(chan *Worker, numWorkers),
}
AppConfig = config
return config
}
// DefaultConfig uses the defaults to configure the application.
func DefaultConfig() *Config {
return Configure(DEFAULT_WORKER_COUNT, DEFAULT_WORK_INTERVAL)
}