2019-09-13 21:33:14 -07:00
|
|
|
// Package runtime is a service runtime manager
|
|
|
|
package runtime
|
|
|
|
|
2020-01-18 02:13:24 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
2020-11-18 16:50:41 +03:00
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/metadata"
|
2020-01-18 02:13:24 +00:00
|
|
|
)
|
2019-11-02 13:25:10 +00:00
|
|
|
|
|
|
|
var (
|
2020-11-03 01:08:23 +03:00
|
|
|
// ErrAlreadyExists error
|
2020-01-18 02:13:24 +00:00
|
|
|
ErrAlreadyExists = errors.New("already exists")
|
2019-11-02 13:25:10 +00:00
|
|
|
)
|
|
|
|
|
2019-09-13 21:33:14 -07:00
|
|
|
// Runtime is a service runtime manager
|
|
|
|
type Runtime interface {
|
2019-11-02 13:25:10 +00:00
|
|
|
// Init initializes runtime
|
|
|
|
Init(...Option) error
|
2019-11-15 13:41:40 +00:00
|
|
|
// Create registers a service
|
2019-09-24 18:32:35 +01:00
|
|
|
Create(*Service, ...CreateOption) error
|
2019-11-25 16:31:14 +00:00
|
|
|
// Read returns the service
|
2019-11-29 11:35:00 +00:00
|
|
|
Read(...ReadOption) ([]*Service, error)
|
2019-10-29 12:29:21 +00:00
|
|
|
// Update the service in place
|
2020-04-23 13:53:42 +01:00
|
|
|
Update(*Service, ...UpdateOption) error
|
2019-11-15 13:41:40 +00:00
|
|
|
// Remove a service
|
2020-04-23 13:53:42 +01:00
|
|
|
Delete(*Service, ...DeleteOption) error
|
2020-04-12 23:41:21 +01:00
|
|
|
// Logs returns the logs for a service
|
2020-08-11 22:57:30 +01:00
|
|
|
Logs(*Service, ...LogsOption) (Logs, error)
|
2019-11-15 13:41:40 +00:00
|
|
|
// Start starts the runtime
|
2019-09-13 21:58:03 -07:00
|
|
|
Start() error
|
2019-11-15 13:41:40 +00:00
|
|
|
// Stop shuts down the runtime
|
2019-09-13 21:33:14 -07:00
|
|
|
Stop() error
|
2020-04-12 23:41:21 +01:00
|
|
|
// String describes runtime
|
|
|
|
String() string
|
2020-04-01 15:40:15 +02:00
|
|
|
}
|
|
|
|
|
2020-08-11 22:57:30 +01:00
|
|
|
// Logs returns a log stream
|
|
|
|
type Logs interface {
|
2021-03-06 23:33:37 +03:00
|
|
|
// Error returns error
|
2020-04-02 00:03:26 +02:00
|
|
|
Error() error
|
2021-03-06 19:45:13 +03:00
|
|
|
// Chan return chan log
|
2020-08-11 22:57:30 +01:00
|
|
|
Chan() chan Log
|
2021-03-06 19:45:13 +03:00
|
|
|
// Stop stops the log stream
|
2020-04-01 15:40:15 +02:00
|
|
|
Stop() error
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:57:30 +01:00
|
|
|
// Log is a log message
|
|
|
|
type Log struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Metadata holds metadata
|
2020-11-18 16:50:41 +03:00
|
|
|
Metadata metadata.Metadata
|
2021-03-06 19:45:13 +03:00
|
|
|
// Message holds the message
|
|
|
|
Message string
|
2019-09-13 21:33:14 -07:00
|
|
|
}
|
|
|
|
|
2020-01-16 13:34:04 +00:00
|
|
|
// Scheduler is a runtime service scheduler
|
|
|
|
type Scheduler interface {
|
|
|
|
// Notify publishes schedule events
|
2019-11-02 13:25:10 +00:00
|
|
|
Notify() (<-chan Event, error)
|
2020-01-16 13:34:04 +00:00
|
|
|
// Close stops the scheduler
|
2019-11-02 13:25:10 +00:00
|
|
|
Close() error
|
2019-09-13 21:33:14 -07:00
|
|
|
}
|
|
|
|
|
2020-01-16 13:34:04 +00:00
|
|
|
// EventType defines schedule event
|
2019-11-02 13:25:10 +00:00
|
|
|
type EventType int
|
2019-09-13 21:58:03 -07:00
|
|
|
|
2019-11-02 13:25:10 +00: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 12:29:21 +00:00
|
|
|
|
2019-11-02 13:25:10 +00: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 12:29:21 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 13:25:10 +00:00
|
|
|
// Event is notification event
|
|
|
|
type Event struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Timestamp of event
|
2019-11-02 13:25:10 +00:00
|
|
|
Timestamp time.Time
|
2020-05-19 10:14:07 +01:00
|
|
|
// Service the event relates to
|
|
|
|
Service *Service
|
|
|
|
// Options to use when processing the event
|
|
|
|
Options *CreateOptions
|
2021-03-06 19:45:13 +03:00
|
|
|
// ID of the event
|
|
|
|
ID string
|
|
|
|
// Type is event type
|
|
|
|
Type EventType
|
2019-09-13 21:33:14 -07:00
|
|
|
}
|
|
|
|
|
2019-11-02 13:25:10 +00:00
|
|
|
// Service is runtime service
|
|
|
|
type Service struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Metadata stores metadata
|
|
|
|
Metadata metadata.Metadata
|
2019-11-02 13:25:10 +00:00
|
|
|
// Name of the service
|
|
|
|
Name string
|
2019-11-23 22:50:13 +00:00
|
|
|
// Version of the service
|
|
|
|
Version string
|
2021-03-06 19:45:13 +03:00
|
|
|
// Name of the service
|
2019-11-02 13:25:10 +00:00
|
|
|
Source string
|
2019-09-13 21:33:14 -07:00
|
|
|
}
|
2020-08-14 11:47:28 +01:00
|
|
|
|
|
|
|
// Resources which are allocated to a serivce
|
|
|
|
type Resources struct {
|
|
|
|
// CPU is the maximum amount of CPU the service will be allocated (unit millicpu)
|
|
|
|
// e.g. 0.25CPU would be passed as 250
|
|
|
|
CPU int
|
|
|
|
// Mem is the maximum amount of memory the service will be allocated (unit mebibyte)
|
|
|
|
// e.g. 128 MiB of memory would be passed as 128
|
|
|
|
Mem int
|
|
|
|
// Disk is the maximum amount of disk space the service will be allocated (unit mebibyte)
|
|
|
|
// e.g. 128 MiB of memory would be passed as 128
|
|
|
|
Disk int
|
|
|
|
}
|