Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove args from job struct #312

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading