Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

primary update of the Task interface with context #10

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/utils/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"
)

func RunWithTimeout(f func() error, timeout time.Duration) error {
func RunWithTimeout(f func(ctx context.Context) error, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

Expand All @@ -14,7 +14,7 @@ func RunWithTimeout(f func() error, timeout time.Duration) error {
go func() {
defer close(done)

done <- f()
done <- f(ctx)
}()

select {
Expand Down
3 changes: 2 additions & 1 deletion pkg/utils/timeout_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package utils_test

import (
"context"
"testing"
"time"

"github.com/WangYihang/gojob/pkg/utils"
)

func TestRunWithTimeout(t *testing.T) {
task := func() error {
task := func(_ context.Context) error {
time.Sleep(2 * time.Second)
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion scheduler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gojob_test

import (
"context"
"fmt"
"reflect"
"sort"
Expand Down Expand Up @@ -46,7 +47,7 @@ func newTask(i int, writer *safeWriter) *schedulerTestTask {
}
}

func (t *schedulerTestTask) Do() error {
func (t *schedulerTestTask) Do(_ context.Context) error {
t.writer.WriteString(fmt.Sprintf("%d\n", t.I))
return nil
}
Expand Down
4 changes: 3 additions & 1 deletion task.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gojob

import (
"context"

"github.com/google/uuid"
)

Expand All @@ -9,7 +11,7 @@ type Task interface {
// Do starts the task, returns error if failed
// If an error is returned, the task will be retried until MaxRetries
// You can set MaxRetries by calling SetMaxRetries on the scheduler
Do() error
Do(context.Context) error
}

type basicTask struct {
Expand Down
Loading