2018-10-20 12:35:24 +02:00
|
|
|
package pruner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type execQueue struct {
|
2019-03-22 19:41:12 +01:00
|
|
|
mtx sync.Mutex
|
2018-10-20 12:35:24 +02:00
|
|
|
pending, completed []*fs
|
|
|
|
}
|
|
|
|
|
|
|
|
func newExecQueue(cap int) *execQueue {
|
|
|
|
q := execQueue{
|
2019-03-22 19:41:12 +01:00
|
|
|
pending: make([]*fs, 0, cap),
|
2018-10-20 12:35:24 +02:00
|
|
|
completed: make([]*fs, 0, cap),
|
|
|
|
}
|
|
|
|
return &q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *execQueue) Report() (pending, completed []FSReport) {
|
|
|
|
q.mtx.Lock()
|
|
|
|
defer q.mtx.Unlock()
|
|
|
|
|
|
|
|
pending = make([]FSReport, len(q.pending))
|
|
|
|
for i, fs := range q.pending {
|
|
|
|
pending[i] = fs.Report()
|
|
|
|
}
|
|
|
|
completed = make([]FSReport, len(q.completed))
|
|
|
|
for i, fs := range q.completed {
|
|
|
|
completed[i] = fs.Report()
|
|
|
|
}
|
|
|
|
|
|
|
|
return pending, completed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *execQueue) HasCompletedFSWithErrors() bool {
|
|
|
|
q.mtx.Lock()
|
|
|
|
defer q.mtx.Unlock()
|
|
|
|
for _, fs := range q.completed {
|
|
|
|
if fs.execErrLast != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *execQueue) Pop() *fs {
|
|
|
|
if len(q.pending) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fs := q.pending[0]
|
|
|
|
q.pending = q.pending[1:]
|
|
|
|
return fs
|
|
|
|
}
|
|
|
|
|
2019-03-22 19:41:12 +01:00
|
|
|
func (q *execQueue) Put(fs *fs, err error, done bool) {
|
2018-10-20 12:35:24 +02:00
|
|
|
fs.mtx.Lock()
|
|
|
|
fs.execErrLast = err
|
2019-03-13 20:50:03 +01:00
|
|
|
if done || err != nil {
|
2018-10-20 12:35:24 +02:00
|
|
|
fs.mtx.Unlock()
|
|
|
|
q.mtx.Lock()
|
|
|
|
q.completed = append(q.completed, fs)
|
|
|
|
q.mtx.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fs.mtx.Unlock()
|
|
|
|
|
|
|
|
q.mtx.Lock()
|
|
|
|
// inefficient priority q
|
|
|
|
q.pending = append(q.pending, fs)
|
|
|
|
sort.SliceStable(q.pending, func(i, j int) bool {
|
|
|
|
q.pending[i].mtx.Lock()
|
|
|
|
defer q.pending[i].mtx.Unlock()
|
|
|
|
q.pending[j].mtx.Lock()
|
|
|
|
defer q.pending[j].mtx.Unlock()
|
|
|
|
return strings.Compare(q.pending[i].path, q.pending[j].path) == -1
|
|
|
|
})
|
|
|
|
q.mtx.Unlock()
|
|
|
|
|
2019-03-22 19:41:12 +01:00
|
|
|
}
|