Exemple #1
0
func Work() {
	cl, err := combainer.NewClient(combainer.COMBAINER_PATH)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Create client", cl)
	cl.Dispatch()
}
Exemple #2
0
func (c *CombaineServer) distributeTasks() {
LOCKSERVER_LOOP:
	for {
		DLS, err := lockserver.NewLockServer(c.CombainerConfig.LockServerSection)
		if err != nil {
			c.log.WithFields(logrus.Fields{
				"error": err,
			}).Error("unable to create Zookeeper lockserver")
			time.Sleep(c.Configuration.Period)
			continue LOCKSERVER_LOOP
		}

		var next <-chan time.Time
		next = time.After(time.Millisecond * 10)

	DISPATCH_LOOP:
		for {
			select {
			// Spawn one more client
			case <-next:
				next = time.After(c.Configuration.Period)

				configs, err := c.Repository.ListParsingConfigs()
				if err != nil {
					c.log.WithFields(logrus.Fields{
						"error": err,
					}).Error("unable to list parsing configs")
					continue DISPATCH_LOOP
				}

				go func(configs []string) {

					for _, cfg := range configs {
						lockerr := DLS.Lock(cfg)
						if lockerr != nil {
							continue
						}

						lockname := cfg

						// Inline function to use defers
						func(lockname string) {
							defer DLS.Unlock(lockname)
							defer Trap()

							if !c.Repository.ParsingConfigIsExists(cfg) {
								c.log.WithField("error", "config doesn't exist").Error(cfg)
								return
							}

							c.log.Info("creating new client", lockname)
							cl, err := combainer.NewClient(c.Context, c.Repository)
							// think about unique ID for metrics name
							combainer.GlobalMetrics.RegisterRegistry(cl.Registry, lockname)
							defer combainer.GlobalMetrics.UnregisterRegistry(lockname)

							if err != nil {
								c.log.WithFields(logrus.Fields{
									"error":    err,
									"lockname": lockname,
								}).Error("can't create client")
								return
							}

							var watcher <-chan zookeeper.Event
							watcher, err = DLS.Watch(lockname)
							if err != nil {
								c.log.WithFields(logrus.Fields{
									"error":    err,
									"lockname": lockname,
								}).Error("can't watch")
								return
							}

							for {
								if err := cl.Dispatch(lockname, GEN_UNIQUE_ID, SHOULD_WAIT); err != nil {
									c.log.WithFields(logrus.Fields{
										"error":    err,
										"lockname": lockname,
									}).Error("Dispatch error")
									return
								}
								select {
								case event := <-watcher:
									if !event.Ok() || event.Type == zookeeper.EVENT_DELETED {
										c.log.Error("lock has been lost: %s", event)
										return
									}
									watcher, err = DLS.Watch(lockname)
									if err != nil {
										c.log.WithFields(logrus.Fields{
											"error":    err,
											"lockname": lockname,
										}).Error("can't watch")
										return
									}
								default:
								}
							}
						}(lockname)
					}
				}(configs)
			case event := <-DLS.Session:
				if !event.Ok() {
					c.log.Errorf("not OK event from Zookeeper: %s", event)
					DLS.Close()
					break DISPATCH_LOOP
				}
			}

		}
	}

}