event.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "context"
  15. // EventStatus indicates the status of an action
  16. type EventStatus int
  17. const (
  18. // Working means that the current task is working
  19. Working EventStatus = iota
  20. // Done means that the current task is done
  21. Done
  22. // Warning means that the current task has warning
  23. Warning
  24. // Error means that the current task has errored
  25. Error
  26. )
  27. const (
  28. StatusError = "Error"
  29. StatusCreating = "Creating"
  30. StatusStarting = "Starting"
  31. StatusStarted = "Started"
  32. StatusWaiting = "Waiting"
  33. StatusHealthy = "Healthy"
  34. StatusExited = "Exited"
  35. StatusRestarting = "Restarting"
  36. StatusRestarted = "Restarted"
  37. StatusRunning = "Running"
  38. StatusCreated = "Created"
  39. StatusStopping = "Stopping"
  40. StatusStopped = "Stopped"
  41. StatusKilling = "Killing"
  42. StatusKilled = "Killed"
  43. StatusRemoving = "Removing"
  44. StatusRemoved = "Removed"
  45. StatusBuilding = "Building"
  46. StatusBuilt = "Built"
  47. StatusPulling = "Pulling"
  48. StatusPulled = "Pulled"
  49. )
  50. // Event represents a progress event.
  51. type Event struct {
  52. ID string
  53. ParentID string
  54. Text string
  55. Status EventStatus
  56. StatusText string
  57. Current int64
  58. Percent int
  59. Total int64
  60. }
  61. // ErrorMessageEvent creates a new Error Event with message
  62. func ErrorMessageEvent(id string, msg string) Event {
  63. return NewEvent(id, Error, msg)
  64. }
  65. // ErrorEvent creates a new Error Event
  66. func ErrorEvent(id string) Event {
  67. return NewEvent(id, Error, StatusError)
  68. }
  69. // CreatingEvent creates a new Create in progress Event
  70. func CreatingEvent(id string) Event {
  71. return NewEvent(id, Working, StatusCreating)
  72. }
  73. // StartingEvent creates a new Starting in progress Event
  74. func StartingEvent(id string) Event {
  75. return NewEvent(id, Working, StatusStarting)
  76. }
  77. // StartedEvent creates a new Started in progress Event
  78. func StartedEvent(id string) Event {
  79. return NewEvent(id, Done, StatusStarted)
  80. }
  81. // Waiting creates a new waiting event
  82. func Waiting(id string) Event {
  83. return NewEvent(id, Working, StatusWaiting)
  84. }
  85. // Healthy creates a new healthy event
  86. func Healthy(id string) Event {
  87. return NewEvent(id, Done, StatusHealthy)
  88. }
  89. // Exited creates a new exited event
  90. func Exited(id string) Event {
  91. return NewEvent(id, Done, StatusExited)
  92. }
  93. // RestartingEvent creates a new Restarting in progress Event
  94. func RestartingEvent(id string) Event {
  95. return NewEvent(id, Working, StatusRestarting)
  96. }
  97. // RestartedEvent creates a new Restarted in progress Event
  98. func RestartedEvent(id string) Event {
  99. return NewEvent(id, Done, StatusRestarted)
  100. }
  101. // RunningEvent creates a new Running in progress Event
  102. func RunningEvent(id string) Event {
  103. return NewEvent(id, Done, StatusRunning)
  104. }
  105. // CreatedEvent creates a new Created (done) Event
  106. func CreatedEvent(id string) Event {
  107. return NewEvent(id, Done, StatusCreated)
  108. }
  109. // StoppingEvent creates a new Stopping in progress Event
  110. func StoppingEvent(id string) Event {
  111. return NewEvent(id, Working, StatusStopping)
  112. }
  113. // StoppedEvent creates a new Stopping in progress Event
  114. func StoppedEvent(id string) Event {
  115. return NewEvent(id, Done, StatusStopped)
  116. }
  117. // KillingEvent creates a new Killing in progress Event
  118. func KillingEvent(id string) Event {
  119. return NewEvent(id, Working, StatusKilling)
  120. }
  121. // KilledEvent creates a new Killed in progress Event
  122. func KilledEvent(id string) Event {
  123. return NewEvent(id, Done, StatusKilled)
  124. }
  125. // RemovingEvent creates a new Removing in progress Event
  126. func RemovingEvent(id string) Event {
  127. return NewEvent(id, Working, StatusRemoving)
  128. }
  129. // RemovedEvent creates a new removed (done) Event
  130. func RemovedEvent(id string) Event {
  131. return NewEvent(id, Done, StatusRemoved)
  132. }
  133. // BuildingEvent creates a new Building in progress Event
  134. func BuildingEvent(id string) Event {
  135. return NewEvent("Image "+id, Working, StatusBuilding)
  136. }
  137. // BuiltEvent creates a new built (done) Event
  138. func BuiltEvent(id string) Event {
  139. return NewEvent("Image "+id, Done, StatusBuilt)
  140. }
  141. // PullingEvent creates a new pulling (in progress) Event
  142. func PullingEvent(id string) Event {
  143. return NewEvent("Image "+id, Working, StatusPulling)
  144. }
  145. // PulledEvent creates a new pulled (done) Event
  146. func PulledEvent(id string) Event {
  147. return NewEvent("Image "+id, Done, StatusPulled)
  148. }
  149. // SkippedEvent creates a new Skipped Event
  150. func SkippedEvent(id string, reason string) Event {
  151. return Event{
  152. ID: id,
  153. Status: Warning,
  154. StatusText: "Skipped: " + reason,
  155. }
  156. }
  157. // NewEvent new event
  158. func NewEvent(id string, status EventStatus, statusText string) Event {
  159. return Event{
  160. ID: id,
  161. Status: status,
  162. StatusText: statusText,
  163. }
  164. }
  165. // EventProcessor is notified about Compose operations and tasks
  166. type EventProcessor interface {
  167. // Start is triggered as a Compose operation is starting with context
  168. Start(ctx context.Context, operation string)
  169. // On notify about (sub)task and progress processing operation
  170. On(events ...Event)
  171. // Done is triggered as a Compose operation completed
  172. Done(operation string, success bool)
  173. }