Documentation
¶
Index ¶
- Variables
- type Environment
- func (e *Environment) Attach(ctx context.Context) error
- func (e *Environment) Config() *environment.Configuration
- func (e *Environment) ContainerInspect(ctx context.Context) (types.ContainerJSON, error)
- func (e *Environment) Create() error
- func (e *Environment) Destroy() error
- func (e *Environment) Events() *events.Bus
- func (e *Environment) Exists() (bool, error)
- func (e *Environment) ExitState() (uint32, bool, error)
- func (e *Environment) InSituUpdate() error
- func (e *Environment) IsAttached() bool
- func (e *Environment) IsRunning(ctx context.Context) (bool, error)
- func (e *Environment) OnBeforeStart(ctx context.Context) error
- func (e *Environment) Readlog(lines int) ([]string, error)
- func (e *Environment) SendCommand(c string) error
- func (e *Environment) SetImage(i string)
- func (e *Environment) SetLogCallback(f func([]byte))
- func (e *Environment) SetState(state string)
- func (e *Environment) SetStopConfiguration(c remote.ProcessStopConfiguration)
- func (e *Environment) SetStream(s *types.HijackedResponse)
- func (e *Environment) SignalContainer(ctx context.Context, signal string) error
- func (e *Environment) Start(ctx context.Context) error
- func (e *Environment) State() string
- func (e *Environment) Stop(ctx context.Context) error
- func (e *Environment) Terminate(ctx context.Context, signal string) error
- func (e *Environment) Type() string
- func (e *Environment) Uptime(ctx context.Context) (int64, error)
- func (e *Environment) WaitForStop(ctx context.Context, duration time.Duration, terminate bool) error
- type Metadata
Constants ¶
This section is empty.
Variables ¶
var ErrNotAttached = errors.Sentinel("尚未連接至實例")
Functions ¶
This section is empty.
Types ¶
type Environment ¶
type Environment struct {
// 此環境的公開識別碼。在此情況下,它是 Docker 容器名稱,將用於
// 在此環境下建立的所有實例。
Id string
// 環境設定。
Configuration *environment.Configuration
// contains filtered or unexported fields
}
func New ¶
func New(id string, m *Metadata, c *environment.Configuration) (*Environment, error)
New 建立一個新的基礎 Docker 環境。傳入的 ID 將用作日後參照此容器的 ID。此值應該是每個伺服器唯一的(我們預設使用 UUID)。此時容器不需要 已經存在。
func (*Environment) Attach ¶
func (e *Environment) Attach(ctx context.Context) error
Attach 附加到 docker 容器本身,確保我們能夠將資料透過管線傳入及 傳出程序串流。此函式應該在容器啟動之前呼叫,但要在確認容器已 存在之後。
呼叫此函式會在背景中持續輪詢容器的資源,直到容器停止為止。傳入 此函式的 context 是用於附加容器,而函式內部會建立第二個 context 來管理輪詢作業。
func (*Environment) Config ¶
func (e *Environment) Config() *environment.Configuration
Config 回傳環境設定,讓程序可以即時修改此環境。
func (*Environment) ContainerInspect ¶
func (e *Environment) ContainerInspect(ctx context.Context) (types.ContainerJSON, error)
ContainerInspect 大致相當於 Docker 的 client.ContainerInspect(), 但改寫為使用效能更佳的 JSON 解碼器。這點很重要,因為 Wings 會對此 端點發出大量請求,而標準的「encoding/json」即使在只執行單一容器時, 效能問題也相當明顯。
func (*Environment) Create ¶
func (e *Environment) Create() error
Create 使用目前所有可取得的資料,為伺服器建立一個新的容器。 若容器已經存在,則會直接回傳該容器。
func (*Environment) Destroy ¶
func (e *Environment) Destroy() error
Destroy 會移除伺服器的 Docker 容器。若容器目前正在執行,Docker 會 將其強制停止。
func (*Environment) Events ¶
func (e *Environment) Events() *events.Bus
Events 回傳此環境的事件匯流排(event bus)。
func (*Environment) Exists ¶
func (e *Environment) Exists() (bool, error)
Exists 判斷此容器是否存在於此環境中。傳入的 ID 應該是伺服器的 UUID,因為容器是以伺服器 UUID 作為名稱來建立的,而除了容器建立時 自動指派的較長 ID 之外,docker 使用容器名稱作為查詢參數也能正常 運作。
func (*Environment) ExitState ¶
func (e *Environment) ExitState() (uint32, bool, error)
ExitState 回傳容器的結束狀態、結束代碼,以及此容器是否被 OOM killer 終止。
func (*Environment) InSituUpdate ¶
func (e *Environment) InSituUpdate() error
InSituUpdate 針對 Docker 容器的資源限制執行原地更新,而不會實際變更 容器的運作狀態。這讓記憶體、cpu 以及 IO 限制能夠針對個別實例即時調整。
func (*Environment) IsAttached ¶
func (e *Environment) IsAttached() bool
IsAttached 藉由檢查串流是否為 nil,判斷此程序目前是否已附加到 容器實例。
func (*Environment) IsRunning ¶
func (e *Environment) IsRunning(ctx context.Context) (bool, error)
IsRunning 判斷伺服器的 docker 容器目前是否正在執行。若容器不存在, 則會引發錯誤(因為在正確開發的情況下,這種情況應該永遠不會發生)。
你可以使用 Docker API 中的 client.IsErrNotFound 來確認是否是因為 找不到此實例。
@see docker/client/errors.go
func (*Environment) OnBeforeStart ¶
func (e *Environment) OnBeforeStart(ctx context.Context) error
OnBeforeStart 會在容器啟動前執行,並從 Panel 取得程序設定。這一點 很重要,因為我們會用它來檢查設定檔,並確保伺服器程序永遠能取得 最新版本的 egg。
此程序也會確認伺服器環境存在且處於可啟動的狀態。這能確保在 Wings 執行期間,即使容器被意外刪除,也不會導致伺服器無法啟動。
func (*Environment) Readlog ¶
func (e *Environment) Readlog(lines int) ([]string, error)
Readlog 讀取伺服器的日誌檔。此函式不在乎伺服器是否正在執行,只會 嘗試讀取檔案最後的 X 個位元組並回傳。
func (*Environment) SendCommand ¶
func (e *Environment) SendCommand(c string) error
SendCommand 將指定的指令傳送到正在執行的容器實例的 stdin。此函式 不會確認資料是否成功送達,只確保資料已被推送進 stdin。
func (*Environment) SetImage ¶
func (e *Environment) SetImage(i string)
func (*Environment) SetLogCallback ¶
func (e *Environment) SetLogCallback(f func([]byte))
func (*Environment) SetState ¶
func (e *Environment) SetState(state string)
SetState 設定此環境的狀態。這會發出一個事件,讓伺服器能夠掛鉤此 事件,依照環境狀態採取自己的動作並追蹤自己的狀態。
func (*Environment) SetStopConfiguration ¶
func (e *Environment) SetStopConfiguration(c remote.ProcessStopConfiguration)
SetStopConfiguration 設定此環境的停止設定。
func (*Environment) SetStream ¶
func (e *Environment) SetStream(s *types.HijackedResponse)
SetStream 設定來自 Docker 客戶端目前的串流值。若提供的值為 nil, 我們會假設此串流已不再運作,且此實例實際上已離線。
func (*Environment) SignalContainer ¶
func (e *Environment) SignalContainer(ctx context.Context, signal string) error
SignalContainer 向容器傳送指定的訊號,以嘗試將其停止。
func (*Environment) Start ¶
func (e *Environment) Start(ctx context.Context) error
Start 會啟動伺服器環境,並開始將輸出透過管線傳送給主控台的事件 監聽器。若容器不存在,或需要重建,這會在呼叫 OnBeforeStart() 時 發生。
func (*Environment) State ¶
func (e *Environment) State() string
func (*Environment) Stop ¶
func (e *Environment) Stop(ctx context.Context) error
Stop 停止伺服器所在的容器。若我們嘗試在不向實例傳送指令的情況下 將其停止,最多會等待 30 秒後才強制終止容器。
你很可能會想使用 WaitForStop() 而不是這個函式,因為此函式只要指令 一送出就會回傳,而不會等待程序完全停止。
func (*Environment) Terminate ¶
func (e *Environment) Terminate(ctx context.Context, signal string) error
Terminate 使用提供的訊號強制終止容器,然後將其狀態設為已停止。
func (*Environment) Type ¶
func (e *Environment) Type() string
func (*Environment) Uptime ¶
func (e *Environment) Uptime(ctx context.Context) (int64, error)
Uptime 回傳容器目前的正常執行時間,單位為毫秒。若容器目前並未 執行,則回傳 0。
func (*Environment) WaitForStop ¶
func (e *Environment) WaitForStop(ctx context.Context, duration time.Duration, terminate bool) error
WaitForStop 嘗試使用定義好的停止指令,優雅地停止伺服器。若經過 指定秒數後伺服器仍未停止,則會依照第二個引數的值,回傳錯誤或 將實例強制終止。
此函式中對 Environment.Terminate() 的呼叫,使用的是傳入的 context, 因為我們不希望僅因為 context.WithTimeout() 已過期,就阻止伺服器 實例被終止。
type Metadata ¶
type Metadata struct {
Image string
Stop remote.ProcessStopConfiguration
}