From aee88dcde588d9dc7d8d2902f97b6b5502fdb771 Mon Sep 17 00:00:00 2001 From: Guillaume Lours Date: Tue, 10 Nov 2026 11:34:52 +0100 Subject: [PATCH] jobs: report jobs in the progress, dry run included up and create report each scheduled job like any other resource: created, or running when an identical job is already registered. A dry run used to say nothing about jobs, and run returned without any output; it now starts the dependencies through the dry-run client and reports the run, without reaching the engine's jobs. Co-Authored-By: Claude Sonnet 5.5 Signed-off-by: Guillaume Lours --- pkg/compose/jobs.go | 32 ++++++++++++++++++++----- pkg/compose/jobs_test.go | 50 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/pkg/compose/jobs.go b/pkg/compose/jobs.go index 5e7116025..eb93a1249 100644 --- a/pkg/compose/jobs.go +++ b/pkg/compose/jobs.go @@ -42,6 +42,7 @@ import ( jobsv0 "github.com/docker/compose/v5/internal/jobsapi" jobspb "github.com/docker/compose/v5/internal/jobsapi/protogen" "github.com/docker/compose/v5/internal/naming" + "github.com/docker/compose/v5/internal/progress" "github.com/docker/compose/v5/pkg/api" ) @@ -415,6 +416,8 @@ func (s *composeService) registerScheduledJobs(ctx context.Context, project *typ eg.SetLimit(s.maxConcurrency) for _, name := range names { eg.Go(func() error { + eventID := "Job " + name + s.events.On(progress.Creating(eventID)) job := project.Jobs[name] scoped, err := s.useAPISocket(scopedProjectForJob(project, name, job)) if err != nil { @@ -443,13 +446,24 @@ func (s *composeService) registerScheduledJobs(ctx context.Context, project *typ return err } if s.dryRun { - return nil // the jobs gRPC dials the real engine, bypassing the dry-run client + // the jobs gRPC dials the real engine, bypassing the dry-run client + s.events.On(progress.Created(eventID)) + return nil } - _, err = jc.Create(ctx, &jobsv0.CreateRequest{ + registered, err := jc.Create(ctx, &jobsv0.CreateRequest{ Name: engineJobName(project, name), Spec: spec, }) - return mapAlreadyExists(err, name, "up") + if err != nil { + return mapAlreadyExists(err, name, "up") + } + // an identical job already registered by an earlier up is armed, not created + outcome := api.StatusCreated + if !registered.Created { + outcome = api.StatusRunning + } + s.events.On(progress.New(eventID, api.Done, outcome)) + return nil }) } return eg.Wait() @@ -486,9 +500,6 @@ func (s *composeService) RunJob(ctx context.Context, project *types.Project, nam if err := refuseScheduledJobOverrides(project, name, job, options); err != nil { return 0, err } - if s.dryRun { - return 0, nil // the jobs gRPC dials the real engine, see registerScheduledJobs - } if options.Name != "" { logrus.Warnf("--name has no effect on a job: the engine names its run containers itself") } @@ -520,6 +531,15 @@ func (s *composeService) RunJob(ctx context.Context, project *types.Project, nam return 0, err } + // The dependencies above went through the dry-run client, the run itself + // would dial the real engine: see registerScheduledJobs. + if s.dryRun { + eventID := "Job " + name + s.events.On(progress.Creating(eventID)) + s.events.On(progress.Created(eventID)) + return 0, nil + } + svc, err := project.GetService(name) if err != nil { return 0, err diff --git a/pkg/compose/jobs_test.go b/pkg/compose/jobs_test.go index ecc881203..b127dee33 100644 --- a/pkg/compose/jobs_test.go +++ b/pkg/compose/jobs_test.go @@ -221,6 +221,7 @@ type fakeJobsClient struct { mu sync.Mutex // registerScheduledJobs calls Create concurrently createCalls []*jobsv0.CreateRequest createErr error + createNoop bool // an identical job is already registered createAndRunCalls []*jobsv0.CreateAndRunRequest createAndRunReply *jobsv0.CreateAndRunReply createAndRunErr error @@ -246,7 +247,7 @@ func (f *fakeJobsClient) Create(_ context.Context, req *jobsv0.CreateRequest) (* if f.createErr != nil { return nil, f.createErr } - return &jobsv0.CreateReply{Job: &jobsv0.Job{Name: req.Name}}, nil + return &jobsv0.CreateReply{Job: &jobsv0.Job{Name: req.Name}, Created: !f.createNoop}, nil } func (f *fakeJobsClient) CreateAndRun(_ context.Context, req *jobsv0.CreateAndRunRequest) (*jobsv0.CreateAndRunReply, error) { @@ -489,6 +490,29 @@ func TestRegisterScheduledJobs(t *testing.T) { assert.NilError(t, s.registerScheduledJobs(t.Context(), project())) assert.Equal(t, len(fake.createCalls), 0) }) + + // Jobs show up in the progress like any other resource, a dry run included: + // it is the only way a dry run tells what up and create would do for them. + t.Run("each scheduled job reports what happened to it", func(t *testing.T) { + for name, tc := range map[string]struct { + fake *fakeJobsClient + dryRun bool + want []string + }{ + "registered": {fake: &fakeJobsClient{}, want: []string{api.StatusCreating, api.StatusCreated}}, + "already registered": {fake: &fakeJobsClient{createNoop: true}, want: []string{api.StatusCreating, api.StatusRunning}}, + "dry run": {fake: &fakeJobsClient{}, dryRun: true, want: []string{api.StatusCreating, api.StatusCreated}}, + } { + t.Run(name, func(t *testing.T) { + s := newService(t, tc.fake) + events := newRecordingEvents() + s.events = events + s.dryRun = tc.dryRun + assert.NilError(t, s.registerScheduledJobs(t.Context(), project())) + assert.DeepEqual(t, events.byID, map[string][]string{"Job backup": tc.want, "Job sync": tc.want}) + }) + } + }) } // create arms the scheduled jobs like up does: the engine's cron is the only @@ -932,14 +956,32 @@ func TestRunJobDetachAndDryRun(t *testing.T) { assert.ErrorIs(t, err, api.ErrJobsNotSupported) }) - t.Run("a dry run starts nothing on the real engine", func(t *testing.T) { + // The dependencies are simulated by the dry-run client like for any service; + // the run itself is reported, not started. + t.Run("a dry run reports the run and starts nothing on the real engine", func(t *testing.T) { + tested, mockAPI := newPreStartTestServiceWithVersion(t, apiVersionJobs) + mockAPI.EXPECT().ImageInspect(gomock.Any(), gomock.Any(), gomock.Any()).Return(client.ImageInspectResult{}, nil).AnyTimes() + mockAPI.EXPECT().ContainerList(gomock.Any(), gomock.Any()).Return(client.ContainerListResult{}, nil).AnyTimes() + mockAPI.EXPECT().NetworkList(gomock.Any(), gomock.Any()).Return(client.NetworkListResult{}, nil).AnyTimes() + mockAPI.EXPECT().VolumeList(gomock.Any(), gomock.Any()).Return(client.VolumeListResult{}, nil).AnyTimes() fake := &fakeJobsClient{} - tested := newJobsTestService(t, apiVersionJobs, fake) + tested.jobsGRPCOnce.Do(func() {}) + tested.jobsAPI = fake + events := newRecordingEvents() + tested.events = events tested.dryRun = true - code, err := tested.RunJob(t.Context(), scheduledJobsProject(), "migrate", options) + + project := scheduledJobsProject() + project.Services = types.Services{"migrate": {Name: "migrate", ContainerSpec: types.ContainerSpec{Image: "alpine"}}} + job := project.Jobs["migrate"] + job.Image = "alpine" + project.Jobs["migrate"] = job + + code, err := tested.RunJob(t.Context(), project, "migrate", options) assert.NilError(t, err) assert.Equal(t, code, 0) assert.Equal(t, len(fake.createAndRunCalls)+len(fake.createCalls)+len(fake.runCalls), 0) + assert.DeepEqual(t, events.byID["Job migrate"], []string{api.StatusCreating, api.StatusCreated}) }) }