Exemple #1
0
func (w *worker) Recover() error {
	logger := w.wc.logger
	logger.Info("worker.Recover(): w=", w)
	if err := w.setS(WS_RECOVERING); err != nil {
		return err
	}

	var instance spi.Instance
	for instance == nil {
		instance := w.getInstanceForRecover()
		if instance == nil {
			return errors.New(fmt.Sprint("Cannot recover worker id=", w.Id(), " looks like state is lost"))
		}

		oldInstance := w.instance()
		if err := w.setSAI(WS_RECOVERING, w.desc.Aid, instance.Id()); err != nil {
			return err
		}

		if oldInstance != nil {
			logger.Info("worker.Recover(): stopping old instance")
			go func() {
				oldInstance.Stop()
			}()
		}

		app := w.wc.ap.Application(w.desc.Aid)
		if app == nil {
			logger.Warn("worker.Recover(): cannot find application, will move worker to STARTED state")
			go func() {
				w.startInstance(true)
			}()
			return errors.New("Cannot recover, application is not found for worker=" + w.String())
		}

		if err := app.Deploy(instance); err != nil {
			go func(instance spi.Instance) {
				instance.Stop()
			}(instance)
			w.wc.logger.Warn("worker.Recover(): failed to deploy application to the instance: ", err)
			instance = nil
		}
	}

	if err := w.setSAI(WS_DEPLOYED, w.desc.Aid, instance.Id()); err != nil {
		return err
	}
	return nil
}