Skip to content

Commit

Permalink
refactor: Update DiscardCloser to ReadDiscardCloser and add io.Reader…
Browse files Browse the repository at this point in the history
… support
  • Loading branch information
WangYihang committed Jun 28, 2024
1 parent f34ef7b commit 9db494c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
37 changes: 37 additions & 0 deletions examples/stdio/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"math/rand"
"time"

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

type MyTask struct{}

func New() *MyTask {
return &MyTask{}
}

func (t *MyTask) Do() error {
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
return nil
}

func main() {
scheduler := gojob.New(
gojob.WithNumWorkers(8),
gojob.WithMaxRetries(4),
gojob.WithMaxRuntimePerTaskSeconds(16),
gojob.WithNumShards(4),
gojob.WithShard(0),
gojob.WithResultFilePath("-"),
gojob.WithStatusFilePath("status.json"),
).
Start()
for range utils.Cat("-") {
scheduler.Submit(New())
}
scheduler.Wait()
}
9 changes: 5 additions & 4 deletions pkg/utils/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,21 @@ func Count[T any](in <-chan T) (count int64) {
return count
}

type DiscardCloser struct {
type ReadDiscardCloser struct {
io.Writer
io.Reader
}

func (wc DiscardCloser) Close() error {
func (wc ReadDiscardCloser) Close() error {
return nil
}

func OpenFile(path string) (io.WriteCloser, error) {
switch path {
case "-":
return DiscardCloser{Writer: os.Stdout}, nil
return ReadDiscardCloser{Writer: os.Stdout, Reader: os.Stdin}, nil
case "":
return DiscardCloser{Writer: io.Discard}, nil
return ReadDiscardCloser{Writer: io.Discard}, nil
default:
// Create folder
dir := filepath.Dir(path)
Expand Down

0 comments on commit 9db494c

Please sign in to comment.