Skip to content

Commit

Permalink
first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
thesilentg committed Jul 19, 2024
1 parent 1e4f04a commit e9b462f
Show file tree
Hide file tree
Showing 3 changed files with 309 additions and 316 deletions.
86 changes: 86 additions & 0 deletions internal/examples/simplejob/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"bytes"
"context"
"encoding/json"
"io"
"log"
"log/slog"
"math/rand"
"os"
"time"

"github.com/gaffo/jorb"
)

type oc struct{}
type ac struct{}
type jc struct{}

func main() {
o := oc{}
a := ac{}
r := jorb.NewRun[oc, jc]("example", o)

slog.SetLogLoggerLevel(slog.LevelWarn)
for i := 0; i < 100; i++ {
r.AddJobWithState(jc{}, "A")
}

states := []jorb.State[ac, oc, jc]{
{
TriggerState: "A",
Exec: func(ctx context.Context, ac ac, oc oc, jc jc) (jc, string, []jorb.KickRequest[jc], error) {
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
return jc, "B", nil, nil
},
Concurrency: 5,
},
{
TriggerState: "B",
Exec: func(ctx context.Context, ac ac, oc oc, jc jc) (jc, string, []jorb.KickRequest[jc], error) {
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
return jc, "C", nil, nil
},
Concurrency: 4,
},
{
TriggerState: "C",
Exec: func(ctx context.Context, ac ac, oc oc, jc jc) (jc, string, []jorb.KickRequest[jc], error) {
time.Sleep(time.Duration(rand.Intn(1000)) * time.Millisecond)
return jc, "D", nil, nil
},
Concurrency: 3,
},
{
TriggerState: "D",
Terminal: true,
},
}

serial := jorb.NewJsonSerializer[oc, jc]("example.state")
listener := &fileListener{fileName: "example.status"}
p := jorb.NewProcessor[ac, oc, jc](a, states, serial, listener)
if err := p.Exec(context.Background(), r); err != nil {
log.Fatal(err)
}
}

// Serializes the status updates to a file
type fileListener struct {
fileName string
}

func (f *fileListener) StatusUpdate(status []jorb.StatusCount) {
buf := &bytes.Buffer{}

encoder := json.NewEncoder(buf)
encoder.SetIndent("", " ")
_ = encoder.Encode(status)

file, _ := os.Create(f.fileName)
defer file.Close()

_, _ = io.Copy(file, buf)
}
Loading

0 comments on commit e9b462f

Please sign in to comment.