go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/scheduler/appengine/task/noop/noop.go (about)

     1  // Copyright 2015 The LUCI Authors.
     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 noop implements tasks that do nothing at all. Used for testing
    16  // only.
    17  package noop
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"fmt"
    23  	"time"
    24  
    25  	"github.com/golang/protobuf/proto"
    26  
    27  	"google.golang.org/api/pubsub/v1"
    28  
    29  	"go.chromium.org/luci/common/clock"
    30  	"go.chromium.org/luci/config/validation"
    31  
    32  	api "go.chromium.org/luci/scheduler/api/scheduler/v1"
    33  	"go.chromium.org/luci/scheduler/appengine/internal"
    34  	"go.chromium.org/luci/scheduler/appengine/messages"
    35  	"go.chromium.org/luci/scheduler/appengine/task"
    36  )
    37  
    38  // TaskManager implements task.Manager interface for tasks defined with
    39  // NoopTask proto message.
    40  type TaskManager struct {
    41  }
    42  
    43  // Name is part of Manager interface.
    44  func (m TaskManager) Name() string {
    45  	return "noop"
    46  }
    47  
    48  // ProtoMessageType is part of Manager interface.
    49  func (m TaskManager) ProtoMessageType() proto.Message {
    50  	return (*messages.NoopTask)(nil)
    51  }
    52  
    53  // ValidateProtoMessage is part of Manager interface.
    54  func (m TaskManager) ValidateProtoMessage(c *validation.Context, msg proto.Message, realmID string) {}
    55  
    56  // Traits is part of Manager interface.
    57  func (m TaskManager) Traits() task.Traits {
    58  	return task.Traits{
    59  		Multistage: false, // we don't use task.StatusRunning state
    60  	}
    61  }
    62  
    63  // LaunchTask is part of Manager interface.
    64  func (m TaskManager) LaunchTask(c context.Context, ctl task.Controller) error {
    65  	cfg := ctl.Task().(*messages.NoopTask)
    66  
    67  	sleepFor := time.Duration(cfg.SleepMs) * time.Millisecond
    68  	if sleepFor == 0 {
    69  		sleepFor = 20 * time.Second
    70  	}
    71  	ctl.DebugLog("Running noop task for %s", sleepFor)
    72  	clock.Sleep(c, sleepFor)
    73  
    74  	for i := int64(0); i < cfg.TriggersCount; i++ {
    75  		ctl.EmitTrigger(c, &internal.Trigger{
    76  			Id:      fmt.Sprintf("noop:%d:%d", ctl.InvocationID(), i),
    77  			Payload: &internal.Trigger_Noop{Noop: &api.NoopTrigger{}},
    78  		})
    79  	}
    80  
    81  	ctl.State().Status = task.StatusSucceeded
    82  	return nil
    83  }
    84  
    85  // AbortTask is part of Manager interface.
    86  func (m TaskManager) AbortTask(c context.Context, ctl task.Controller) error {
    87  	return nil
    88  }
    89  
    90  // ExamineNotification is part of Manager interface.
    91  func (m TaskManager) ExamineNotification(c context.Context, msg *pubsub.PubsubMessage) string {
    92  	return ""
    93  }
    94  
    95  // HandleNotification is part of Manager interface.
    96  func (m TaskManager) HandleNotification(c context.Context, ctl task.Controller, msg *pubsub.PubsubMessage) error {
    97  	return errors.New("not implemented")
    98  }
    99  
   100  // HandleTimer is part of Manager interface.
   101  func (m TaskManager) HandleTimer(c context.Context, ctl task.Controller, name string, payload []byte) error {
   102  	return nil
   103  }
   104  
   105  // GetDebugState is part of Manager interface.
   106  func (m TaskManager) GetDebugState(c context.Context, ctl task.ControllerReadOnly) (*internal.DebugManagerState, error) {
   107  	return nil, fmt.Errorf("no debug state")
   108  }