David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 4 | "os/exec" |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 5 | "time" |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 6 | ) |
| 7 | |
| 8 | type WorkRequest struct { |
| 9 | Info *RequestInfo |
| 10 | Script string |
| 11 | Role string |
| 12 | } |
| 13 | |
| 14 | type Worker struct { |
| 15 | ID int |
| 16 | Work chan WorkRequest |
| 17 | StatusChan chan StatusMsg |
| 18 | WorkerQueue chan chan WorkRequest |
| 19 | QuitChan chan bool |
| 20 | } |
| 21 | |
| 22 | type StatusMsg struct { |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 23 | Request *WorkRequest `json:"request"` |
| 24 | Worker int `json:"worker"` |
| 25 | Status TaskStatus `json:"status"` |
| 26 | Message string `json:"message"` |
| 27 | Timestamp int64 `json:"timestamp"` |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | func NewWorker(id int, workerQueue chan chan WorkRequest, statusChan chan StatusMsg) Worker { |
| 31 | // Create, and return the worker. |
| 32 | worker := Worker{ |
| 33 | ID: id, |
| 34 | Work: make(chan WorkRequest), |
| 35 | StatusChan: statusChan, |
| 36 | WorkerQueue: workerQueue, |
David K. Bainbridge | 3850158 | 2016-06-01 18:15:45 -0700 | [diff] [blame] | 37 | QuitChan: make(chan bool), |
| 38 | } |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 39 | |
| 40 | return worker |
| 41 | } |
| 42 | |
| 43 | func (w *Worker) Start() { |
| 44 | go func() { |
| 45 | for { |
| 46 | // Add ourselves into the worker queue. |
| 47 | w.WorkerQueue <- w.Work |
| 48 | |
| 49 | select { |
| 50 | case work := <-w.Work: |
| 51 | // Receive a work request. |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 52 | w.StatusChan <- StatusMsg{&work, w.ID, Running, "", time.Now().Unix()} |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 53 | log.Debugf("RUN: %s %s %s %s %s %s", |
David K. Bainbridge | 3850158 | 2016-06-01 18:15:45 -0700 | [diff] [blame] | 54 | work.Script, work.Info.Id, work.Info.Name, |
| 55 | work.Info.Ip, work.Info.Mac, work.Role) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 56 | err := exec.Command(work.Script, work.Info.Id, work.Info.Name, |
| 57 | work.Info.Ip, work.Info.Mac, work.Role).Run() |
| 58 | if err != nil { |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 59 | w.StatusChan <- StatusMsg{&work, w.ID, Failed, err.Error(), |
| 60 | time.Now().Unix()} |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 61 | } else { |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 62 | w.StatusChan <- StatusMsg{&work, w.ID, Complete, "", |
| 63 | time.Now().Unix()} |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 64 | } |
| 65 | case <-w.QuitChan: |
| 66 | // We have been asked to stop. |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 67 | log.Infof("worker%d stopping\n", w.ID) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 68 | return |
| 69 | } |
| 70 | } |
| 71 | }() |
| 72 | } |
| 73 | |
| 74 | func (w *Worker) Stop() { |
| 75 | go func() { |
| 76 | w.QuitChan <- true |
| 77 | }() |
| 78 | } |
| 79 | |
| 80 | type Dispatcher struct { |
| 81 | Storage Storage |
| 82 | WorkQueue chan WorkRequest |
| 83 | WorkerQueue chan chan WorkRequest |
| 84 | StatusChan chan StatusMsg |
| 85 | QuitChan chan bool |
| 86 | NumWorkers int |
| 87 | } |
| 88 | |
| 89 | func NewDispatcher(numWorkers int, storage Storage) *Dispatcher { |
| 90 | d := Dispatcher{ |
| 91 | Storage: storage, |
| 92 | WorkQueue: make(chan WorkRequest, 100), |
| 93 | StatusChan: make(chan StatusMsg, 100), |
| 94 | NumWorkers: numWorkers, |
| 95 | WorkerQueue: make(chan chan WorkRequest, numWorkers), |
| 96 | QuitChan: make(chan bool), |
| 97 | } |
| 98 | |
| 99 | return &d |
| 100 | } |
| 101 | |
David K. Bainbridge | 3850158 | 2016-06-01 18:15:45 -0700 | [diff] [blame] | 102 | func (d *Dispatcher) Dispatch(info *RequestInfo, role string, script string) error { |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 103 | d.WorkQueue <- WorkRequest{ |
David K. Bainbridge | 3850158 | 2016-06-01 18:15:45 -0700 | [diff] [blame] | 104 | Info: info, |
| 105 | Script: script, |
| 106 | Role: role, |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 107 | } |
| 108 | return nil |
| 109 | } |
| 110 | |
| 111 | func (d *Dispatcher) Start() { |
| 112 | // Now, create all of our workers. |
| 113 | for i := 0; i < d.NumWorkers; i++ { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 114 | log.Infof("Creating worker %d", i) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 115 | worker := NewWorker(i, d.WorkerQueue, d.StatusChan) |
| 116 | worker.Start() |
| 117 | } |
| 118 | |
| 119 | go func() { |
| 120 | for { |
| 121 | select { |
| 122 | case work := <-d.WorkQueue: |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 123 | log.Debugf("Received work requeust") |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 124 | d.StatusChan <- StatusMsg{&work, -1, Pending, "", time.Now().Unix()} |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 125 | go func() { |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 126 | worker := <-d.WorkerQueue |
| 127 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 128 | log.Debugf("Dispatching work request") |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 129 | worker <- work |
| 130 | }() |
| 131 | case update := <-d.StatusChan: |
David K. Bainbridge | 546cdc3 | 2016-06-29 15:30:22 -0700 | [diff] [blame] | 132 | err := d.Storage.Put(update.Request.Info.Id, update) |
| 133 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 134 | log.Errorf("Unable to update storage with status for '%s' : %s", |
David K. Bainbridge | 546cdc3 | 2016-06-29 15:30:22 -0700 | [diff] [blame] | 135 | update.Request.Info.Id, err) |
| 136 | } else { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 137 | log.Debugf("Storage updated for '%s'", update.Request.Info.Id) |
David K. Bainbridge | 546cdc3 | 2016-06-29 15:30:22 -0700 | [diff] [blame] | 138 | } |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 139 | case <-d.QuitChan: |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 140 | log.Infof("Stopping dispatcher") |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 141 | return |
| 142 | } |
| 143 | } |
| 144 | }() |
| 145 | } |
| 146 | |
| 147 | func (d *Dispatcher) Stop() { |
| 148 | go func() { |
| 149 | d.QuitChan <- true |
| 150 | }() |
| 151 | } |