Ejemplo n.º 1
0
func startFileQuicklog(ctx context.Context, system *managed.System) {

	log.Log(ctx).Info("Loading config from file", "file", configFile)

	// load config
	cfg, err := config.LoadFile(configFile)
	if err != nil {
		log.Log(ctx).Error("Error loading configuration", "error", err)
		os.Exit(255)
		return
	}

	// setup chain
	chain := fromConfig(ctx, cfg)

	// execute chain
	system.Add(managed.Simple("chain", chain.Execute))

	return
}
Ejemplo n.º 2
0
func startEtcdQuicklog(mainCtx context.Context, system *managed.System) {

	log.Log(mainCtx).Info("Loading config from etcd")

	log.Log(mainCtx).Debug("Connecting to endpoint", "endpoints", strings.Split(etcdEndpoints, ","))

	etcdCfg := client.Config{
		Endpoints: strings.Split(etcdEndpoints, ","),
	}

	c, err := client.New(etcdCfg)
	if err != nil {
		log.Log(mainCtx).Crit("Error connecting to etcd server", "error", err)
		return
	}
	root := "/quicklog/" + instanceName

	log.Log(mainCtx).Debug("Listening for etcd keys", "key", root)

	kapi := client.NewKeysAPI(c)

	chainApp := managed.NewSystem("app-chain-" + instanceName)
	system.SpawnSystem(chainApp)

	var cfg config.Config

	err = syncFromEtcd(mainCtx, root, kapi, &cfg)
	if err != nil {
		log.Log(mainCtx).Error("Error syncing from etcd", "error", err)
	}

	// setup chain
	chain := fromConfig(mainCtx, &cfg)

	chainApp.Add(managed.Simple("chain-sub-"+instanceName, chain.Execute))

	system.Add(managed.Simple("etcd", func(ctx context.Context) {

		w := kapi.Watcher(root+"/reload", &client.WatcherOptions{
			Recursive: false,
		})

		for {
			resp, err := w.Next(ctx)

			if err != nil {
				if err == context.DeadlineExceeded {
					continue
				} else if err == context.Canceled {
					return
				} else if cerr, ok := err.(*client.ClusterError); ok {
					for _, e := range cerr.Errors {
						if e != context.Canceled {
							log.Log(ctx).Error("Error getting next etcd watch event", "parentError", err, "error", e)
						}
					}
				} else {
					log.Log(ctx).Error("Error getting next etcd watch event", "error", err)
				}

				return
			}
			if resp == nil {
				return
			}

			switch resp.Action {
			case "get":
				// do nothing
			default:
				log.Log(ctx).Info("Got update on quicklog config", "etcd.action", resp.Action)

				var newCfg config.Config

				err = syncFromEtcd(ctx, root, kapi, &newCfg)
				if err != nil {
					log.Log(ctx).Error("Error syncing from etcd", "error", err)
				} else {

					chainApp.Stop()
					<-time.After(1 * time.Second)
					chainApp = managed.NewSystem("app-chain-" + instanceName)
					system.SpawnSystem(chainApp)

					// setup chain
					chain = fromConfig(ctx, &newCfg)

					chainApp.Add(managed.Simple("chain-sub-"+instanceName, chain.Execute))
				}
				//TODO: (re)load config
			}
		}
	}))
}