-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
8 changed files
with
156 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package heartbeat | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/taylorchu/work" | ||
) | ||
|
||
// HeartbeaterOptions defines Heartbeater periodically refreshes InvisibleSec for long-running jobs. | ||
type HeartbeaterOptions struct { | ||
// Queue is where the current job is currently in. | ||
Queue work.Queue | ||
// After the job is dequeued, no other dequeuer can see this job for a while. | ||
// InvisibleSec controls how long this period is. | ||
InvisibleSec int64 | ||
// IntervalSec controls how often job InvisibleSec is refreshed. | ||
IntervalSec int64 | ||
} | ||
|
||
// Heartbeater refreshes InvisibleSec for long-running jobs periodically. | ||
// As a result, if the job is lost, it won't take a long time before it is retried. | ||
func Heartbeater(hopts *HeartbeaterOptions) work.HandleMiddleware { | ||
return func(f work.HandleFunc) work.HandleFunc { | ||
return func(job *work.Job, opt *work.DequeueOptions) error { | ||
refresh := func() error { | ||
now := time.Now() | ||
copiedJob := *job | ||
copiedJob.UpdatedAt = now | ||
copiedJob.EnqueuedAt = now.Add(time.Duration(hopts.InvisibleSec) * time.Second) | ||
return hopts.Queue.Enqueue(&copiedJob, &work.EnqueueOptions{ | ||
Namespace: opt.Namespace, | ||
QueueID: opt.QueueID, | ||
}) | ||
} | ||
|
||
done := make(chan struct{}) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
go func() { | ||
defer close(done) | ||
|
||
ticker := time.NewTicker(time.Duration(hopts.IntervalSec) * time.Second) | ||
defer ticker.Stop() | ||
|
||
refresh() | ||
for { | ||
select { | ||
case <-ticker.C: | ||
refresh() | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
}() | ||
|
||
err := f(job, opt) | ||
cancel() | ||
<-done | ||
return err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package heartbeat | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-redis/redis/v8" | ||
"github.com/stretchr/testify/require" | ||
"github.com/taylorchu/work" | ||
"github.com/taylorchu/work/redistest" | ||
) | ||
|
||
func TestHeartbeater(t *testing.T) { | ||
client := redistest.NewClient() | ||
defer client.Close() | ||
require.NoError(t, redistest.Reset(client)) | ||
|
||
job := work.NewJob() | ||
opt := &work.DequeueOptions{ | ||
Namespace: "{ns1}", | ||
QueueID: "q1", | ||
At: time.Now(), | ||
InvisibleSec: 60, | ||
} | ||
|
||
hb := Heartbeater(&HeartbeaterOptions{ | ||
Queue: work.NewRedisQueue(client), | ||
InvisibleSec: 30, | ||
IntervalSec: 1, | ||
}) | ||
|
||
h := hb(func(*work.Job, *work.DequeueOptions) error { | ||
return nil | ||
}) | ||
|
||
err := h(job, opt) | ||
require.NoError(t, err) | ||
require.Equal(t, job.CreatedAt, job.UpdatedAt) | ||
require.Equal(t, job.CreatedAt, job.EnqueuedAt) | ||
|
||
z, err := client.ZRangeByScoreWithScores( | ||
context.Background(), | ||
"{ns1}:queue:q1", | ||
&redis.ZRangeBy{ | ||
Min: "-inf", | ||
Max: "+inf", | ||
}).Result() | ||
require.NoError(t, err) | ||
require.Len(t, z, 1) | ||
require.EqualValues(t, job.EnqueuedAt.Unix()+30, z[0].Score) | ||
|
||
err = h(job, opt) | ||
require.NoError(t, err) | ||
require.Equal(t, job.CreatedAt, job.UpdatedAt) | ||
require.Equal(t, job.CreatedAt, job.EnqueuedAt) | ||
|
||
z, err = client.ZRangeByScoreWithScores( | ||
context.Background(), | ||
"{ns1}:queue:q1", | ||
&redis.ZRangeBy{ | ||
Min: "-inf", | ||
Max: "+inf", | ||
}).Result() | ||
require.NoError(t, err) | ||
require.Len(t, z, 1) | ||
require.EqualValues(t, job.EnqueuedAt.Unix()+30, z[0].Score) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters