Пример #1
0
func Init(listen, location, ttlString *string, errorChannel chan error) error {
	if location != nil {

		conn, err := bolt.Open(*location, 0644, nil)

		if err != nil {
			return err
		}

		manager = &Manager{
			errorChannel: errorChannel,
			conn:         conn,
			mutex:        sync.RWMutex{},
		}

		if ttlString != nil && len(*ttlString) > 0 {
			ttl, err := config.ParseTimeInterval(*ttlString)
			if err != nil {
				return err
			}
			manager.ttl = ttl
		} else {
			manager.ttl = 0
		}

		// Create default buckets
		err = conn.Update(func(tx *bolt.Tx) error {

			if _, err := tx.CreateBucketIfNotExists([]byte("_counters")); err != nil {
				return err
			}

			if _, err := tx.CreateBucketIfNotExists([]byte("_series")); err != nil {
				return err
			}

			if _, err := tx.CreateBucketIfNotExists([]byte("_oauth")); err != nil {
				return err
			}

			return nil
		})

		return err
	}
	log.Printf("Error: %s", "Data Manager -> No `data.path` property provided. The Data Manager will not run.")

	return nil
}
)

var seriesFunctions = map[string]func(s *aggregations.Series) lua.Function{
	"name": func(s *aggregations.Series) lua.Function {
		return func(l *lua.State) int {
			util.DeepPush(l, s.Name)

			return 1
		}
	},

	"trimSince": func(s *aggregations.Series) lua.Function {
		return func(l *lua.State) int {

			if l.TypeOf(1) == lua.TypeString {
				duration, err := config.ParseTimeInterval(lua.CheckString(l, 1))

				if err != nil {
					lua.Errorf(l, "%s", err)
					panic("unreachable")
				}

				since := time.Now().Add(-duration)

				if err = s.TrimSince(since); err != nil {
					lua.Errorf(l, "%s", err)
					panic("unreachable")
				}

				return 0
			}
Пример #3
0
func (p *ProcessPlugin) Init(job *job.Job) error {
	c := job.Config()

	job.Debugf("The configuration is %#v", c)

	var ok bool

	p.flowTag, ok = c["flow_tag"].(string)

	if !ok {
		p.flowTag, _ = c["tag"].(string)
	}

	p.batch, ok = c["batch"].(bool)

	if ok && p.flowTag != "" {
		return errors.New("You cannot specify both `flow_tag` and `batch` properties.")
	}

	exec, _ := c["exec"].(string)
	script, _ := c["script"].(string)

	if exec != "" && script != "" {
		return errors.New("You cannot specify both `script` and `exec` properties.")
	}

	if exec != "" {
		p.path = exec
	} else if script != "" {
		p.path = script
	}

	p.url, _ = c["url"].(string)

	if p.path == "" && p.url == "" {
		return errors.New("You must specify a `script`, `exec`, or `url` property.")
	}

	if p.path != "" && p.url != "" {
		return errors.New("You cannot provide both `script` or `exec` and `url` properties.")
	}

	p.args = []string{}
	p.scriptArgs = map[string]interface{}{}

	if args, ok := c["args"].([]interface{}); ok {
		for _, arg := range args {
			if a, ok := arg.(string); ok {
				p.args = append(p.args, a)
			} else {
				p.args = append(p.args, fmt.Sprintf("%#v", arg))
			}
		}
	} else if args, ok := c["args"].(map[interface{}]interface{}); ok {
		p.scriptArgs = config.MapTemplate(args).(map[string]interface{})
	} else if args, ok := c["args"].(map[string]interface{}); ok {
		p.scriptArgs = args
	}

	if p.path != "" {
		if _, err := os.Stat(p.path); os.IsNotExist(err) {
			return errors.New("File " + p.path + " does not exist.")
		}

		if path.Ext(p.path) == ".lua" {
			p.url = "tpl://" + p.path
			p.path = ""
		} else {
			if len(p.scriptArgs) != 0 {
				return errors.New("You cannot specify an key/value hash of arguments when executing an external process. Provide an array of arguments instead.")
			}
		}
	}

	if p.url != "" {
		if len(p.args) != 0 {
			return errors.New("You cannot specify an array of arguments when executing a template. Provide a key/value hash instead.")
		}

		if strings.HasPrefix(p.url, "tpl://") {
			URL, err := url.Parse(p.url)

			if err != nil {
				return err
			}

			p.templateFile = URL.Host + URL.Path

			if _, err := os.Stat(p.templateFile); os.IsNotExist(err) {
				return errors.New("Template " + p.templateFile + " does not exist.")
			}
		}
	}

	template, templateOK := c["template"]
	variant, variantOK := c["variant"].(string)

	if variantOK && templateOK {
		if p.flowTag == "" {
			return errors.New("The required `flow_tag` property (`string`) is either missing or of the wrong type.")
		}

		if f, err := job.GetOrCreateFlow(p.flowTag, variant, template); err != nil {
			return err
		} else {
			p.flow = f
		}
	}

	if interval, ok := c["interval"].(int); ok {
		p.expiration = time.Duration(interval*3) * time.Second

		p.PluginHelper.AddTaskWithClosure(p.performAllTasks, time.Duration(interval)*time.Second)
	} else if interval, ok := c["interval"].(string); ok {
		if timeInterval, err := config.ParseTimeInterval(interval); err == nil {
			if p.expiration == 0 {
				p.expiration = timeInterval * 3.0
			}

			p.PluginHelper.AddTaskWithClosure(p.performAllTasks, timeInterval)
		} else {
			return err
		}
	} else {
		p.PluginHelper.AddTaskWithClosure(p.performAllTasks, 0)
	}

	if p.expiration > 0 && p.expiration < time.Second*60 {
		p.expiration = time.Second * 60
	}

	switch c["expiration"].(type) {
	case int:
		p.expiration = time.Duration(c["expiration"].(int)) * time.Second

	case int64:
		p.expiration = time.Duration(c["expiration"].(int64)) * time.Second

	case string:
		if timeInterval, err := config.ParseTimeInterval(c["expiration"].(string)); err == nil {
			p.expiration = timeInterval
		} else {
			return errors.New("Invalid expiration value. Must be either a number of seconds or a time interval string.")
		}
	}

	if p.expiration < 0 {
		return errors.New("Invalid expiration time")
	}

	if p.expiration > 0 {
		job.Debugf("Expiration is set to %dµs", p.expiration)
	} else {
		job.Debugf("Expiration is off.")
	}

	return nil
}