コード例 #1
0
ファイル: main.go プロジェクト: tomzhang/p2
func (r RCtl) RollingUpdate(oldID, newID string, want, need int, deletes bool) {
	if want < need {
		r.logger.WithFields(logrus.Fields{
			"want": want,
			"need": need,
		}).Fatalln("Cannot run update with desired replicas less than minimum replicas")
	}
	sessions := make(chan string)
	quit := make(chan struct{})

	go kp.ConsulSessionManager(api.SessionEntry{
		LockDelay: 1 * time.Nanosecond,
		Behavior:  api.SessionBehaviorDelete,
		TTL:       "15s",
	}, r.baseClient, sessions, quit, r.logger)

	session := <-sessions
	if session == "" {
		r.logger.NoFields().Fatalln("Could not acquire session")
	}
	lock := r.kps.NewUnmanagedLock(session, "")

	result := make(chan bool, 1)
	go func() {
		result <- roll.NewUpdate(roll_fields.Update{
			OldRC:           rc_fields.ID(oldID),
			NewRC:           rc_fields.ID(newID),
			DesiredReplicas: want,
			MinimumReplicas: need,
			DeletePods:      deletes,
		}, r.kps, r.rcs, r.hcheck, r.labeler, r.sched, r.logger, lock).Run(quit)
		close(result)
	}()

	signals := make(chan os.Signal, 2)
	signal.Notify(signals, syscall.SIGTERM, os.Interrupt)

LOOP:
	for {
		select {
		case <-signals:
			// try to clean up locks on ^C
			close(quit)
			// do not exit right away - the session and result channels will be
			// closed after the quit is requested, ensuring that the locks held
			// by the farm were released.
			r.logger.NoFields().Errorln("Got signal, exiting")
		case <-sessions:
			r.logger.NoFields().Fatalln("Lost session")
		case res := <-result:
			// done, either due to ^C (already printed message above) or
			// clean finish
			if res {
				r.logger.NoFields().Infoln("Done")
			}
			break LOOP
		}
	}
}
コード例 #2
0
ファイル: main.go プロジェクト: tomzhang/p2
func (r RCtl) Farm() {
	sessions := make(chan string)
	go kp.ConsulSessionManager(api.SessionEntry{
		LockDelay: 1 * time.Nanosecond,
		Behavior:  api.SessionBehaviorDelete,
		TTL:       "15s",
	}, r.baseClient, sessions, nil, r.logger)
	firstSession := <-sessions

	pub := stream.NewStringValuePublisher(sessions, firstSession)
	rcSub := pub.Subscribe(nil)
	rlSub := pub.Subscribe(nil)

	go rc.NewFarm(r.kps, r.rcs, r.sched, r.labeler, rcSub.Chan(), r.logger).Start(nil)
	roll.NewFarm(roll.UpdateFactory{
		KPStore:       r.kps,
		RCStore:       r.rcs,
		HealthChecker: r.hcheck,
		Labeler:       r.labeler,
		Scheduler:     r.sched,
	}, r.kps, r.rls, r.rcs, rlSub.Chan(), r.logger).Start(nil)
}