コード例 #1
0
ファイル: dummy.go プロジェクト: slok/khronos
// SaveResult saves a result on a job in memory
func (c *Dummy) SaveResult(r *job.Result) error {
	c.resultsMutex.Lock()
	defer c.resultsMutex.Unlock()

	resultsKey := fmt.Sprintf(jobResultsKeyFmt, r.Job.ID)
	c.ResultsCounter[resultsKey]++
	r.ID = c.ResultsCounter[resultsKey]
	results, ok := c.Results[resultsKey]
	if !ok {
		results = map[string]*job.Result{}
		c.Results[resultsKey] = results
	}
	results[fmt.Sprintf(resultKeyFmt, r.ID)] = r

	return nil
}
コード例 #2
0
ファイル: boltdb.go プロジェクト: slok/khronos
// SaveResult stores a result of a job on boltdb
func (c *BoltDB) SaveResult(r *job.Result) error {
	// First check if the job is present, if not, then error
	if _, err := c.GetJob(r.Job.ID); err != nil {
		return err
	}

	err := c.DB.Update(func(tx *bolt.Tx) error {
		resB := tx.Bucket([]byte(resultsBucket))

		// Create job results bucket if doens't exist
		jobresKey := fmt.Sprintf(jobResultsBuckets, string(idToByte(r.Job.ID)))
		b, err := resB.CreateBucketIfNotExists([]byte(jobresKey))
		if err != nil {
			return fmt.Errorf("error creating bucket: %s", err)
		}

		// Create a new ID for the new result, not new ID if it has already (update)
		// Starts in 1, so its safe to check with 0
		if r.ID == 0 {
			id, _ := b.NextSequence()
			r.ID = int(id)
		}

		// Marshal the result
		buf, err := json.Marshal(r)
		if err != nil {
			return err
		}

		// Save the result
		return b.Put(idToByte(r.ID), buf)
	})
	if err != nil {
		err = fmt.Errorf("error storing result '%d': %v", r.ID, err)
		logrus.Error(err.Error())
		return err
	}
	logrus.Debugf("Stored result '%d' boltdb", r.ID)
	return nil
}