event.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 api
  14. import (
  15. "context"
  16. )
  17. // EventStatus indicates the status of an action
  18. type EventStatus int
  19. const (
  20. // Working means that the current task is working
  21. Working EventStatus = iota
  22. // Done means that the current task is done
  23. Done
  24. // Warning means that the current task has warning
  25. Warning
  26. // Error means that the current task has errored
  27. Error
  28. )
  29. // ResourceCompose is a special resource ID used when event applies to all resources in the application
  30. const ResourceCompose = "Compose"
  31. const (
  32. StatusError = "Error"
  33. StatusCreating = "Creating"
  34. StatusStarting = "Starting"
  35. StatusStarted = "Started"
  36. StatusWaiting = "Waiting"
  37. StatusHealthy = "Healthy"
  38. StatusExited = "Exited"
  39. StatusRestarting = "Restarting"
  40. StatusRestarted = "Restarted"
  41. StatusRunning = "Running"
  42. StatusCreated = "Created"
  43. StatusStopping = "Stopping"
  44. StatusStopped = "Stopped"
  45. StatusKilling = "Killing"
  46. StatusKilled = "Killed"
  47. StatusRemoving = "Removing"
  48. StatusRemoved = "Removed"
  49. StatusBuilding = "Building"
  50. StatusBuilt = "Built"
  51. StatusPulling = "Pulling"
  52. StatusPulled = "Pulled"
  53. StatusCommitting = "Committing"
  54. StatusCommitted = "Committed"
  55. StatusCopying = "Copying"
  56. StatusCopied = "Copied"
  57. StatusExporting = "Exporting"
  58. StatusExported = "Exported"
  59. )
  60. // Resource represents status change and progress for a compose resource.
  61. type Resource struct {
  62. ID string
  63. ParentID string
  64. Text string
  65. Details string
  66. Status EventStatus
  67. Current int64
  68. Percent int
  69. Total int64
  70. }
  71. func (e *Resource) StatusText() string {
  72. switch e.Status {
  73. case Working:
  74. return "Working"
  75. case Warning:
  76. return "Warning"
  77. case Done:
  78. return "Done"
  79. default:
  80. return "Error"
  81. }
  82. }
  83. // EventProcessor is notified about Compose operations and tasks
  84. type EventProcessor interface {
  85. // Start is triggered as a Compose operation is starting with context
  86. Start(ctx context.Context, operation string)
  87. // On notify about (sub)task and progress processing operation
  88. On(events ...Resource)
  89. // Done is triggered as a Compose operation completed
  90. Done(operation string, success bool)
  91. }