Skip to content

Commit

Permalink
chore: remove args from job struct
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Oct 23, 2023
1 parent d900a53 commit 722f7da
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
28 changes: 22 additions & 6 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go.opentelemetry.io/otel/trace"
"gorm.io/gorm"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"

"github.com/flanksource/duty"
"github.com/flanksource/duty/models"
Expand Down Expand Up @@ -93,21 +94,36 @@ func (k Context) WithDB(db *gorm.DB, pool *pgxpool.Pool) Context {
}

func (k Context) DB() *gorm.DB {
return k.Value("db").(*gorm.DB)
v, ok := k.Value("db").(*gorm.DB)
if !ok {
return nil
}
return v
}

func (k Context) Pool() *pgxpool.Pool {
return k.Value("pgxpool").(*pgxpool.Pool)
v, ok := k.Value("pgxpool").(*pgxpool.Pool)
if !ok {
return nil
}
return v

}

// TODO: Handle it being nil/empty
func (k *Context) Kubernetes() kubernetes.Interface {
return k.Value("kubernetes").(kubernetes.Interface)
v, ok := k.Value("kubernetes").(kubernetes.Interface)
if !ok {
return fake.NewSimpleClientset()
}
return v
}

// TODO: Handle it being nil/empty
func (k *Context) Kommons() *kommons.Client {
return k.Value("kommons").(*kommons.Client)
v, ok := k.Value("kommons").(*kommons.Client)
if !ok {
return nil
}
return v
}

func (k Context) StartSpan(name string) (Context, trace.Span) {
Expand Down
18 changes: 7 additions & 11 deletions job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ type Job struct {
Schedule string
AllowConcurrent bool
Timeout time.Duration
Fn func(ctx JobRuntime, args ...any) error
Args []any
Fn func(ctx JobRuntime) error
RunNow bool
ID string
entryID *cron.EntryID
Expand All @@ -29,13 +28,12 @@ type JobRuntime struct {
Ended time.Time
}

func NewJob(ctx context.Context, name string, schedule string, fn func(ctx JobRuntime, args ...any) error, args ...any) *Job {
func NewJob(ctx context.Context, name string, schedule string, fn func(ctx JobRuntime) error) *Job {
return &Job{
Context: ctx,
Name: name,
Schedule: schedule,
Fn: fn,
Args: args,
}
}

Expand Down Expand Up @@ -70,7 +68,7 @@ func (j Job) Run() {
}

defer span.End()
if err := j.Fn(r, j.Args...); err != nil {
if err := j.Fn(r); err != nil {
ctx.Error(err)
}
}
Expand All @@ -96,11 +94,9 @@ func (j Job) GetEntry(cronRunner *cron.Cron) *cron.Entry {
return &entry
}

func (j Job) RemoveFromScheduler(cronRunner *cron.Cron) *cron.Entry {
for _, entry := range cronRunner.Entries() {
if entry.Job.(Job).Name == j.Name {
return &entry
}
func (j Job) RemoveFromScheduler(cronRunner *cron.Cron) {
if j.entryID == nil {
return
}
return nil
cronRunner.Remove(*j.entryID)
}

0 comments on commit 722f7da

Please sign in to comment.