func (q *queue) markJobDone(id uint64, status jobStatus) error {
	if status != jobComplete && status != jobFailed {
		return errors.New("Can only move to Complete or Failed Status")
	}
	key := []byte(getJobKey(jobInProgress, id))
	q.dbL.Lock()
	defer q.dbL.Unlock()
	err := q.db.Update(func(txn *badger.Txn) error {
		b, err := getBytesForKey(txn, key)
		if err != nil {
			return err
		}
		err = moveItem(txn, key, []byte(getJobKey(status, id)), b)
		return err
	})
	return err
}
markJobDone moves a job from the inprogress status to complete/failed
package blero
import (
	"bytes"
	"encoding/gob"
	"errors"
	"fmt"
	"os"
	"sync"
	"github.com/dgraph-io/badger"
)
type queueOpts struct {
	DBPath string
	Logger badger.Logger
}
type queue struct {
	opts queueOpts
	db   *badger.DB
	seq  *badger.Sequence
	dbL  sync.Mutex
}
func newQueue(opts queueOpts) *queue {
	q := &queue{opts: opts}
	return q
}
type badgerLogger struct{}
func (l *badgerLogger) Infof(format string, a ...interface{}) {
}
func (l *badgerLogger) Errorf(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}
func (l *badgerLogger) Warningf(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}
func (l *badgerLogger) Debugf(format string, a ...interface{}) {
}
func (q *queue) start() error {
	if q.opts.DBPath == "" {
		return errors.New("DBPath is required")
	}
	x := badger.Options{}
	x.Dir = "ds"
	badgerOpts := badger.DefaultOptions(q.opts.DBPath)
	badgerOpts.Logger = &badgerLogger{}
	badgerOpts.SyncWrites = true
	db, err := badger.Open(badgerOpts)
	if err != nil {
		return err
	}
	q.db = db
	q.seq, err = db.GetSequence([]byte("standard"), 1000)
	return err
}
func (q *queue) stop() error {
	err := q.seq.Release()
	if err != nil {
		return err
	}
	err = q.db.Close()
	if err != nil {
		return err
	}
	return nil
}
func getNextSeq(seq *badger.Sequence) (num uint64, err error) {
	defer func() {
		r := recover()
		if r != nil {
			err = r.(error)
		}
	}()
	num, err = seq.Next()
	return num, err
}
func (q *queue) enqueueJob(name string, data []byte) (uint64, error) {
	num, err := getNextSeq(q.seq)
	if err != nil {
		return 0, err
	}
	j := &Job{ID: num + 1, Name: name, Data: data}
	jKey := getJobKey(jobPending, j.ID)
	err = q.db.Update(func(txn *badger.Txn) error {
		b, err := encodeJob(j)
		if err != nil {
			return err
		}
		err = txn.Set([]byte(jKey), b)
		return err
	})
	if err != nil {
		return 0, err
	}
	return j.ID, nil
}
func encodeJob(j *Job) ([]byte, error) {
	var b bytes.Buffer
	err := gob.NewEncoder(&b).Encode(j)
	return b.Bytes(), err
}
type jobStatus uint8
const (
	jobPending jobStatus = iota
	jobInProgress
	jobComplete
	jobFailed
)
func getQueueKeyPrefix(status jobStatus) string {
	return fmt.Sprintf("q:%v:", status)
}
func getJobKey(status jobStatus, jID uint64) string {
	return getQueueKeyPrefix(status) + jIDString(jID)
}
func jIDString(jID uint64) string {
	return fmt.Sprintf("%020d", jID)
}
func (q *queue) dequeueJob() (*Job, error) {
	var j *Job
	q.dbL.Lock()
	defer q.dbL.Unlock()
	err := q.db.Update(func(txn *badger.Txn) error {
		prefix := []byte(getQueueKeyPrefix(jobPending))
		k, v, err := getFirstKVForPrefix(txn, prefix)
		if err != nil {
			return err
		}
		if k == nil {
			return nil
		}
		j, err = decodeJob(v)
		if err != nil {
			return err
		}
		err = moveItem(txn, k, []byte(getJobKey(jobInProgress, j.ID)), v)
		return err
	})
	return j, err
}
func getFirstKVForPrefix(txn *badger.Txn, prefix []byte) ([]byte, []byte, error) {
	itOpts := badger.DefaultIteratorOptions
	itOpts.PrefetchValues = true
	itOpts.PrefetchSize = 1
	it := txn.NewIterator(itOpts)
	it.Seek(prefix)
	defer it.Close()
	if !it.ValidForPrefix(prefix) {
		return nil, nil, nil
	}
	item := it.Item()
	k := item.KeyCopy(nil)
	v, err := item.ValueCopy(nil)
	return k, v, err
}