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