event.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. StatusDownloading = "Downloading"
  60. StatusDownloadComplete = "Download complete"
  61. StatusConfiguring = "Configuring"
  62. StatusConfigured = "Configured"
  63. )
  64. // Resource represents status change and progress for a compose resource.
  65. type Resource struct {
  66. ID string
  67. ParentID string
  68. Text string
  69. Details string
  70. Status EventStatus
  71. Current int64
  72. Percent int
  73. Total int64
  74. }
  75. func (e *Resource) StatusText() string {
  76. switch e.Status {
  77. case Working:
  78. return "Working"
  79. case Warning:
  80. return "Warning"
  81. case Done:
  82. return "Done"
  83. default:
  84. return "Error"
  85. }
  86. }
  87. // EventProcessor is notified about Compose operations and tasks
  88. type EventProcessor interface {
  89. // Start is triggered as a Compose operation is starting with context
  90. Start(ctx context.Context, operation string)
  91. // On notify about (sub)task and progress processing operation
  92. On(events ...Resource)
  93. // Done is triggered as a Compose operation completed
  94. Done(operation string, success bool)
  95. }