Beispiel #1
0
// Recent returns up to n of the most recently completed jobs (including
// failed ones).
func (d *DB) Recent(n int) ([]*Job, error) {
	it := d.db.NewIterator(util.BytesPrefix([]byte(finishPrefix)), nil)
	defer it.Release()

	// the last iterated over jobs are the most recent
	ids := []JobId{}
	for it.Next() {
		var id JobId
		copy(id[:], it.Value())
		ids = append(ids, id)
	}
	if err := it.Error(); err != nil {
		return nil, err
	}

	if len(ids) > n {
		ids = ids[len(ids)-n:]
	}

	jobs := make([]*Job, len(ids))
	for i, id := range ids {
		j, err := d.Get(id)
		if err != nil {
			return nil, err
		}
		jobs[i] = j
	}

	return jobs, nil
}
Beispiel #2
0
// Current returns the all jobs from the database that aren't completed - e.g.
// queued or running.
func (d *DB) Current() ([]*Job, error) {
	it := d.db.NewIterator(util.BytesPrefix([]byte(currPrefix)), nil)
	defer it.Release()

	ids := []JobId{}
	for it.Next() {
		var id JobId
		copy(id[:], it.Value())
		ids = append(ids, id)
	}
	if err := it.Error(); err != nil {
		return nil, err
	}

	jobs := make([]*Job, len(ids))
	for i, id := range ids {
		j, err := d.Get(id)
		if err != nil {
			return nil, err
		}
		jobs[i] = j
	}

	return jobs, nil
}