github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/runtime/runtime.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); 2 // you may not use this file except in compliance with the License. 3 // You may obtain a copy of the License at 4 // 5 // https://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, 9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 // See the License for the specific language governing permissions and 11 // limitations under the License. 12 // 13 // Original source: github.com/micro/go-micro/v3/runtime/runtime.go 14 15 // Package runtime is a service runtime manager 16 package runtime 17 18 import ( 19 "errors" 20 "time" 21 ) 22 23 var ( 24 // DefaultRuntime implementation 25 DefaultRuntime Runtime 26 27 ErrAlreadyExists = errors.New("already exists") 28 ErrInvalidResource = errors.New("invalid resource") 29 ErrNotFound = errors.New("not found") 30 ) 31 32 // Runtime is a service runtime manager 33 type Runtime interface { 34 // Init initializes runtime 35 Init(...Option) error 36 // Create a resource 37 Create(Resource, ...CreateOption) error 38 // Read a resource 39 Read(...ReadOption) ([]*Service, error) 40 // Update a resource 41 Update(Resource, ...UpdateOption) error 42 // Delete a resource 43 Delete(Resource, ...DeleteOption) error 44 // Logs returns the logs for a resource 45 Logs(Resource, ...LogsOption) (LogStream, error) 46 // Start starts the runtime 47 Start() error 48 // Stop shuts down the runtime 49 Stop() error 50 // String defines the runtime implementation 51 String() string 52 } 53 54 // LogStream returns a log stream 55 type LogStream interface { 56 Error() error 57 Chan() chan Log 58 Stop() error 59 } 60 61 // Log is a log message 62 type Log struct { 63 Message string 64 Metadata map[string]string 65 } 66 67 // EventType defines schedule event 68 type EventType int 69 70 const ( 71 // CreateEvent is emitted when a new build has been craeted 72 CreateEvent EventType = iota 73 // UpdateEvent is emitted when a new update become available 74 UpdateEvent 75 // DeleteEvent is emitted when a build has been deleted 76 DeleteEvent 77 ) 78 79 // String returns human readable event type 80 func (t EventType) String() string { 81 switch t { 82 case CreateEvent: 83 return "create" 84 case DeleteEvent: 85 return "delete" 86 case UpdateEvent: 87 return "update" 88 default: 89 return "unknown" 90 } 91 } 92 93 // Event is notification event 94 type Event struct { 95 // ID of the event 96 ID string 97 // Type is event type 98 Type EventType 99 // Timestamp is event timestamp 100 Timestamp time.Time 101 // Service the event relates to 102 Service *Service 103 // Options to use when processing the event 104 Options *CreateOptions 105 } 106 107 // ServiceStatus defines service statuses 108 type ServiceStatus int 109 110 const ( 111 // Unknown indicates the status of the service is not known 112 Unknown ServiceStatus = iota 113 // Pending is the initial status of a service 114 Pending 115 // Building is the status when the service is being built 116 Building 117 // Starting is the status when the service has been started but is not yet ready to accept traffic 118 Starting 119 // Running is the status when the service is active and accepting traffic 120 Running 121 // Stopping is the status when a service is stopping 122 Stopping 123 // Stopped is the status when a service has been stopped or has completed 124 Stopped 125 // Error is the status when an error occured, this could be a build error or a run error. The error 126 // details can be found within the service's metadata 127 Error 128 ) 129 130 // String returns human-readable service status 131 func (s ServiceStatus) String() string { 132 switch s { 133 case Unknown: 134 return "unknown" 135 case Pending: 136 return "pending" 137 case Building: 138 return "building" 139 case Starting: 140 return "starting" 141 case Running: 142 return "running" 143 case Stopping: 144 return "stopping" 145 case Stopped: 146 return "stopped" 147 case Error: 148 return "error" 149 default: 150 return "undefined" 151 } 152 } 153 154 // Resources which are allocated to a service 155 type Resources struct { 156 // CPU is the maximum amount of CPU the service will be allocated (unit millicpu) 157 // e.g. 0.25CPU would be passed as 250 158 CPU int 159 // Mem is the maximum amount of memory the service will be allocated (unit mebibyte) 160 // e.g. 128 MiB of memory would be passed as 128 161 Mem int 162 // Disk is the maximum amount of disk space the service will be allocated (unit mebibyte) 163 // e.g. 128 MiB of memory would be passed as 128 164 Disk int 165 } 166 167 // Create a resource 168 func Create(resource Resource, opts ...CreateOption) error { 169 return DefaultRuntime.Create(resource, opts...) 170 } 171 172 // Read returns the service 173 func Read(opts ...ReadOption) ([]*Service, error) { 174 return DefaultRuntime.Read(opts...) 175 } 176 177 // Update the resource in place 178 func Update(resource Resource, opts ...UpdateOption) error { 179 return DefaultRuntime.Update(resource, opts...) 180 } 181 182 // Delete a resource 183 func Delete(resource Resource, opts ...DeleteOption) error { 184 return DefaultRuntime.Delete(resource, opts...) 185 } 186 187 // Logs for a resource 188 func Logs(resource Resource, opts ...LogsOption) (LogStream, error) { 189 return DefaultRuntime.Logs(resource, opts...) 190 }