Brian O'Connor | 6a37ea9 | 2017-08-03 22:45:59 -0700 | [diff] [blame] | 1 | // Copyright 2016 Open Networking Foundation |
David K. Bainbridge | df9df63 | 2016-07-07 18:47:46 -0700 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 14 | package main |
| 15 | |
| 16 | import ( |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 17 | "os/exec" |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 18 | "time" |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | type WorkRequest struct { |
| 22 | Info *RequestInfo |
| 23 | Script string |
| 24 | Role string |
| 25 | } |
| 26 | |
| 27 | type Worker struct { |
| 28 | ID int |
| 29 | Work chan WorkRequest |
| 30 | StatusChan chan StatusMsg |
| 31 | WorkerQueue chan chan WorkRequest |
| 32 | QuitChan chan bool |
| 33 | } |
| 34 | |
| 35 | type StatusMsg struct { |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 36 | Request *WorkRequest `json:"request"` |
| 37 | Worker int `json:"worker"` |
| 38 | Status TaskStatus `json:"status"` |
| 39 | Message string `json:"message"` |
| 40 | Timestamp int64 `json:"timestamp"` |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | func NewWorker(id int, workerQueue chan chan WorkRequest, statusChan chan StatusMsg) Worker { |
| 44 | // Create, and return the worker. |
| 45 | worker := Worker{ |
| 46 | ID: id, |
| 47 | Work: make(chan WorkRequest), |
| 48 | StatusChan: statusChan, |
| 49 | WorkerQueue: workerQueue, |
David K. Bainbridge | 3850158 | 2016-06-01 18:15:45 -0700 | [diff] [blame] | 50 | QuitChan: make(chan bool), |
| 51 | } |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 52 | |
| 53 | return worker |
| 54 | } |
| 55 | |
| 56 | func (w *Worker) Start() { |
| 57 | go func() { |
| 58 | for { |
| 59 | // Add ourselves into the worker queue. |
| 60 | w.WorkerQueue <- w.Work |
| 61 | |
| 62 | select { |
| 63 | case work := <-w.Work: |
| 64 | // Receive a work request. |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 65 | w.StatusChan <- StatusMsg{&work, w.ID, Running, "", time.Now().Unix()} |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 66 | log.Debugf("RUN: %s %s %s %s %s %s", |
David K. Bainbridge | 3850158 | 2016-06-01 18:15:45 -0700 | [diff] [blame] | 67 | work.Script, work.Info.Id, work.Info.Name, |
| 68 | work.Info.Ip, work.Info.Mac, work.Role) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 69 | err := exec.Command(work.Script, work.Info.Id, work.Info.Name, |
| 70 | work.Info.Ip, work.Info.Mac, work.Role).Run() |
| 71 | if err != nil { |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 72 | w.StatusChan <- StatusMsg{&work, w.ID, Failed, err.Error(), |
| 73 | time.Now().Unix()} |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 74 | } else { |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 75 | w.StatusChan <- StatusMsg{&work, w.ID, Complete, "", |
| 76 | time.Now().Unix()} |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 77 | } |
| 78 | case <-w.QuitChan: |
| 79 | // We have been asked to stop. |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 80 | log.Infof("worker%d stopping\n", w.ID) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 81 | return |
| 82 | } |
| 83 | } |
| 84 | }() |
| 85 | } |
| 86 | |
| 87 | func (w *Worker) Stop() { |
| 88 | go func() { |
| 89 | w.QuitChan <- true |
| 90 | }() |
| 91 | } |
| 92 | |
| 93 | type Dispatcher struct { |
| 94 | Storage Storage |
| 95 | WorkQueue chan WorkRequest |
| 96 | WorkerQueue chan chan WorkRequest |
| 97 | StatusChan chan StatusMsg |
| 98 | QuitChan chan bool |
| 99 | NumWorkers int |
| 100 | } |
| 101 | |
| 102 | func NewDispatcher(numWorkers int, storage Storage) *Dispatcher { |
| 103 | d := Dispatcher{ |
| 104 | Storage: storage, |
| 105 | WorkQueue: make(chan WorkRequest, 100), |
| 106 | StatusChan: make(chan StatusMsg, 100), |
| 107 | NumWorkers: numWorkers, |
| 108 | WorkerQueue: make(chan chan WorkRequest, numWorkers), |
| 109 | QuitChan: make(chan bool), |
| 110 | } |
| 111 | |
| 112 | return &d |
| 113 | } |
| 114 | |
David K. Bainbridge | 3850158 | 2016-06-01 18:15:45 -0700 | [diff] [blame] | 115 | func (d *Dispatcher) Dispatch(info *RequestInfo, role string, script string) error { |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 116 | d.WorkQueue <- WorkRequest{ |
David K. Bainbridge | 3850158 | 2016-06-01 18:15:45 -0700 | [diff] [blame] | 117 | Info: info, |
| 118 | Script: script, |
| 119 | Role: role, |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 120 | } |
| 121 | return nil |
| 122 | } |
| 123 | |
| 124 | func (d *Dispatcher) Start() { |
| 125 | // Now, create all of our workers. |
| 126 | for i := 0; i < d.NumWorkers; i++ { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 127 | log.Infof("Creating worker %d", i) |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 128 | worker := NewWorker(i, d.WorkerQueue, d.StatusChan) |
| 129 | worker.Start() |
| 130 | } |
| 131 | |
| 132 | go func() { |
| 133 | for { |
| 134 | select { |
| 135 | case work := <-d.WorkQueue: |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 136 | log.Debugf("Received work requeust") |
David K. Bainbridge | 068e87d | 2016-06-30 13:53:19 -0700 | [diff] [blame] | 137 | d.StatusChan <- StatusMsg{&work, -1, Pending, "", time.Now().Unix()} |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 138 | go func() { |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 139 | worker := <-d.WorkerQueue |
| 140 | |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 141 | log.Debugf("Dispatching work request") |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 142 | worker <- work |
| 143 | }() |
| 144 | case update := <-d.StatusChan: |
David K. Bainbridge | 546cdc3 | 2016-06-29 15:30:22 -0700 | [diff] [blame] | 145 | err := d.Storage.Put(update.Request.Info.Id, update) |
| 146 | if err != nil { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 147 | log.Errorf("Unable to update storage with status for '%s' : %s", |
David K. Bainbridge | 546cdc3 | 2016-06-29 15:30:22 -0700 | [diff] [blame] | 148 | update.Request.Info.Id, err) |
| 149 | } else { |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 150 | log.Debugf("Storage updated for '%s'", update.Request.Info.Id) |
David K. Bainbridge | 546cdc3 | 2016-06-29 15:30:22 -0700 | [diff] [blame] | 151 | } |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 152 | case <-d.QuitChan: |
David K. Bainbridge | a9c2e0a | 2016-07-01 18:33:50 -0700 | [diff] [blame] | 153 | log.Infof("Stopping dispatcher") |
David K. Bainbridge | f0da873 | 2016-06-01 16:15:37 -0700 | [diff] [blame] | 154 | return |
| 155 | } |
| 156 | } |
| 157 | }() |
| 158 | } |
| 159 | |
| 160 | func (d *Dispatcher) Stop() { |
| 161 | go func() { |
| 162 | d.QuitChan <- true |
| 163 | }() |
| 164 | } |