github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/tasks/handler.go (about)

     1  // Copyright 2021 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 tasks
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    19  	"github.com/matrixorigin/matrixone/pkg/logutil"
    20  	ops "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/tasks/worker"
    21  )
    22  
    23  var (
    24  	ErrTaskHandleEnqueue = moerr.NewInternalErrorNoCtx("tae: task handle enqueue")
    25  )
    26  
    27  type BaseTaskHandler struct {
    28  	ops.OpWorker
    29  }
    30  
    31  func NewBaseEventHandler(name string) *BaseTaskHandler {
    32  	h := &BaseTaskHandler{
    33  		OpWorker: *ops.NewOpWorker(name),
    34  	}
    35  	return h
    36  }
    37  
    38  func (h *BaseTaskHandler) Enqueue(task Task) {
    39  	if !h.SendOp(task) {
    40  		task.SetError(ErrTaskHandleEnqueue)
    41  		err := task.Cancel()
    42  		if err != nil {
    43  			logutil.Warnf("%v", err)
    44  		}
    45  	}
    46  }
    47  
    48  func (h *BaseTaskHandler) Execute(task Task) {
    49  	h.ExecFunc(task)
    50  }
    51  
    52  func (h *BaseTaskHandler) Close() error {
    53  	h.Stop()
    54  	return nil
    55  }
    56  
    57  type singleWorkerHandler struct {
    58  	BaseTaskHandler
    59  }
    60  
    61  func NewSingleWorkerHandler(name string) *singleWorkerHandler {
    62  	h := &singleWorkerHandler{
    63  		BaseTaskHandler: *NewBaseEventHandler(name),
    64  	}
    65  	return h
    66  }