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 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()
}
Ejemplo 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
			}
		}
	}))
}