mirror of
https://github.com/wyx2685/V2bX.git
synced 2026-02-04 04:30:08 +00:00
fix hy tls bug
fix task
This commit is contained in:
@@ -24,20 +24,24 @@ func (t *Task) hasClosed() bool {
|
||||
return !t.running
|
||||
}
|
||||
|
||||
func (t *Task) checkedExecute() error {
|
||||
func (t *Task) checkedExecute(first bool) error {
|
||||
if t.hasClosed() {
|
||||
return nil
|
||||
}
|
||||
|
||||
t.access.Lock()
|
||||
defer t.access.Unlock()
|
||||
|
||||
if first {
|
||||
if err := t.Execute(); err != nil {
|
||||
t.running = false
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !t.running {
|
||||
return nil
|
||||
}
|
||||
|
||||
t.timer = time.AfterFunc(t.Interval, func() {
|
||||
t.checkedExecute()
|
||||
t.checkedExecute(true)
|
||||
})
|
||||
|
||||
return nil
|
||||
@@ -52,21 +56,12 @@ func (t *Task) Start(first bool) error {
|
||||
}
|
||||
t.running = true
|
||||
t.access.Unlock()
|
||||
if first {
|
||||
if err := t.Execute(); err != nil {
|
||||
t.access.Lock()
|
||||
t.running = false
|
||||
t.access.Unlock()
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := t.checkedExecute(); err != nil {
|
||||
if err := t.checkedExecute(first); err != nil {
|
||||
t.access.Lock()
|
||||
t.running = false
|
||||
t.access.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
15
common/task/task_test.go
Normal file
15
common/task/task_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestTask(t *testing.T) {
|
||||
ts := Task{Execute: func() error {
|
||||
log.Println("q")
|
||||
return nil
|
||||
}, Interval: time.Second}
|
||||
ts.Start(false)
|
||||
}
|
||||
Reference in New Issue
Block a user