Skip to content

Commit

Permalink
example: add resultChan()
Browse files Browse the repository at this point in the history
  • Loading branch information
WangYihang committed May 7, 2024
1 parent bd249ba commit c30d1f7
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions examples/result-channel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"encoding/json"
"fmt"
"log/slog"
"net/http"

"github.com/WangYihang/gojob"
)

type MyTask struct {
Url string `json:"url"`
StatusCode int `json:"status_code"`
}

func New(url string) *MyTask {
return &MyTask{
Url: url,
}
}

func (t *MyTask) Do() error {
response, err := http.Get(t.Url)
if err != nil {
return err
}
t.StatusCode = response.StatusCode
defer response.Body.Close()
return nil
}

func main() {
var numTotalTasks int64 = 256
scheduler := gojob.New(
gojob.WithNumWorkers(8),
gojob.WithMaxRetries(4),
gojob.WithMaxRuntimePerTaskSeconds(16),
gojob.WithNumShards(4),
gojob.WithShard(0),
gojob.WithTotalTasks(numTotalTasks),
gojob.WithStatusFilePath("status.json"),
gojob.WithResultFilePath("result.json"),
gojob.WithMetadataFilePath("metadata.json"),
)
go func() {
for result := range scheduler.ResultChan() {
data, err := json.Marshal(result)
if err != nil {
slog.Error("failed to marshal result", slog.String("error", err.Error()))
continue
}
fmt.Println(string(data))
}
}()
for i := range numTotalTasks {
scheduler.Submit(New(fmt.Sprintf("https://httpbin.org/task/%d", i)))
}
scheduler.Wait()
}

0 comments on commit c30d1f7

Please sign in to comment.