From 59ec05c3e9f943ecfa48bb657d5407d2cd4ff5bd Mon Sep 17 00:00:00 2001 From: Erik Unger Date: Wed, 25 Sep 2024 11:50:56 +0200 Subject: [PATCH] pass IDs as args instead of internal gen --- job.go | 12 ++++++------ jobbundle.go | 9 +++++---- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/job.go b/job.go index 3dae58e..90071c8 100644 --- a/job.go +++ b/job.go @@ -75,7 +75,7 @@ func (j *Job) String() string { // NewJobWithPriority creates a Job but does not add it to the queue. // The passed payload will be marshalled to JSON or directly interpreted as JSON if possible. // If startAt is not null then the job will not start before that time. -func NewJobWithPriority(jobType, origin string, payload any, priority int64, startAt nullable.Time) (*Job, error) { +func NewJobWithPriority(id uu.ID, jobType, origin string, payload any, priority int64, startAt nullable.Time) (*Job, error) { if jobType == "" { return nil, errors.New("empty jobType") } @@ -133,7 +133,7 @@ func NewJobWithPriority(jobType, origin string, payload any, priority int64, sta now := time.Now() job := &Job{ - ID: uu.IDv4(), + ID: id, Type: jobType, Payload: payloadJSON, Origin: origin, @@ -149,8 +149,8 @@ func NewJobWithPriority(jobType, origin string, payload any, priority int64, sta // NewJob creates a Job but does not add it to the queue. // The passed payload will be marshalled to JSON or directly interpreted as JSON if possible. // If startAt is not null then the job will not start before that time. -func NewJob(jobType, origin string, payload any, startAt nullable.Time) (*Job, error) { - return NewJobWithPriority(jobType, origin, payload, 0, startAt) +func NewJob(id uu.ID, jobType, origin string, payload any, startAt nullable.Time) (*Job, error) { + return NewJobWithPriority(id, jobType, origin, payload, 0, startAt) } // NewJobReflectType creates a Job but does not add it to the queue. @@ -162,6 +162,6 @@ func NewJob(jobType, origin string, payload any, startAt nullable.Time) (*Job, e // The job type string starts with the package import path of the type // followed by a point and the type name. // Pointer types will be dereferenced. -func NewJobReflectType(origin string, payload any, startAt nullable.Time) (*Job, error) { - return NewJob(ReflectJobTypeOfPayload(payload), origin, payload, startAt) +func NewJobReflectType(id uu.ID, origin string, payload any, startAt nullable.Time) (*Job, error) { + return NewJob(id, ReflectJobTypeOfPayload(payload), origin, payload, startAt) } diff --git a/jobbundle.go b/jobbundle.go index ce5ac30..438594d 100644 --- a/jobbundle.go +++ b/jobbundle.go @@ -1,6 +1,7 @@ package jobqueue import ( + "context" "errors" "fmt" "time" @@ -52,7 +53,7 @@ func (b *JobBundle) String() string { // A job will be added for every JobDesc. // If a JobDesc.Type is an empty string, then ReflectJobType(JobDesc.Payload) will be used instead. // If startAt is not null then the job bundle will not start before that time. -func NewJobBundle(jobBundleType, jobBundleOrigin string, jobDescriptions []JobDesc, startAt nullable.Time) (*JobBundle, error) { +func NewJobBundle(ctx context.Context, jobBundleType, jobBundleOrigin string, jobDescriptions []JobDesc, startAt nullable.Time) (*JobBundle, error) { if len(jobDescriptions) == 0 { return nil, errors.New("no jobDescriptions") } @@ -64,7 +65,7 @@ func NewJobBundle(jobBundleType, jobBundleOrigin string, jobDescriptions []JobDe if jobType == "" { jobType = ReflectJobTypeOfPayload(desc.Payload) } - job, err := NewJobWithPriority(jobType, desc.Origin, desc.Payload, desc.Priority, startAt) + job, err := NewJobWithPriority(uu.NewID(ctx), jobType, desc.Origin, desc.Payload, desc.Priority, startAt) if err != nil { return nil, err } @@ -72,7 +73,7 @@ func NewJobBundle(jobBundleType, jobBundleOrigin string, jobDescriptions []JobDe } jobBundle := &JobBundle{ - ID: uu.IDv4(), + ID: uu.NewID(ctx), Type: jobBundleType, Origin: jobBundleOrigin, Jobs: jobs, @@ -116,7 +117,7 @@ func NewJobBundle(jobBundleType, jobBundleOrigin string, jobDescriptions []JobDe // } // jobBundle := &JobBundle{ -// ID: uu.IDv4(), +// ID: uu.NewID(ctx), // Type: jobBundleType, // Origin: jobBundleOrigin, // Jobs: jobs,