github.com/matrixorigin/matrixone@v1.2.0/pkg/pb/task/task.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package task 16 17 import ( 18 "fmt" 19 "time" 20 21 "github.com/matrixorigin/matrixone/pkg/common/moerr" 22 ) 23 24 type Task interface { 25 GetID() uint64 26 GetMetadata() TaskMetadata 27 } 28 29 // IsDelayTask returns true if the task is a delay task 30 func (m AsyncTask) IsDelayTask() bool { 31 return m.Metadata.Options.DelayDuration > 0 32 } 33 34 // GetDelayDuration returns delay duration 35 func (m AsyncTask) GetDelayDuration() time.Duration { 36 return time.Duration(m.Metadata.Options.DelayDuration) 37 } 38 39 // DebugString returns the debug string 40 func (m AsyncTask) DebugString() string { 41 return fmt.Sprintf("%s/%d", m.Metadata.ID, m.Metadata.Executor) 42 } 43 44 // DebugString returns the debug string 45 func (m CronTask) DebugString() string { 46 return fmt.Sprintf("%s/%d/%s", 47 m.Metadata.ID, 48 m.TriggerTimes, 49 m.CronExpr) 50 } 51 52 // Type returns the task's type. It panics if the type is invalid. 53 func (t *Details) Type() TaskType { 54 typ, err := detailsType(t.Details) 55 if err != nil { 56 panic(err) 57 } 58 return typ 59 } 60 61 func detailsType(d isDetails_Details) (TaskType, error) { 62 switch d := d.(type) { 63 case *Details_Connector: 64 return TaskType_TypeKafkaSinkConnector, nil 65 default: 66 return TaskType_TypeUnknown, moerr.NewInternalErrorNoCtx("Unknown details type: %T", d) 67 } 68 } 69 70 func (t *Details) Scan(src any) error { 71 var data []byte 72 if b, ok := src.([]byte); ok { 73 data = b 74 } else if s, ok := src.(string); ok { 75 data = []byte(s) 76 } 77 return t.Unmarshal(data) 78 }