Dragonfly2
99 строк · 2.9 Кб
1/*
2* Copyright 2020 The Dragonfly Authors
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17package service
18
19import (
20"context"
21"strconv"
22
23machineryv1tasks "github.com/RichardKnop/machinery/v1/tasks"
24"google.golang.org/grpc/codes"
25"google.golang.org/grpc/status"
26
27logger "d7y.io/dragonfly/v2/internal/dflog"
28internaljob "d7y.io/dragonfly/v2/internal/job"
29"d7y.io/dragonfly/v2/manager/models"
30"d7y.io/dragonfly/v2/manager/types"
31)
32
33const (
34// V1PreheatingStatePending is the preheating is waiting for starting
35V1PreheatingStatePending = "WAITING"
36
37// V1PreheatingStateRunning is the preheating is running
38V1PreheatingStateRunning = "RUNNING"
39
40// V1PreheatingStateSuccess is the preheating is success
41V1PreheatingStateSuccess = "SUCCESS"
42
43// V1PreheatingStateFail is the preheating is failed
44V1PreheatingStateFail = "FAIL"
45)
46
47func (s *service) CreateV1Preheat(ctx context.Context, json types.CreateV1PreheatRequest) (*types.CreateV1PreheatResponse, error) {
48job, err := s.CreatePreheatJob(ctx, types.CreatePreheatJobRequest{
49Type: internaljob.PreheatJob,
50Args: types.PreheatArgs{
51Type: json.Type,
52URL: json.URL,
53FilteredQueryParams: json.FilteredQueryParams,
54Headers: json.Headers,
55},
56})
57if err != nil {
58return nil, err
59}
60
61return &types.CreateV1PreheatResponse{
62ID: strconv.FormatUint(uint64(job.ID), 10),
63}, nil
64}
65
66func (s *service) GetV1Preheat(ctx context.Context, rawID string) (*types.GetV1PreheatResponse, error) {
67id, err := strconv.ParseUint(rawID, 10, 32)
68if err != nil {
69logger.Errorf("preheat convert error", err)
70return nil, status.Error(codes.Unknown, err.Error())
71}
72
73job := models.Job{}
74if err := s.db.WithContext(ctx).First(&job, uint(id)).Error; err != nil {
75return nil, status.Error(codes.Unknown, err.Error())
76}
77
78return &types.GetV1PreheatResponse{
79ID: strconv.FormatUint(uint64(job.ID), 10),
80Status: convertState(job.State),
81StartTime: job.CreatedAt.String(),
82FinishTime: job.UpdatedAt.String(),
83}, nil
84}
85
86func convertState(state string) string {
87switch state {
88case machineryv1tasks.StatePending, machineryv1tasks.StateReceived, machineryv1tasks.StateRetry:
89return V1PreheatingStatePending
90case machineryv1tasks.StateStarted:
91return V1PreheatingStateRunning
92case machineryv1tasks.StateSuccess:
93return V1PreheatingStateSuccess
94case machineryv1tasks.StateFailure:
95return V1PreheatingStateFail
96}
97
98return V1PreheatingStateFail
99}
100