job.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package engine
  2. import (
  3. "fmt"
  4. "io"
  5. "strings"
  6. "time"
  7. )
  8. // A job is the fundamental unit of work in the docker engine.
  9. // Everything docker can do should eventually be exposed as a job.
  10. // For example: execute a process in a container, create a new container,
  11. // download an archive from the internet, serve the http api, etc.
  12. //
  13. // The job API is designed after unix processes: a job has a name, arguments,
  14. // environment variables, standard streams for input, output and error, and
  15. // an exit status which can indicate success (0) or error (anything else).
  16. //
  17. // One slight variation is that jobs report their status as a string. The
  18. // string "0" indicates success, and any other strings indicates an error.
  19. // This allows for richer error reporting.
  20. //
  21. type Job struct {
  22. Eng *Engine
  23. Name string
  24. Args []string
  25. env *Env
  26. Stdout *Output
  27. Stderr *Output
  28. Stdin *Input
  29. handler Handler
  30. status Status
  31. end time.Time
  32. onExit []func()
  33. }
  34. type Status int
  35. const (
  36. StatusOK Status = 0
  37. StatusErr Status = 1
  38. StatusNotFound Status = 127
  39. )
  40. // Run executes the job and blocks until the job completes.
  41. // If the job returns a failure status, an error is returned
  42. // which includes the status.
  43. func (job *Job) Run() error {
  44. // FIXME: make this thread-safe
  45. // FIXME: implement wait
  46. if !job.end.IsZero() {
  47. return fmt.Errorf("%s: job has already completed", job.Name)
  48. }
  49. // Log beginning and end of the job
  50. job.Eng.Logf("+job %s", job.CallString())
  51. defer func() {
  52. job.Eng.Logf("-job %s%s", job.CallString(), job.StatusString())
  53. }()
  54. var errorMessage string
  55. job.Stderr.AddString(&errorMessage)
  56. if job.handler == nil {
  57. job.Errorf("%s: command not found", job.Name)
  58. job.status = 127
  59. } else {
  60. job.status = job.handler(job)
  61. job.end = time.Now()
  62. }
  63. // Wait for all background tasks to complete
  64. if err := job.Stdout.Close(); err != nil {
  65. return err
  66. }
  67. if err := job.Stderr.Close(); err != nil {
  68. return err
  69. }
  70. if job.status != 0 {
  71. return fmt.Errorf("%s: %s", job.Name, errorMessage)
  72. }
  73. return nil
  74. }
  75. func (job *Job) CallString() string {
  76. return fmt.Sprintf("%s(%s)", job.Name, strings.Join(job.Args, ", "))
  77. }
  78. func (job *Job) StatusString() string {
  79. // If the job hasn't completed, status string is empty
  80. if job.end.IsZero() {
  81. return ""
  82. }
  83. var okerr string
  84. if job.status == StatusOK {
  85. okerr = "OK"
  86. } else {
  87. okerr = "ERR"
  88. }
  89. return fmt.Sprintf(" = %s (%d)", okerr, job.status)
  90. }
  91. // String returns a human-readable description of `job`
  92. func (job *Job) String() string {
  93. return fmt.Sprintf("%s.%s%s", job.Eng, job.CallString(), job.StatusString())
  94. }
  95. func (job *Job) Getenv(key string) (value string) {
  96. return job.env.Get(key)
  97. }
  98. func (job *Job) GetenvBool(key string) (value bool) {
  99. return job.env.GetBool(key)
  100. }
  101. func (job *Job) SetenvBool(key string, value bool) {
  102. job.env.SetBool(key, value)
  103. }
  104. func (job *Job) GetenvInt64(key string) int64 {
  105. return job.env.GetInt64(key)
  106. }
  107. func (job *Job) GetenvInt(key string) int {
  108. return job.env.GetInt(key)
  109. }
  110. func (job *Job) SetenvInt64(key string, value int64) {
  111. job.env.SetInt64(key, value)
  112. }
  113. func (job *Job) SetenvInt(key string, value int) {
  114. job.env.SetInt(key, value)
  115. }
  116. // Returns nil if key not found
  117. func (job *Job) GetenvList(key string) []string {
  118. return job.env.GetList(key)
  119. }
  120. func (job *Job) GetenvJson(key string, iface interface{}) error {
  121. return job.env.GetJson(key, iface)
  122. }
  123. func (job *Job) SetenvJson(key string, value interface{}) error {
  124. return job.env.SetJson(key, value)
  125. }
  126. func (job *Job) SetenvList(key string, value []string) error {
  127. return job.env.SetJson(key, value)
  128. }
  129. func (job *Job) Setenv(key, value string) {
  130. job.env.Set(key, value)
  131. }
  132. // DecodeEnv decodes `src` as a json dictionary, and adds
  133. // each decoded key-value pair to the environment.
  134. //
  135. // If `src` cannot be decoded as a json dictionary, an error
  136. // is returned.
  137. func (job *Job) DecodeEnv(src io.Reader) error {
  138. return job.env.Decode(src)
  139. }
  140. func (job *Job) EncodeEnv(dst io.Writer) error {
  141. return job.env.Encode(dst)
  142. }
  143. func (job *Job) ExportEnv(dst interface{}) (err error) {
  144. return job.env.Export(dst)
  145. }
  146. func (job *Job) ImportEnv(src interface{}) (err error) {
  147. return job.env.Import(src)
  148. }
  149. func (job *Job) Environ() map[string]string {
  150. return job.env.Map()
  151. }
  152. func (job *Job) Logf(format string, args ...interface{}) (n int, err error) {
  153. prefixedFormat := fmt.Sprintf("[%s] %s\n", job, strings.TrimRight(format, "\n"))
  154. return fmt.Fprintf(job.Stderr, prefixedFormat, args...)
  155. }
  156. func (job *Job) Printf(format string, args ...interface{}) (n int, err error) {
  157. return fmt.Fprintf(job.Stdout, format, args...)
  158. }
  159. func (job *Job) Errorf(format string, args ...interface{}) (n int, err error) {
  160. return fmt.Fprintf(job.Stderr, format, args...)
  161. }
  162. func (job *Job) Error(err error) (int, error) {
  163. return fmt.Fprintf(job.Stderr, "%s", err)
  164. }