mirror of
https://github.com/docker/compose.git
synced 2026-11-17 13:45:39 +00:00
jobs: report jobs in the progress, dry run included
Some checks are pending
ci / validate (lint) (push) Waiting to run
ci / validate (relay-lint) (push) Waiting to run
ci / validate (relay-test) (push) Waiting to run
ci / validate (validate-docs) (push) Waiting to run
ci / validate (validate-go-mod) (push) Waiting to run
ci / validate (validate-headers) (push) Waiting to run
ci / validate (validate-mocks) (push) Waiting to run
ci / binary (push) Waiting to run
ci / binary-finalize (push) Blocked by required conditions
ci / bin-image-test (push) Waiting to run
ci / relay-image-test (push) Waiting to run
ci / test (push) Waiting to run
ci / e2e (plugin, oldstable, graphdriver) (push) Waiting to run
ci / e2e (standalone, oldstable, graphdriver) (push) Waiting to run
ci / e2e (plugin, stable, containerd) (push) Waiting to run
ci / e2e (plugin, stable, graphdriver) (push) Waiting to run
ci / e2e (standalone, stable, containerd) (push) Waiting to run
ci / e2e (standalone, stable, graphdriver) (push) Waiting to run
ci / e2e (plugin, test, graphdriver, jobs extension) (push) Waiting to run
ci / coverage (push) Blocked by required conditions
ci / ci-ok (push) Blocked by required conditions
ci / release (push) Blocked by required conditions
docs-upstream / docs-yaml (push) Waiting to run
docs-upstream / validate (push) Blocked by required conditions
merge / bin-image-prepare (push) Waiting to run
merge / bin-image (push) Blocked by required conditions
merge / relay-image (push) Waiting to run
merge / module-image (push) Waiting to run
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run
zizmor / zizmor (push) Waiting to run
Some checks are pending
ci / validate (lint) (push) Waiting to run
ci / validate (relay-lint) (push) Waiting to run
ci / validate (relay-test) (push) Waiting to run
ci / validate (validate-docs) (push) Waiting to run
ci / validate (validate-go-mod) (push) Waiting to run
ci / validate (validate-headers) (push) Waiting to run
ci / validate (validate-mocks) (push) Waiting to run
ci / binary (push) Waiting to run
ci / binary-finalize (push) Blocked by required conditions
ci / bin-image-test (push) Waiting to run
ci / relay-image-test (push) Waiting to run
ci / test (push) Waiting to run
ci / e2e (plugin, oldstable, graphdriver) (push) Waiting to run
ci / e2e (standalone, oldstable, graphdriver) (push) Waiting to run
ci / e2e (plugin, stable, containerd) (push) Waiting to run
ci / e2e (plugin, stable, graphdriver) (push) Waiting to run
ci / e2e (standalone, stable, containerd) (push) Waiting to run
ci / e2e (standalone, stable, graphdriver) (push) Waiting to run
ci / e2e (plugin, test, graphdriver, jobs extension) (push) Waiting to run
ci / coverage (push) Blocked by required conditions
ci / ci-ok (push) Blocked by required conditions
ci / release (push) Blocked by required conditions
docs-upstream / docs-yaml (push) Waiting to run
docs-upstream / validate (push) Blocked by required conditions
merge / bin-image-prepare (push) Waiting to run
merge / bin-image (push) Blocked by required conditions
merge / relay-image (push) Waiting to run
merge / module-image (push) Waiting to run
Scorecards supply-chain security / Scorecards analysis (push) Waiting to run
zizmor / zizmor (push) Waiting to run
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 <job> 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 <noreply@anthropic.com> Signed-off-by: Guillaume Lours <glours@users.noreply.github.com>
This commit is contained in:
parent
0d775158dc
commit
aee88dcde5
2 changed files with 72 additions and 10 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue