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

update examples with context #11

Closed
wants to merge 2 commits 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
3 changes: 2 additions & 1 deletion examples/complex-http-crawler/pkg/model/task.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"context"
"net/http"
"time"
)
Expand All @@ -18,7 +19,7 @@ func New(url string) *MyTask {
}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
transport := &http.Transport{
DisableCompression: true,
}
Expand Down
3 changes: 2 additions & 1 deletion examples/metadata/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"math/rand"
"time"

Expand All @@ -15,7 +16,7 @@ func New() *MyTask {
return &MyTask{}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion examples/nopper/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"math/rand"
"time"

Expand All @@ -14,7 +15,7 @@ func New() *MyTask {
return &MyTask{}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion examples/prometheus/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"net/http"

Expand All @@ -18,7 +19,7 @@ func New(url string) *MyTask {
}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
response, err := http.Get(t.Url)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion examples/random-error/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"errors"
"math/rand"
"time"
Expand All @@ -22,7 +23,7 @@ func New(index int, sleepSeconds int) *MyTask {
}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
time.Sleep(time.Duration(t.SleepSeconds) * time.Second)
if rand.Float64() < t.ErrorProbability {
return errors.New("an error occurred")
Expand Down
3 changes: 2 additions & 1 deletion examples/result-channel/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/json"
"fmt"
"log/slog"
Expand All @@ -20,7 +21,7 @@ func New(url string) *MyTask {
}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
response, err := http.Get(t.Url)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion examples/simple-http-crawler/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"net/http"

Expand All @@ -18,7 +19,7 @@ func New(url string) *MyTask {
}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
response, err := http.Get(t.Url)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion examples/sleeper/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"math/rand"
"time"

Expand All @@ -19,7 +20,7 @@ func New(index int, sleepSeconds int) *MyTask {
}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
time.Sleep(time.Duration(t.SleepSeconds) * time.Second)
return nil
}
Expand Down
7 changes: 5 additions & 2 deletions examples/tcp-port-scanner/task.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "net"
import (
"context"
"net"
)

type MyTask struct {
IP string `json:"ip"`
Expand All @@ -16,7 +19,7 @@ func New(ip string, port uint16) *MyTask {
}
}

func (t *MyTask) Do() error {
func (t *MyTask) Do(_ context.Context) error {
conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{
IP: net.ParseIP(t.IP),
Port: int(t.Port),
Expand Down
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