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