diff --git a/examples/stdio/main.go b/examples/stdio/main.go new file mode 100644 index 0000000..a91f5d4 --- /dev/null +++ b/examples/stdio/main.go @@ -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() +} diff --git a/pkg/utils/io.go b/pkg/utils/io.go index 3b9c9e4..9484054 100644 --- a/pkg/utils/io.go +++ b/pkg/utils/io.go @@ -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)