Beispiel #1
0
// SaveJob stores an HTTP job on boltdb
func (c *BoltDB) SaveJob(j *job.Job) error {
	err := c.DB.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(jobsBucket))

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

		// Marshal to json our job
		buf, err := json.Marshal(j)
		if err != nil {
			return err
		}

		// save as always (insert or update doesn't matter)
		key := idToByte(j.ID)
		return b.Put(key, buf)
	})

	if err != nil {
		err = fmt.Errorf("error storing job '%d': %v", j.ID, err)
		logrus.Error(err.Error())
		return err
	}

	logrus.Debugf("Stored job '%d' boltdb", j.ID)
	return nil
}
Beispiel #2
0
// SaveJob stores a job on memory
func (c *Dummy) SaveJob(j *job.Job) error {
	c.jobsMutex.Lock()
	defer c.jobsMutex.Unlock()

	c.JobCounter++
	j.ID = c.JobCounter
	key := fmt.Sprintf(jobKeyFmt, j.ID)
	c.Jobs[key] = j

	// Never conflict (always creates a new id)
	return nil
}