| 1 |
package controller |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
|
| 6 |
"github.com/mesos/mesos-go/api/v1/lib" |
| 7 |
"github.com/mesos/mesos-go/api/v1/lib/encoding" |
| 8 |
"github.com/mesos/mesos-go/api/v1/lib/scheduler" |
| 9 |
"github.com/mesos/mesos-go/api/v1/lib/scheduler/calls" |
| 10 |
"github.com/mesos/mesos-go/api/v1/lib/scheduler/events" |
| 11 |
) |
| 12 |
|
| 13 |
type ( |
| 14 |
|
| 15 |
Option func(*Config) Option |
| 16 |
|
| 17 |
|
| 18 |
Config struct { |
| 19 |
frameworkIDFunc func() string |
| 20 |
handler events.Handler |
| 21 |
registrationTokens <-chan struct{} |
| 22 |
subscriptionTerminated func(error) |
| 23 |
} |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
func WithEventHandler(handler events.Handler) Option { |
| 30 |
return func(c *Config) Option { |
| 31 |
old := c.handler |
| 32 |
c.handler = handler |
| 33 |
return WithEventHandler(old) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
func WithFrameworkID(frameworkIDFunc func() string) Option { |
| 42 |
return func(c *Config) Option { |
| 43 |
old := c.frameworkIDFunc |
| 44 |
c.frameworkIDFunc = frameworkIDFunc |
| 45 |
return WithFrameworkID(old) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
func WithSubscriptionTerminated(handler func(error)) Option { |
| 53 |
return func(c *Config) Option { |
| 54 |
old := c.subscriptionTerminated |
| 55 |
c.subscriptionTerminated = handler |
| 56 |
return WithSubscriptionTerminated(old) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
func WithRegistrationTokens(registrationTokens <-chan struct{}) Option { |
| 65 |
return func(c *Config) Option { |
| 66 |
old := c.registrationTokens |
| 67 |
c.registrationTokens = registrationTokens |
| 68 |
return WithRegistrationTokens(old) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func (c *Config) tryFrameworkID() (result string) { |
| 73 |
if c.frameworkIDFunc != nil { |
| 74 |
result = c.frameworkIDFunc() |
| 75 |
} |
| 76 |
return |
| 77 |
} |
| 78 |
|
| 79 |
func isDone(ctx context.Context) (result bool) { |
| 80 |
select { |
| 81 |
case <-ctx.Done(): |
| 82 |
return true |
| 83 |
default: |
| 84 |
return false |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
func Run(ctx context.Context, framework *mesos.FrameworkInfo, caller calls.Caller, options ...Option) (lastErr error) { |
| 92 |
var config Config |
| 93 |
for _, opt := range options { |
| 94 |
if opt != nil { |
| 95 |
opt(&config) |
| 96 |
} |
| 97 |
} |
| 98 |
if config.handler == nil { |
| 99 |
config.handler = DefaultHandler |
| 100 |
} |
| 101 |
subscribe := calls.Subscribe(framework) |
| 102 |
for !isDone(ctx) { |
| 103 |
frameworkID := config.tryFrameworkID() |
| 104 |
if framework.GetFailoverTimeout() > 0 && frameworkID != "" { |
| 105 |
subscribe.With(calls.SubscribeTo(frameworkID)) |
| 106 |
} |
| 107 |
if config.registrationTokens != nil { |
| 108 |
select { |
| 109 |
case _, ok := <-config.registrationTokens: |
| 110 |
if !ok { |
| 111 |
|
| 112 |
return |
| 113 |
} |
| 114 |
case <-ctx.Done(): |
| 115 |
return ctx.Err() |
| 116 |
} |
| 117 |
} |
| 118 |
resp, err := caller.Call(ctx, subscribe) |
| 119 |
lastErr = processSubscription(ctx, config, resp, err) |
| 120 |
if config.subscriptionTerminated != nil { |
| 121 |
config.subscriptionTerminated(lastErr) |
| 122 |
} |
| 123 |
} |
| 124 |
return |
| 125 |
} |
| 126 |
|
| 127 |
func processSubscription(ctx context.Context, config Config, resp mesos.Response, err error) error { |
| 128 |
if resp != nil { |
| 129 |
defer resp.Close() |
| 130 |
} |
| 131 |
if err == nil { |
| 132 |
err = eventLoop(ctx, config, resp) |
| 133 |
} |
| 134 |
return err |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
func eventLoop(ctx context.Context, config Config, eventDecoder encoding.Decoder) (err error) { |
| 140 |
for err == nil && !isDone(ctx) { |
| 141 |
var e scheduler.Event |
| 142 |
if err = eventDecoder.Decode(&e); err == nil { |
| 143 |
err = config.handler.HandleEvent(ctx, &e) |
| 144 |
} |
| 145 |
} |
| 146 |
return err |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
const DefaultHandler = events.NoopHandler |