job.go 5.2 KB

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