runtime.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package entities
  2. import (
  3. "time"
  4. )
  5. type (
  6. PluginRuntime struct {
  7. State PluginRuntimeState `json:"state"`
  8. Config PluginConfiguration `json:"config"`
  9. Connector PluginConnector `json:"-"`
  10. }
  11. PluginRuntimeInterface interface {
  12. PluginRuntimeTimeLifeInterface
  13. PluginRuntimeSessionIOInterface
  14. }
  15. PluginRuntimeTimeLifeInterface interface {
  16. InitEnvironment() error
  17. StartPlugin() error
  18. Stopped() bool
  19. Stop()
  20. Configuration() *PluginConfiguration
  21. RuntimeState() *PluginRuntimeState
  22. }
  23. PluginRuntimeSessionIOInterface interface {
  24. Listen(session_id string) *BytesIOListener
  25. Write(session_id string, data []byte)
  26. Request(session_id string, data []byte) ([]byte, error)
  27. }
  28. )
  29. func (r *PluginRuntime) Stopped() bool {
  30. return r.State.Status == PLUGIN_RUNTIME_STATUS_STOPPED
  31. }
  32. func (r *PluginRuntime) Stop() {
  33. r.State.Status = PLUGIN_RUNTIME_STATUS_STOPPED
  34. }
  35. func (r *PluginRuntime) Configuration() *PluginConfiguration {
  36. return &r.Config
  37. }
  38. func (r *PluginRuntime) RuntimeState() *PluginRuntimeState {
  39. return &r.State
  40. }
  41. type PluginRuntimeState struct {
  42. Restarts int `json:"restarts"`
  43. Status string `json:"status"`
  44. RelativePath string `json:"relative_path"`
  45. ActiveAt *time.Time `json:"active_at"`
  46. StoppedAt *time.Time `json:"stopped_at"`
  47. Verified bool `json:"verified"`
  48. }
  49. const (
  50. PLUGIN_RUNTIME_STATUS_ACTIVE = "active"
  51. PLUGIN_RUNTIME_STATUS_LAUNCHING = "launching"
  52. PLUGIN_RUNTIME_STATUS_STOPPED = "stopped"
  53. PLUGIN_RUNTIME_STATUS_RESTARTING = "restarting"
  54. PLUGIN_RUNTIME_STATUS_PENDING = "pending"
  55. )
  56. type PluginConnector interface {
  57. OnMessage(func([]byte))
  58. Read([]byte) int
  59. Write([]byte) int
  60. }