Skip to content

Commit

Permalink
fix: add run id
Browse files Browse the repository at this point in the history
  • Loading branch information
WangYihang committed Feb 29, 2024
1 parent f51821f commit ef9d95d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module github.com/WangYihang/gojob

go 1.21

require github.com/jessevdk/go-flags v1.5.0
require (
github.com/google/uuid v1.6.0
github.com/jessevdk/go-flags v1.5.0
)

require golang.org/x/sys v0.17.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
18 changes: 11 additions & 7 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"time"

"github.com/WangYihang/gojob/pkg/util"
"github.com/google/uuid"
)

// Scheduler is a task scheduler
type Scheduler struct {
ID string
NumWorkers int
OutputFilePath string
OutputFd io.WriteCloser
Expand All @@ -41,7 +43,9 @@ type Scheduler struct {

// NewScheduler creates a new scheduler
func NewScheduler() *Scheduler {
scheduler := &Scheduler{
id := uuid.New().String()
return (&Scheduler{
ID: id,
NumWorkers: 1,
Metadata: make(map[string]interface{}),
MaxRetries: 4,
Expand All @@ -58,11 +62,11 @@ func NewScheduler() *Scheduler {
taskWg: &sync.WaitGroup{},
logWg: &sync.WaitGroup{},
statusWg: &sync.WaitGroup{},
}
scheduler.SetOutputFilePath("-")
scheduler.SetStatusFilePath("-")
scheduler.SetMetadataFilePath("-")
return scheduler
}).
SetOutputFilePath("-").
SetStatusFilePath("-").
SetMetadataFilePath("-").
SetMetadata("id", id)
}

// SetNumShards sets the number of shards, default is 1 which means no sharding
Expand Down Expand Up @@ -182,7 +186,7 @@ func (s *Scheduler) Submit(task Task) {
index := s.CurrentIndex.Load()
if (index % s.NumShards) == s.Shard {
s.taskWg.Add(1)
s.TaskChan <- NewBasicTask(index, task)
s.TaskChan <- NewBasicTask(index, s.ID, task)
}
s.CurrentIndex.Add(1)
}
Expand Down
4 changes: 3 additions & 1 deletion task.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ type Task interface {

type BasicTask struct {
Index int64 `json:"index"`
ID string `json:"id"`
StartedAt int64 `json:"started_at"`
FinishedAt int64 `json:"finished_at"`
NumTries int `json:"num_tries"`
Task Task `json:"task"`
Error string `json:"error"`
}

func NewBasicTask(index int64, task Task) *BasicTask {
func NewBasicTask(index int64, id string, task Task) *BasicTask {
return &BasicTask{
Index: index,
ID: id,
StartedAt: 0,
FinishedAt: 0,
NumTries: 0,
Expand Down

0 comments on commit ef9d95d

Please sign in to comment.