2019-09-14 06:33:14 +02:00
|
|
|
// Package runtime is a service runtime manager
|
|
|
|
package runtime
|
|
|
|
|
2020-01-18 04:13:24 +02:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
2019-11-02 15:25:10 +02:00
|
|
|
|
|
|
|
var (
|
|
|
|
// DefaultRuntime is default micro runtime
|
|
|
|
DefaultRuntime Runtime = NewRuntime()
|
2019-11-20 16:54:42 +02:00
|
|
|
// DefaultName is default runtime service name
|
|
|
|
DefaultName = "go.micro.runtime"
|
2020-01-18 04:13:24 +02:00
|
|
|
|
|
|
|
ErrAlreadyExists = errors.New("already exists")
|
2019-11-02 15:25:10 +02:00
|
|
|
)
|
|
|
|
|
2019-09-14 06:33:14 +02:00
|
|
|
// Runtime is a service runtime manager
|
|
|
|
type Runtime interface {
|
2020-02-03 17:56:16 +02:00
|
|
|
// String describes runtime
|
|
|
|
String() string
|
2019-11-02 15:25:10 +02:00
|
|
|
// Init initializes runtime
|
|
|
|
Init(...Option) error
|
2019-11-15 15:41:40 +02:00
|
|
|
// Create registers a service
|
2019-09-24 19:32:35 +02:00
|
|
|
Create(*Service, ...CreateOption) error
|
2019-11-25 18:31:14 +02:00
|
|
|
// Read returns the service
|
2019-11-29 13:35:00 +02:00
|
|
|
Read(...ReadOption) ([]*Service, error)
|
2019-10-29 14:29:21 +02:00
|
|
|
// Update the service in place
|
|
|
|
Update(*Service) error
|
2019-11-15 15:41:40 +02:00
|
|
|
// Remove a service
|
|
|
|
Delete(*Service) error
|
2019-10-29 14:29:21 +02:00
|
|
|
// List the managed services
|
|
|
|
List() ([]*Service, error)
|
2019-11-15 15:41:40 +02:00
|
|
|
// Start starts the runtime
|
2019-09-14 06:58:03 +02:00
|
|
|
Start() error
|
2019-11-15 15:41:40 +02:00
|
|
|
// Stop shuts down the runtime
|
2019-09-14 06:33:14 +02:00
|
|
|
Stop() error
|
2020-04-01 15:40:15 +02:00
|
|
|
// Logs
|
|
|
|
Logs(*Service, ...LogsOption) (LogStream, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stream returns a log stream
|
|
|
|
type LogStream interface {
|
2020-04-02 00:03:26 +02:00
|
|
|
Error() error
|
2020-04-01 15:40:15 +02:00
|
|
|
Chan() chan LogRecord
|
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type LogRecord struct {
|
|
|
|
Message string
|
|
|
|
Metadata map[string]string
|
2019-09-14 06:33:14 +02:00
|
|
|
}
|
|
|
|
|
2020-01-16 15:34:04 +02:00
|
|
|
// Scheduler is a runtime service scheduler
|
|
|
|
type Scheduler interface {
|
|
|
|
// Notify publishes schedule events
|
2019-11-02 15:25:10 +02:00
|
|
|
Notify() (<-chan Event, error)
|
2020-01-16 15:34:04 +02:00
|
|
|
// Close stops the scheduler
|
2019-11-02 15:25:10 +02:00
|
|
|
Close() error
|
2019-09-14 06:33:14 +02:00
|
|
|
}
|
|
|
|
|
2020-01-16 15:34:04 +02:00
|
|
|
// EventType defines schedule event
|
2019-11-02 15:25:10 +02:00
|
|
|
type EventType int
|
2019-09-14 06:58:03 +02:00
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
const (
|
|
|
|
// Create is emitted when a new build has been craeted
|
|
|
|
Create EventType = iota
|
|
|
|
// Update is emitted when a new update become available
|
|
|
|
Update
|
|
|
|
// Delete is emitted when a build has been deleted
|
|
|
|
Delete
|
|
|
|
)
|
2019-10-29 14:29:21 +02:00
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
// String returns human readable event type
|
|
|
|
func (t EventType) String() string {
|
|
|
|
switch t {
|
|
|
|
case Create:
|
|
|
|
return "create"
|
|
|
|
case Delete:
|
|
|
|
return "delete"
|
|
|
|
case Update:
|
|
|
|
return "update"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
2019-10-29 14:29:21 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
// Event is notification event
|
|
|
|
type Event struct {
|
|
|
|
// Type is event type
|
|
|
|
Type EventType
|
|
|
|
// Timestamp is event timestamp
|
|
|
|
Timestamp time.Time
|
|
|
|
// Service is the name of the service
|
|
|
|
Service string
|
|
|
|
// Version of the build
|
|
|
|
Version string
|
2019-09-14 06:33:14 +02:00
|
|
|
}
|
|
|
|
|
2019-11-02 15:25:10 +02:00
|
|
|
// Service is runtime service
|
|
|
|
type Service struct {
|
|
|
|
// Name of the service
|
|
|
|
Name string
|
2019-11-24 00:50:13 +02:00
|
|
|
// Version of the service
|
|
|
|
Version string
|
2019-11-02 15:25:10 +02:00
|
|
|
// url location of source
|
|
|
|
Source string
|
2019-11-22 19:10:00 +02:00
|
|
|
// Metadata stores metadata
|
|
|
|
Metadata map[string]string
|
2019-09-14 06:33:14 +02:00
|
|
|
}
|