event.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  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. package progress
  14. import (
  15. "context"
  16. "fmt"
  17. )
  18. // EventStatus indicates the status of an action
  19. type EventStatus int
  20. const (
  21. // Working means that the current task is working
  22. Working EventStatus = iota
  23. // Done means that the current task is done
  24. Done
  25. // Warning means that the current task has warning
  26. Warning
  27. // Error means that the current task has errored
  28. Error
  29. )
  30. const (
  31. StatusError = "Error"
  32. StatusCreating = "Creating"
  33. StatusStarting = "Starting"
  34. StatusStarted = "Started"
  35. StatusWaiting = "Waiting"
  36. StatusHealthy = "Healthy"
  37. StatusExited = "Exited"
  38. StatusRestarting = "Restarting"
  39. StatusRestarted = "Restarted"
  40. StatusRunning = "Running"
  41. StatusCreated = "Created"
  42. StatusStopping = "Stopping"
  43. StatusStopped = "Stopped"
  44. StatusKilling = "Killing"
  45. StatusKilled = "Killed"
  46. StatusRemoving = "Removing"
  47. StatusRemoved = "Removed"
  48. StatusBuilding = "Building"
  49. StatusBuilt = "Built"
  50. StatusPulling = "Pulling"
  51. StatusPulled = "Pulled"
  52. StatusCommitting = "Committing"
  53. StatusCommitted = "Committed"
  54. StatusCopying = "Copying"
  55. StatusCopied = "Copied"
  56. StatusExporting = "Exporting"
  57. StatusExported = "Exported"
  58. )
  59. // Event represents a progress event.
  60. type Event struct {
  61. ID string
  62. ParentID string
  63. Text string
  64. Details string
  65. Status EventStatus
  66. Current int64
  67. Percent int
  68. Total int64
  69. }
  70. func (e *Event) StatusText() string {
  71. switch e.Status {
  72. case Working:
  73. return "Working"
  74. case Warning:
  75. return "Warning"
  76. case Done:
  77. return "Done"
  78. default:
  79. return "Error"
  80. }
  81. }
  82. // ErrorEvent creates a new Error Event with message
  83. func ErrorEvent(id string, msg string) Event {
  84. return Event{
  85. ID: id,
  86. Status: Error,
  87. Text: StatusError,
  88. Details: msg,
  89. }
  90. }
  91. // ErrorEventf creates a new Error Event with format message
  92. func ErrorEventf(id string, msg string, args ...any) Event {
  93. return ErrorEvent(id, fmt.Sprintf(msg, args...))
  94. }
  95. // CreatingEvent creates a new Create in progress Event
  96. func CreatingEvent(id string) Event {
  97. return NewEvent(id, Working, StatusCreating)
  98. }
  99. // StartingEvent creates a new Starting in progress Event
  100. func StartingEvent(id string) Event {
  101. return NewEvent(id, Working, StatusStarting)
  102. }
  103. // StartedEvent creates a new Started in progress Event
  104. func StartedEvent(id string) Event {
  105. return NewEvent(id, Done, StatusStarted)
  106. }
  107. // Waiting creates a new waiting event
  108. func Waiting(id string) Event {
  109. return NewEvent(id, Working, StatusWaiting)
  110. }
  111. // Healthy creates a new healthy event
  112. func Healthy(id string) Event {
  113. return NewEvent(id, Done, StatusHealthy)
  114. }
  115. // Exited creates a new exited event
  116. func Exited(id string) Event {
  117. return NewEvent(id, Done, StatusExited)
  118. }
  119. // RestartingEvent creates a new Restarting in progress Event
  120. func RestartingEvent(id string) Event {
  121. return NewEvent(id, Working, StatusRestarting)
  122. }
  123. // RestartedEvent creates a new Restarted in progress Event
  124. func RestartedEvent(id string) Event {
  125. return NewEvent(id, Done, StatusRestarted)
  126. }
  127. // RunningEvent creates a new Running in progress Event
  128. func RunningEvent(id string) Event {
  129. return NewEvent(id, Done, StatusRunning)
  130. }
  131. // CreatedEvent creates a new Created (done) Event
  132. func CreatedEvent(id string) Event {
  133. return NewEvent(id, Done, StatusCreated)
  134. }
  135. // StoppingEvent creates a new Stopping in progress Event
  136. func StoppingEvent(id string) Event {
  137. return NewEvent(id, Working, StatusStopping)
  138. }
  139. // StoppedEvent creates a new Stopping in progress Event
  140. func StoppedEvent(id string) Event {
  141. return NewEvent(id, Done, StatusStopped)
  142. }
  143. // KillingEvent creates a new Killing in progress Event
  144. func KillingEvent(id string) Event {
  145. return NewEvent(id, Working, StatusKilling)
  146. }
  147. // KilledEvent creates a new Killed in progress Event
  148. func KilledEvent(id string) Event {
  149. return NewEvent(id, Done, StatusKilled)
  150. }
  151. // RemovingEvent creates a new Removing in progress Event
  152. func RemovingEvent(id string) Event {
  153. return NewEvent(id, Working, StatusRemoving)
  154. }
  155. // RemovedEvent creates a new removed (done) Event
  156. func RemovedEvent(id string) Event {
  157. return NewEvent(id, Done, StatusRemoved)
  158. }
  159. // BuildingEvent creates a new Building in progress Event
  160. func BuildingEvent(id string) Event {
  161. return NewEvent("Image "+id, Working, StatusBuilding)
  162. }
  163. // BuiltEvent creates a new built (done) Event
  164. func BuiltEvent(id string) Event {
  165. return NewEvent("Image "+id, Done, StatusBuilt)
  166. }
  167. // PullingEvent creates a new pulling (in progress) Event
  168. func PullingEvent(id string) Event {
  169. return NewEvent("Image "+id, Working, StatusPulling)
  170. }
  171. // PulledEvent creates a new pulled (done) Event
  172. func PulledEvent(id string) Event {
  173. return NewEvent("Image "+id, Done, StatusPulled)
  174. }
  175. // SkippedEvent creates a new Skipped Event
  176. func SkippedEvent(id string, reason string) Event {
  177. return Event{
  178. ID: id,
  179. Status: Warning,
  180. Text: "Skipped: " + reason,
  181. }
  182. }
  183. // NewEvent new event
  184. func NewEvent(id string, status EventStatus, text string) Event {
  185. return Event{
  186. ID: id,
  187. Status: status,
  188. Text: text,
  189. }
  190. }
  191. // EventProcessor is notified about Compose operations and tasks
  192. type EventProcessor interface {
  193. // Start is triggered as a Compose operation is starting with context
  194. Start(ctx context.Context, operation string)
  195. // On notify about (sub)task and progress processing operation
  196. On(events ...Event)
  197. // Done is triggered as a Compose operation completed
  198. Done(operation string, success bool)
  199. }