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