-
Notifications
You must be signed in to change notification settings - Fork 11
/
engine.go
137 lines (118 loc) · 3.4 KB
/
engine.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package ik
import (
"github.com/moriyoshi/ik/task"
"math/rand"
"time"
)
type recurringTaskDaemon struct {
engine *engineImpl
shutdown bool
}
func (daemon *recurringTaskDaemon) Run() error {
daemon.engine.recurringTaskScheduler.ProcessEvent()
if daemon.shutdown {
return nil
}
time.Sleep(1000000000)
return Continue
}
func (daemon *recurringTaskDaemon) Shutdown() error {
daemon.shutdown = true
daemon.engine.recurringTaskScheduler.NoOp()
return nil
}
type engineImpl struct {
logger Logger
opener Opener
lineParserPluginRegistry LineParserPluginRegistry
randSource rand.Source
scorekeeper *Scorekeeper
defaultPort Port
spawner *Spawner
pluginInstances []PluginInstance
taskRunner task.TaskRunner
recurringTaskScheduler *task.RecurringTaskScheduler
}
func (engine *engineImpl) Logger() Logger {
return engine.logger
}
func (engine *engineImpl) Opener() Opener {
return engine.opener
}
func (engine *engineImpl) LineParserPluginRegistry() LineParserPluginRegistry {
return engine.lineParserPluginRegistry
}
func (engine *engineImpl) RandSource() rand.Source {
return engine.randSource
}
func (engine *engineImpl) Scorekeeper() *Scorekeeper {
return engine.scorekeeper
}
func (engine *engineImpl) SpawneeStatuses() ([]SpawneeStatus, error) {
return engine.spawner.GetSpawneeStatuses()
}
func (engine *engineImpl) DefaultPort() Port {
return engine.defaultPort
}
func (engine *engineImpl) Dispose() error {
spawnees, err := engine.spawner.GetRunningSpawnees()
if err != nil {
return err
}
for _, spawnee := range spawnees {
engine.spawner.Kill(spawnee)
}
engine.spawner.PollMultiple(spawnees)
return nil
}
func (engine *engineImpl) Spawn(spawnee Spawnee) error {
return engine.spawner.Spawn(spawnee)
}
func (engine *engineImpl) Launch(pluginInstance PluginInstance) error {
var err error
spawnee, ok := pluginInstance.(Spawnee)
if ok {
err = engine.Spawn(spawnee)
if err != nil {
return err
}
}
engine.pluginInstances = append(engine.pluginInstances, pluginInstance)
return nil
}
func (engine *engineImpl) PluginInstances() []PluginInstance {
retval := make([]PluginInstance, len(engine.pluginInstances))
copy(retval, engine.pluginInstances)
return retval
}
func (engine *engineImpl) RecurringTaskScheduler() *task.RecurringTaskScheduler {
return engine.recurringTaskScheduler
}
func (engine *engineImpl) Start() error {
spawnees, err := engine.spawner.GetRunningSpawnees()
if err != nil {
return err
}
return engine.spawner.PollMultiple(spawnees)
}
func NewEngine(logger Logger, opener Opener, lineParserPluginRegistry LineParserPluginRegistry, scorekeeper *Scorekeeper, defaultPort Port) *engineImpl {
taskRunner := &task.SimpleTaskRunner{}
recurringTaskScheduler := task.NewRecurringTaskScheduler(
func() time.Time { return time.Now() },
taskRunner,
)
engine := &engineImpl{
logger: logger,
opener: opener,
lineParserPluginRegistry: lineParserPluginRegistry,
randSource: NewRandSourceWithTimestampSeed(),
scorekeeper: scorekeeper,
defaultPort: defaultPort,
spawner: NewSpawner(),
pluginInstances: make([]PluginInstance, 0),
taskRunner: taskRunner,
recurringTaskScheduler: recurringTaskScheduler,
}
engine.Spawn(&recurringTaskDaemon{engine, false})
return engine
}