Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions v4/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const (
EventTypeServiceTerminate
EventTypeBackoff
EventTypeResume
EventTypeServiceStart
EventTypeServiceStop
)

type EventStopTimeout struct {
Expand Down Expand Up @@ -184,3 +186,47 @@ func (e EventResume) Map() map[string]interface{} {
"supervisor_name": e.SupervisorName,
}
}

type EventServiceStart struct {
Supervisor *Supervisor `json:"-"`
SupervisorName string `json:"supervisor_name"`
Service Service `json:"-"`
ServiceName string `json:"service_name"`
}

func (e EventServiceStart) Type() EventType {
return EventTypeServiceStart
}

func (e EventServiceStart) String() string {
return fmt.Sprintf("%s: Service %s started.", e.Supervisor, e.Service)
}

func (e EventServiceStart) Map() map[string]interface{} {
return map[string]interface{}{
"supervisor_name": e.SupervisorName,
"service_name": e.ServiceName,
}
}

type EventServiceStop struct {
Supervisor *Supervisor `json:"-"`
SupervisorName string `json:"supervisor_name"`
Service Service `json:"-"`
ServiceName string `json:"service_name"`
}

func (e EventServiceStop) Type() EventType {
return EventTypeServiceStop
}

func (e EventServiceStop) String() string {
return fmt.Sprintf("%s: Service %s stopped.", e.Supervisor, e.Service)
}

func (e EventServiceStop) Map() map[string]interface{} {
return map[string]interface{}{
"supervisor_name": e.SupervisorName,
"service_name": e.ServiceName,
}
}
17 changes: 10 additions & 7 deletions v4/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (s *Supervisor) Serve(ctx context.Context) error {
for _, id := range s.restartQueue {
namedService, present := s.services[id]
if present {
s.runService(ctx, namedService.Service, id)
s.runService(ctx, namedService, id)
}
}
s.restartQueue = make([]serviceID, 0, 1)
Expand Down Expand Up @@ -387,8 +387,9 @@ func (s *Supervisor) Serve(ctx context.Context) error {
id := s.serviceCounter
s.serviceCounter++

s.services[id] = serviceWithName{msg.service, msg.name}
s.runService(ctx, msg.service, id)
namedService := serviceWithName{msg.service, msg.name}
s.services[id] = namedService
s.runService(ctx, namedService, id)

msg.response <- id
case removeService:
Expand Down Expand Up @@ -424,7 +425,7 @@ func (s *Supervisor) Serve(ctx context.Context) error {
for _, id := range s.restartQueue {
namedService, present := s.services[id]
if present {
s.runService(ctx, namedService.Service, id)
s.runService(ctx, namedService, id)
}
}
s.restartQueue = make([]serviceID, 0, 1)
Expand Down Expand Up @@ -499,7 +500,7 @@ func (s *Supervisor) handleFailedService(ctx context.Context, id serviceID, err
curState := s.state
s.m.Unlock()
if curState == normal {
s.runService(ctx, failedService.Service, id)
s.runService(ctx, failedService, id)
} else {
s.restartQueue = append(s.restartQueue, id)
}
Expand Down Expand Up @@ -533,7 +534,7 @@ func (s *Supervisor) handleFailedService(ctx context.Context, id serviceID, err
}
}

func (s *Supervisor) runService(ctx context.Context, service Service, id serviceID) {
func (s *Supervisor) runService(ctx context.Context, namedService serviceWithName, id serviceID) {
childCtx, cancel := context.WithCancel(ctx)
done := make(chan struct{})
blockingCancellation := func() {
Expand Down Expand Up @@ -562,13 +563,15 @@ func (s *Supervisor) runService(ctx context.Context, service Service, id service
r := recover()
if r == nil {
s.serviceEnded(id, err)
s.spec.EventHook(EventServiceStop{s, s.Name, namedService.Service, namedService.name})
} else {
panic(r)
}
}()

err = service.Serve(childCtx)
err = namedService.Service.Serve(childCtx)
}()
s.spec.EventHook(EventServiceStart{s, s.Name, namedService.Service, namedService.name})
}

func (s *Supervisor) removeService(id serviceID, notificationChan chan struct{}) {
Expand Down