コード例 #1
0
ファイル: spirit_classic.go プロジェクト: jmptrader/spirit
func (p *ClassicSpirit) buildEventInspectSubscribers() (err error) {
	if !p.inspectMode {
		return
	}

	events := EventCenter.ListEvents()

	loggerSubscriber := event_center.NewSubscriber(func(eventName string, values ...interface{}) {

		str := ""
		for i, v := range values {
			str = str + fmt.Sprintf("v_%d: %v\n", i, v)
		}

		logs.Info(fmt.Sprintf("SPIRIT-EVENT-INSPECT EVENT_NAME: %s\nValues:\n%s\n", eventName, str))

	})

	for _, event := range events {
		if err = EventCenter.Subscribe(event, loggerSubscriber); err != nil {
			return
		}
	}

	return
}
コード例 #2
0
ファイル: spirit_classic.go プロジェクト: jmptrader/spirit
func (p *ClassicSpirit) waitSignal() {
	isStopping := false
	interrupt := make(chan os.Signal, 1)
	signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM, syscall.SIGUSR1)

	for {
		select {
		case signal := <-interrupt:
			logs.Info("singal received", signal)
			switch signal {
			case os.Interrupt, syscall.SIGTERM:
				{
					if isStopping {
						os.Exit(0)
					}

					isStopping = true
					EventCenter.PushEvent(EVENT_CMD_STOP)

					go func() {
						for _, runningComponent := range p.runningComponents {
							go runningComponent.Stop()
						}

						wg := sync.WaitGroup{}

						for _, runningComponent := range p.runningComponents {
							wg.Add(1)

							go func(component Component) {
								defer wg.Done()

								for component.Status() != STATUS_STOPPED {
									time.Sleep(time.Second)
								}
								logs.Info(fmt.Sprintf("[spirit] component %s was gracefully stoped\n", component.Name()))
							}(runningComponent)
						}
						wg.Wait()
						os.Exit(0)
					}()

				}
			case syscall.SIGUSR1:
				{
					for _, runningComponent := range p.runningComponents {
						if runningComponent.Status() == STATUS_RUNNING {
							runningComponent.Pause()
							if runningComponent.Status() == STATUS_PAUSED {
								logs.Info(fmt.Sprintf("spirit - component %s was paused\n", runningComponent.Name()))
							}
						} else if runningComponent.Status() == STATUS_PAUSED {
							runningComponent.Resume()
							if runningComponent.Status() == STATUS_RUNNING {
								logs.Info(fmt.Sprintf("spirit - component %s was resumed\n", runningComponent.Name()))
							}
						}
					}
				}
			}
		case <-time.After(time.Second):
			{
				continue
			}
		}
	}
}