chore: aria2 task wait for transfer

refactor/fs
Noah Hsu 2022-06-29 18:12:31 +08:00
parent d24e51bc86
commit c88680b495
1 changed files with 18 additions and 4 deletions

View File

@ -22,6 +22,7 @@ type Monitor struct {
retried int retried int
c chan int c chan int
dstDirPath string dstDirPath string
finish chan struct{}
} }
func (m *Monitor) Loop() error { func (m *Monitor) Loop() error {
@ -31,24 +32,36 @@ func (m *Monitor) Loop() error {
//_ = os.RemoveAll(m.tempDir) //_ = os.RemoveAll(m.tempDir)
}() }()
m.c = make(chan int) m.c = make(chan int)
m.finish = make(chan struct{})
notify.Signals.Store(m.tsk.ID, m.c) notify.Signals.Store(m.tsk.ID, m.c)
var (
err error
ok bool
)
outer:
for { for {
select { select {
case <-m.tsk.Ctx.Done(): case <-m.tsk.Ctx.Done():
_, err := client.Remove(m.tsk.ID) _, err := client.Remove(m.tsk.ID)
return err return err
case <-m.c: case <-m.c:
ok, err := m.Update() ok, err = m.Update()
if ok { if ok {
return err break outer
} }
case <-time.After(time.Second * 5): case <-time.After(time.Second * 5):
ok, err := m.Update() ok, err = m.Update()
if ok { if ok {
return err break outer
} }
} }
} }
if err != nil {
return err
}
m.tsk.SetStatus("aria2 download completed, waiting for transfer")
<-m.finish
return nil
} }
func (m *Monitor) Update() (bool, error) { func (m *Monitor) Update() (bool, error) {
@ -113,6 +126,7 @@ func (m *Monitor) Complete() error {
go func() { go func() {
wg.Wait() wg.Wait()
err := os.RemoveAll(m.tempDir) err := os.RemoveAll(m.tempDir)
m.finish <- struct{}{}
if err != nil { if err != nil {
log.Errorf("failed to remove aria2 temp dir: %+v", err.Error()) log.Errorf("failed to remove aria2 temp dir: %+v", err.Error())
} }