示例#1
0
func (ts *Service) Load(name string) (*kapacitor.Task, error) {
	task, err := ts.LoadRaw(name)
	if err != nil {
		return nil, err
	}
	return kapacitor.NewTask(task.Name, task.TICKscript, task.Type, task.DBRPs)
}
示例#2
0
func (ts *Service) Save(task *rawTask) error {

	// Validate task
	_, err := kapacitor.NewTask(task.Name, task.TICKscript, task.Type, task.DBRPs)
	if err != nil {
		return fmt.Errorf("invalid task: %s", err)
	}

	var buf bytes.Buffer

	enc := gob.NewEncoder(&buf)
	err = enc.Encode(task)
	if err != nil {
		return err
	}

	err = ts.db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists(tasksBucket)
		if err != nil {
			return err
		}
		exists := b.Get([]byte(task.Name)) != nil
		err = b.Put([]byte(task.Name), buf.Bytes())
		if err != nil {
			return err
		}
		if !exists {
			kapacitor.NumTasks.Add(1)
		}
		return nil
	})
	return err
}
示例#3
0
// Helper test function for batcher
func testBatcher(t *testing.T, name, script string) (clock.Setter, *kapacitor.ExecutingTask, <-chan error, *kapacitor.TaskMaster) {
	if testing.Verbose() {
		wlog.SetLevel(wlog.DEBUG)
	} else {
		wlog.SetLevel(wlog.OFF)
	}

	// Create a new execution env
	tm := kapacitor.NewTaskMaster(logService)
	tm.HTTPDService = httpService
	tm.TaskStore = taskStore{}
	tm.Open()
	scope := tm.CreateTICKScope()

	// Create task
	task, err := kapacitor.NewTask(name, script, kapacitor.BatchTask, dbrps, 0, scope)
	if err != nil {
		t.Fatal(err)
	}

	// Load test data
	var allData []io.ReadCloser
	var data io.ReadCloser
	for i := 0; err == nil; {
		f := fmt.Sprintf("%s.%d.brpl", name, i)
		data, err = os.Open(path.Join("data", f))
		if err == nil {
			allData = append(allData, data)
			i++
		}
	}
	if len(allData) == 0 {
		t.Fatal("could not find any data files for", name)
	}

	// Use 1971 so that we don't get true negatives on Epoch 0 collisions
	c := clock.New(time.Date(1971, 1, 1, 0, 0, 0, 0, time.UTC))
	r := kapacitor.NewReplay(c)

	//Start the task
	et, err := tm.StartTask(task)
	if err != nil {
		t.Fatal(err)
	}

	// Replay test data to executor
	batches := tm.BatchCollectors(name)
	replayErr := r.ReplayBatch(allData, batches, false)

	t.Log(string(et.Task.Dot()))
	return r.Setter, et, replayErr, tm
}
示例#4
0
func (ts *Service) Load(name string) (*kapacitor.Task, error) {
	task, err := ts.LoadRaw(name)
	if err != nil {
		return nil, err
	}
	return kapacitor.NewTask(task.Name,
		task.TICKscript,
		task.Type,
		task.DBRPs,
		task.SnapshotInterval,
		ts.TaskMaster.CreateTICKScope(),
	)
}
示例#5
0
func (ts *Service) Save(task *rawTask) error {

	// Validate task
	_, err := kapacitor.NewTask(task.Name,
		task.TICKscript,
		task.Type,
		task.DBRPs,
		task.SnapshotInterval,
		ts.TaskMaster.CreateTICKScope(),
	)
	if err != nil {
		return fmt.Errorf("invalid task: %s", err)
	}

	// Write 0 snapshot interval if it is the default.
	// This way if the default changes the task will change too.
	if task.SnapshotInterval == ts.snapshotInterval {
		task.SnapshotInterval = 0
	}

	var buf bytes.Buffer

	enc := gob.NewEncoder(&buf)
	err = enc.Encode(task)
	if err != nil {
		return err
	}

	err = ts.db.Update(func(tx *bolt.Tx) error {
		b, err := tx.CreateBucketIfNotExists(tasksBucket)
		if err != nil {
			return err
		}
		exists := b.Get([]byte(task.Name)) != nil
		err = b.Put([]byte(task.Name), buf.Bytes())
		if err != nil {
			return err
		}
		if !exists {
			kapacitor.NumTasks.Add(1)
		}
		return nil
	})
	return err
}