-
Notifications
You must be signed in to change notification settings - Fork 302
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
feat: add delete task and list tasks manager api with request type and service type. #3378
Merged
gaius-qi
merged 9 commits into
dragonflyoss:main
from
IRONICBo:feat/add-delete-task-manager-api
Aug 13, 2024
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
64420a9
feat: add delete task and list tasks manager api define.
IRONICBo b3d4e17
feat: add manager job service and service mock for delete task and li…
IRONICBo df2ae9c
chore: fix lint issue and generate the mocked job file.
IRONICBo dcbb0f7
feat: add delete task job and list tasks job in scheduler job impleme…
IRONICBo 16cb224
fix: update delete task job and list tasks job api args.
IRONICBo 16e73d6
feat: add delete task job and list tasks job in scheduler job impleme…
IRONICBo a212060
feat: add delete task job and list tasks job scheduler define and bas…
IRONICBo eae443a
feat: update delete task job and list tasks with concurrency and filt…
IRONICBo 915d880
fix: update getFinishedPeers function and remove concurrency feature.
IRONICBo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright 2024 The Dragonfly Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
//go:generate mockgen -destination mocks/manager_tasks_mock.go -source manager_tasks.go -package mocks | ||
|
||
package job | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
machineryv1tasks "github.com/RichardKnop/machinery/v1/tasks" | ||
"github.com/google/uuid" | ||
"go.opentelemetry.io/otel/trace" | ||
|
||
logger "d7y.io/dragonfly/v2/internal/dflog" | ||
internaljob "d7y.io/dragonfly/v2/internal/job" | ||
"d7y.io/dragonfly/v2/manager/config" | ||
"d7y.io/dragonfly/v2/manager/models" | ||
"d7y.io/dragonfly/v2/manager/types" | ||
) | ||
|
||
// ManagerTask is an interface for delete and list tasks. | ||
type ManagerTasks interface { | ||
// CreateDeleteTask create a delete task job | ||
CreateDeleteTask(context.Context, []models.Scheduler, types.DeleteTasksArgs) (*internaljob.GroupJobState, error) | ||
// CreateListTasks create a list tasks job | ||
CreateListTasks(context.Context, []models.Scheduler, types.ListTasksArgs) (*internaljob.GroupJobState, error) | ||
} | ||
|
||
// managerTasks is an implementation of ManagerTasks. | ||
type managerTasks struct { | ||
job *internaljob.Job | ||
registryTimeout time.Duration | ||
} | ||
|
||
// newManagerTasks create a new ManagerTasks. | ||
func newManagerTasks(job *internaljob.Job, registryTimeout time.Duration) ManagerTasks { | ||
return &managerTasks{ | ||
job: job, | ||
registryTimeout: registryTimeout, | ||
} | ||
} | ||
|
||
// Create a delete task job. | ||
func (m *managerTasks) CreateDeleteTask(ctx context.Context, schedulers []models.Scheduler, json types.DeleteTasksArgs) (*internaljob.GroupJobState, error) { | ||
var span trace.Span | ||
ctx, span = tracer.Start(ctx, config.SpanDeleteTask, trace.WithSpanKind(trace.SpanKindProducer)) | ||
span.SetAttributes(config.AttributeDeleteTaskID.String(json.TaskID)) | ||
defer span.End() | ||
|
||
args, err := internaljob.MarshalRequest(json) | ||
if err != nil { | ||
logger.Errorf("delete task marshal request: %v, error: %v", args, err) | ||
return nil, err | ||
} | ||
|
||
// Initialize queues. | ||
queues := getSchedulerQueues(schedulers) | ||
return m.createGroupJob(ctx, internaljob.DeleteTaskJob, args, queues) | ||
} | ||
|
||
// Create a list tasks job. | ||
func (m *managerTasks) CreateListTasks(ctx context.Context, schedulers []models.Scheduler, json types.ListTasksArgs) (*internaljob.GroupJobState, error) { | ||
var span trace.Span | ||
ctx, span = tracer.Start(ctx, config.SpanListTasks, trace.WithSpanKind(trace.SpanKindProducer)) | ||
span.SetAttributes(config.AttributeListTasksID.String(json.TaskID)) | ||
span.SetAttributes(config.AttributeListTasksPage.Int(json.Page)) | ||
span.SetAttributes(config.AttributeListTasksPerPage.Int(json.PerPage)) | ||
defer span.End() | ||
|
||
args, err := internaljob.MarshalRequest(json) | ||
if err != nil { | ||
logger.Errorf("list tasks marshal request: %v, error: %v", args, err) | ||
return nil, err | ||
} | ||
|
||
// Initialize queues. | ||
queues := getSchedulerQueues(schedulers) | ||
return m.createGroupJob(ctx, internaljob.ListTasksJob, args, queues) | ||
} | ||
|
||
// createGroupJob creates a group job. | ||
func (m *managerTasks) createGroupJob(ctx context.Context, name string, args []machineryv1tasks.Arg, queues []internaljob.Queue) (*internaljob.GroupJobState, error) { | ||
var signatures []*machineryv1tasks.Signature | ||
for _, queue := range queues { | ||
signatures = append(signatures, &machineryv1tasks.Signature{ | ||
UUID: fmt.Sprintf("task_%s", uuid.New().String()), | ||
Name: name, | ||
RoutingKey: queue.String(), | ||
Args: args, | ||
}) | ||
} | ||
|
||
group, err := machineryv1tasks.NewGroup(signatures...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var tasks []machineryv1tasks.Signature | ||
for _, signature := range signatures { | ||
tasks = append(tasks, *signature) | ||
} | ||
|
||
logger.Infof("create manager tasks group %s in queues %v, tasks: %#v", group.GroupUUID, queues, tasks) | ||
if _, err := m.job.Server.SendGroupWithContext(ctx, group, 0); err != nil { | ||
logger.Errorf("create manager tasks group %s failed", group.GroupUUID, err) | ||
return nil, err | ||
} | ||
|
||
return &internaljob.GroupJobState{ | ||
GroupUUID: group.GroupUUID, | ||
State: machineryv1tasks.StatePending, | ||
CreatedAt: time.Now(), | ||
}, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove extra spaces in log.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, done.