Skip to content

Commit

Permalink
scheduler/state: avoid passing JobStore via ctx
Browse files Browse the repository at this point in the history
This (hack) is no longer necessary since all indexing logic is now self-contained within one package.
  • Loading branch information
radeksimko committed Jul 5, 2022
1 parent 0b351b2 commit 8ef3b35
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 27 deletions.
15 changes: 0 additions & 15 deletions internal/job/job_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,9 @@ package job

import (
"context"
"fmt"
)

type JobStore interface {
EnqueueJob(newJob Job) (ID, error)
WaitForJobs(ctx context.Context, ids ...ID) error
}

type jobStoreCtxKey struct{}

func WithJobStore(ctx context.Context, js JobStore) context.Context {
return context.WithValue(ctx, jobStoreCtxKey{}, js)
}

func JobStoreFromContext(ctx context.Context) (JobStore, error) {
js, ok := ctx.Value(jobStoreCtxKey{}).(JobStore)
if !ok {
return nil, fmt.Errorf("not found JobStore in context")
}
return js, nil
}
3 changes: 1 addition & 2 deletions internal/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func (s *Scheduler) eval(ctx context.Context) {

deferredJobIds := make(job.IDs, 0)
if nextJob.Defer != nil {
deferCtx := job.WithJobStore(ctx, s.jobStorage)
deferredJobIds, err = nextJob.Defer(deferCtx, jobErr)
deferredJobIds, err = nextJob.Defer(ctx, jobErr)
if err != nil {
s.logger.Printf("deferred job failed: %s", err)
}
Expand Down
6 changes: 1 addition & 5 deletions internal/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,7 @@ func TestScheduler_defer(t *testing.T) {
Type: "test-type",
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
ids := make(job.IDs, 0)
je, err := job.JobStoreFromContext(ctx)
if err != nil {
log.Fatal(err)
return ids, err
}
je := ss.JobStore

id1, err := je.EnqueueJob(job.Job{
Dir: document.DirHandleFromPath(dirPath),
Expand Down
6 changes: 1 addition & 5 deletions internal/state/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,7 @@ func TestJobStore_FinishJob_defer(t *testing.T) {

defer1Func := func(ctx context.Context, jobErr error) (job.IDs, error) {
ids := make(job.IDs, 0)
jobStore, err := job.JobStoreFromContext(ctx)
if err != nil {
return ids, err
}
jobStore := ss.JobStore

id, err := jobStore.EnqueueJob(job.Job{
Func: func(ctx context.Context) error {
Expand Down Expand Up @@ -672,7 +669,6 @@ func TestJobStore_FinishJob_defer(t *testing.T) {
}

ctx := context.Background()
ctx = job.WithJobStore(ctx, ss.JobStore)
// execute deferred func, which is what scheduler would do
deferredIds, err := defer1Func(ctx, nil)
if err != nil {
Expand Down

0 comments on commit 8ef3b35

Please sign in to comment.