Esempio n. 1
0
func main() {

	flag.Parse()

	if verFlag {
		fmt.Printf("%s\n", version)
		return
	}

	// Setup context
	ctx := context.Background()
	ctx = log.NewContext(ctx)
	log.Log(ctx).Info("Starting quicklog")

	// Setup system
	system := managed.NewSystem("quicklog")
	system.StartWithContext(ctx)

	// Register signal listeners
	system.RegisterForStop(os.Interrupt, os.Kill)

	switch {
	case etcdEndpoints != "" && instanceName != "":
		startEtcdQuicklog(ctx, system)
	case configFile != "":
		startFileQuicklog(ctx, system)
	}

	system.Wait()
}
Esempio n. 2
0
func main() {

	system := managed.NewSystem("app1")
	system.Start()

	sender := make(chan int)
	accum := make(chan int)

	go func() {
		a := 0
		for i := 0; i < 5; i++ {
			select {
			case val := <-accum:
				a += val
			}
		}

		fmt.Printf("Got: %d\n", a)
	}()

	for _, i := range []int{1, 2, 3, 4, 5} {
		name := "my." + strconv.FormatInt(int64(i), 10)
		x := managed.Simple("my."+strconv.FormatInt(int64(i), 10), mySubprocessFactory(name, sender, accum))
		system.Add(x)
	}

	system.Add(managed.Timer("printer", 3*time.Millisecond, true, func(ctx context.Context) {
		system.WriteTree(managed.TextWriter(os.Stdout, 1))
		system.Stop()
	}))

	go func() {
		sender <- 1
		sender <- 2
		sender <- 3
		sender <- 4
		sender <- 5
	}()

	system.Wait()
}
Esempio n. 3
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
			}
		}
	}))
}