github.com/annwntech/go-micro/v2@v2.9.5/runtime/runtime.go (about)

     1  // Package runtime is a service runtime manager
     2  package runtime
     3  
     4  import (
     5  	"errors"
     6  	"time"
     7  )
     8  
     9  var (
    10  	// DefaultRuntime is default micro runtime
    11  	DefaultRuntime Runtime = NewRuntime()
    12  	// DefaultName is default runtime service name
    13  	DefaultName = "go.micro.runtime"
    14  
    15  	ErrAlreadyExists = errors.New("already exists")
    16  )
    17  
    18  // Runtime is a service runtime manager
    19  type Runtime interface {
    20  	// Init initializes runtime
    21  	Init(...Option) error
    22  	// Create registers a service
    23  	Create(*Service, ...CreateOption) error
    24  	// Read returns the service
    25  	Read(...ReadOption) ([]*Service, error)
    26  	// Update the service in place
    27  	Update(*Service, ...UpdateOption) error
    28  	// Remove a service
    29  	Delete(*Service, ...DeleteOption) error
    30  	// Logs returns the logs for a service
    31  	Logs(*Service, ...LogsOption) (LogStream, error)
    32  	// Start starts the runtime
    33  	Start() error
    34  	// Stop shuts down the runtime
    35  	Stop() error
    36  	// String describes runtime
    37  	String() string
    38  }
    39  
    40  // Stream returns a log stream
    41  type LogStream interface {
    42  	Error() error
    43  	Chan() chan LogRecord
    44  	Stop() error
    45  }
    46  
    47  type LogRecord struct {
    48  	Message  string
    49  	Metadata map[string]string
    50  }
    51  
    52  // Scheduler is a runtime service scheduler
    53  type Scheduler interface {
    54  	// Notify publishes schedule events
    55  	Notify() (<-chan Event, error)
    56  	// Close stops the scheduler
    57  	Close() error
    58  }
    59  
    60  // EventType defines schedule event
    61  type EventType int
    62  
    63  const (
    64  	// Create is emitted when a new build has been craeted
    65  	Create EventType = iota
    66  	// Update is emitted when a new update become available
    67  	Update
    68  	// Delete is emitted when a build has been deleted
    69  	Delete
    70  )
    71  
    72  // String returns human readable event type
    73  func (t EventType) String() string {
    74  	switch t {
    75  	case Create:
    76  		return "create"
    77  	case Delete:
    78  		return "delete"
    79  	case Update:
    80  		return "update"
    81  	default:
    82  		return "unknown"
    83  	}
    84  }
    85  
    86  // Event is notification event
    87  type Event struct {
    88  	// ID of the event
    89  	ID string
    90  	// Type is event type
    91  	Type EventType
    92  	// Timestamp is event timestamp
    93  	Timestamp time.Time
    94  	// Service the event relates to
    95  	Service *Service
    96  	// Options to use when processing the event
    97  	Options *CreateOptions
    98  }
    99  
   100  // Service is runtime service
   101  type Service struct {
   102  	// Name of the service
   103  	Name string
   104  	// Version of the service
   105  	Version string
   106  	// url location of source
   107  	Source string
   108  	// Metadata stores metadata
   109  	Metadata map[string]string
   110  }