better jobs queue
This commit is contained in:
parent
e0314dcf56
commit
cca34832b7
2 changed files with 23 additions and 15 deletions
|
|
@ -5,20 +5,25 @@ package server
|
|||
import "context"
|
||||
|
||||
type ScanQueue struct {
|
||||
ctx context.Context
|
||||
srv *HTTPServer
|
||||
pchan chan string
|
||||
ctx context.Context
|
||||
srv *HTTPServer
|
||||
jobs chan job
|
||||
}
|
||||
|
||||
type job struct {
|
||||
path string
|
||||
callback func()
|
||||
}
|
||||
|
||||
func NewScanQueue(ctx context.Context, srv *HTTPServer, buffer int) *ScanQueue {
|
||||
return &ScanQueue{
|
||||
ctx: ctx,
|
||||
srv: srv,
|
||||
pchan: make(chan string, buffer),
|
||||
ctx: ctx,
|
||||
srv: srv,
|
||||
jobs: make(chan job, buffer),
|
||||
}
|
||||
}
|
||||
|
||||
func (sq *ScanQueue) Add(path string) (err error) {
|
||||
func (sq *ScanQueue) Add(path string, callback func()) (err error) {
|
||||
err = sq.srv.FS.AddScan(path)
|
||||
if err != nil {
|
||||
return
|
||||
|
|
@ -27,7 +32,7 @@ func (sq *ScanQueue) Add(path string) (err error) {
|
|||
select {
|
||||
case <-sq.ctx.Done():
|
||||
return
|
||||
case sq.pchan <- path:
|
||||
case sq.jobs <- job{path, callback}:
|
||||
}
|
||||
}()
|
||||
return
|
||||
|
|
@ -37,16 +42,20 @@ func (sq *ScanQueue) Scan() {
|
|||
for {
|
||||
select {
|
||||
case <-sq.ctx.Done():
|
||||
//close(sq.pchan)
|
||||
//close(sq.jobs)
|
||||
return
|
||||
case p := <-sq.pchan:
|
||||
sq.scanFile(p)
|
||||
case j := <-sq.jobs:
|
||||
sq.scanFile(j)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (sq *ScanQueue) scanFile(path string) {
|
||||
func (sq *ScanQueue) scanFile(j job) {
|
||||
srv := sq.srv
|
||||
path := j.path
|
||||
if j.callback != nil {
|
||||
defer j.callback()
|
||||
}
|
||||
defer srv.FS.RemoveScan(path)
|
||||
log := srv.Log.WithValues("file", path)
|
||||
file, txt, err := srv.Scanner.Scan(path)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue