github.com/thanos-io/thanos@v0.32.5/internal/cortex/util/services/service.go (about)

     1  // Copyright (c) The Cortex Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package services
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  )
    10  
    11  // State of the service. See Service interface for full state diagram.
    12  type State int
    13  
    14  // Possible states to represent the service State.
    15  const (
    16  	New        State = iota // New: Service is new, not running yet. Initial State.
    17  	Starting                // Starting: Service is starting. If starting succeeds, service enters Running state.
    18  	Running                 // Running: Service is fully running now. When service stops running, it enters Stopping state.
    19  	Stopping                // Stopping: Service is shutting down
    20  	Terminated              // Terminated: Service has stopped successfully. Terminal state.
    21  	Failed                  // Failed: Service has failed in Starting, Running or Stopping state. Terminal state.
    22  )
    23  
    24  func (s State) String() string {
    25  	switch s {
    26  	case New:
    27  		return "New"
    28  	case Starting:
    29  		return "Starting"
    30  	case Running:
    31  		return "Running"
    32  	case Stopping:
    33  		return "Stopping"
    34  	case Terminated:
    35  		return "Terminated"
    36  	case Failed:
    37  		return "Failed"
    38  	default:
    39  		return fmt.Sprintf("Unknown state: %d", s)
    40  	}
    41  }
    42  
    43  // Service defines interface for controlling a service.
    44  //
    45  // State diagram for the service:
    46  //
    47  //	   ┌────────────────────────────────────────────────────────────────────┐
    48  //	   │                                                                    │
    49  //	   │                                                                    ▼
    50  //	┌─────┐      ┌──────────┐      ┌─────────┐     ┌──────────┐      ┌────────────┐
    51  //	│ New │─────▶│ Starting │─────▶│ Running │────▶│ Stopping │───┬─▶│ Terminated │
    52  //	└─────┘      └──────────┘      └─────────┘     └──────────┘   │  └────────────┘
    53  //	                   │                                          │
    54  //	                   │                                          │
    55  //	                   │                                          │   ┌────────┐
    56  //	                   └──────────────────────────────────────────┴──▶│ Failed │
    57  //	                                                                  └────────┘
    58  type Service interface {
    59  	// StartAsync starts Service asynchronously. Service must be in New State, otherwise error is returned.
    60  	// Context is used as a parent context for service own context.
    61  	StartAsync(ctx context.Context) error
    62  
    63  	// AwaitRunning waits until service gets into Running state.
    64  	// If service is in New or Starting state, this method is blocking.
    65  	// If service is already in Running state, returns immediately with no error.
    66  	// If service is in a state, from which it cannot get into Running state, error is returned immediately.
    67  	AwaitRunning(ctx context.Context) error
    68  
    69  	// StopAsync tell the service to stop. This method doesn't block and can be called multiple times.
    70  	// If Service is New, it is Terminated without having been started nor stopped.
    71  	// If Service is in Starting or Running state, this initiates shutdown and returns immediately.
    72  	// If Service has already been stopped, this method returns immediately, without taking action.
    73  	StopAsync()
    74  
    75  	// AwaitTerminated waits for the service to reach Terminated or Failed state. If service is already in one of these states,
    76  	// when method is called, method returns immediately.
    77  	// If service enters Terminated state, this method returns nil.
    78  	// If service enters Failed state, or context is finished before reaching Terminated or Failed, error is returned.
    79  	AwaitTerminated(ctx context.Context) error
    80  
    81  	// FailureCase returns error if Service is in Failed state.
    82  	// If Service is not in Failed state, this method returns nil.
    83  	FailureCase() error
    84  
    85  	// State returns current state of the service.
    86  	State() State
    87  
    88  	// AddListener adds listener to this service. Listener will be notified on subsequent state transitions
    89  	// of the service. Previous state transitions are not replayed, so it is suggested to add listeners before
    90  	// service is started.
    91  	//
    92  	// AddListener guarantees execution ordering across calls to a given listener but not across calls to
    93  	// multiple listeners. Specifically, a given listener will have its callbacks invoked in the same order
    94  	// as the service enters those states. Additionally, at most one of the listener's callbacks will execute
    95  	// at once. However, multiple listeners' callbacks may execute concurrently, and listeners may execute
    96  	// in an order different from the one in which they were registered.
    97  	AddListener(listener Listener)
    98  }
    99  
   100  // NamedService extends Service with a name.
   101  type NamedService interface {
   102  	Service
   103  
   104  	// ServiceName returns name of the service, if it has one.
   105  	ServiceName() string
   106  }
   107  
   108  // Listener receives notifications about Service state changes.
   109  type Listener interface {
   110  	// Starting is called when the service transitions from NEW to STARTING.
   111  	Starting()
   112  
   113  	// Running is called when the service transitions from STARTING to RUNNING.
   114  	Running()
   115  
   116  	// Stopping is called when the service transitions to the STOPPING state.
   117  	Stopping(from State)
   118  
   119  	// Terminated is called when the service transitions to the TERMINATED state.
   120  	Terminated(from State)
   121  
   122  	// Failed is called when the service transitions to the FAILED state.
   123  	Failed(from State, failure error)
   124  }