Serge Bazanski | 3d116b2 | 2021-03-27 15:43:18 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
Serge Bazanski | 3d116b2 | 2021-03-27 15:43:18 +0000 | [diff] [blame] | 5 | "flag" |
Serge Bazanski | 3d116b2 | 2021-03-27 15:43:18 +0000 | [diff] [blame] | 6 | "net/http" |
| 7 | "sync" |
| 8 | "time" |
| 9 | |
| 10 | "code.hackerspace.pl/hscloud/go/mirko" |
| 11 | "github.com/golang/glog" |
| 12 | "github.com/grpc-ecosystem/grpc-gateway/runtime" |
| 13 | |
| 14 | pb "code.hackerspace.pl/hscloud/personal/q3k/shipstuck/proto" |
| 15 | ) |
| 16 | |
| 17 | type vessel struct { |
Serge Bazanski | 5c1ab3c | 2021-03-27 15:57:16 +0000 | [diff] [blame] | 18 | Speed float64 `json:"ss"` |
Serge Bazanski | 3d116b2 | 2021-03-27 15:43:18 +0000 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | // get retrieves the current status of the ship - returns true if stack, false |
| 22 | // otherwise. |
Serge Bazanski | a4ae66b | 2021-03-29 13:26:37 +0000 | [diff] [blame] | 23 | func get(ctx context.Context) (shipState, error) { |
Serge Bazanski | 3df9d0c | 2021-03-29 14:23:37 +0000 | [diff] [blame] | 24 | // 2021/03/29/ 17:23 UTC+2 it's been freed! |
| 25 | return shipStateFreed, nil |
Serge Bazanski | 3d116b2 | 2021-03-27 15:43:18 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | type shipState string |
| 29 | |
| 30 | const ( |
| 31 | shipStateUnknown shipState = "UNKNOWN" |
| 32 | shipStateStuck shipState = "STUCK" |
| 33 | shipStateFreed shipState = "FREED" |
Serge Bazanski | a4ae66b | 2021-03-29 13:26:37 +0000 | [diff] [blame] | 34 | shipStateTowed shipState = "TOWED" |
Serge Bazanski | 3d116b2 | 2021-03-27 15:43:18 +0000 | [diff] [blame] | 35 | ) |
| 36 | |
| 37 | type service struct { |
| 38 | lastStateMu sync.RWMutex |
| 39 | lastState shipState |
| 40 | lastStateTime time.Time |
| 41 | } |
| 42 | |
| 43 | func (s *service) worker(ctx context.Context) { |
| 44 | update := func() { |
| 45 | state := shipStateUnknown |
| 46 | // shitty back off, good enough. |
| 47 | retries := 10 |
| 48 | for { |
| 49 | stuck, err := get(ctx) |
| 50 | if err != nil { |
| 51 | glog.Warningf("get: %v", err) |
| 52 | if retries > 0 { |
| 53 | time.Sleep(60 * time.Second) |
| 54 | retries -= 1 |
| 55 | } else { |
| 56 | glog.Errorf("giving up on get") |
| 57 | break |
| 58 | } |
| 59 | } else { |
Serge Bazanski | a4ae66b | 2021-03-29 13:26:37 +0000 | [diff] [blame] | 60 | state = stuck |
Serge Bazanski | 3d116b2 | 2021-03-27 15:43:18 +0000 | [diff] [blame] | 61 | break |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | glog.Infof("New state: %v", state) |
| 66 | s.lastStateMu.Lock() |
| 67 | s.lastState = state |
| 68 | s.lastStateTime = time.Now() |
| 69 | s.lastStateMu.Unlock() |
| 70 | } |
| 71 | |
| 72 | update() |
| 73 | ticker := time.NewTicker(15 * 60 * time.Second) |
| 74 | for { |
| 75 | select { |
| 76 | case <-ctx.Done(): |
| 77 | return |
| 78 | case <-ticker.C: |
| 79 | update() |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func timeMust(t time.Time, err error) time.Time { |
| 85 | if err != nil { |
| 86 | panic(err) |
| 87 | } |
| 88 | return t |
| 89 | } |
| 90 | |
| 91 | var ( |
| 92 | timeStuck = timeMust(time.Parse( |
Serge Bazanski | 5da0494 | 2021-03-27 16:03:45 +0000 | [diff] [blame] | 93 | "At 15:04 Eastern European Time (-0700) on 2 January 2006", |
| 94 | "At 07:40 Eastern European Time (+0200) on 23 March 2021", |
Serge Bazanski | 3d116b2 | 2021-03-27 15:43:18 +0000 | [diff] [blame] | 95 | )) |
| 96 | ) |
| 97 | |
| 98 | func (s *service) Status(ctx context.Context, req *pb.StatusRequest) (*pb.StatusResponse, error) { |
| 99 | s.lastStateMu.RLock() |
| 100 | state := s.lastState |
| 101 | lastChecked := s.lastStateTime |
| 102 | s.lastStateMu.RUnlock() |
| 103 | |
| 104 | res := &pb.StatusResponse{ |
| 105 | LastChecked: lastChecked.UnixNano(), |
| 106 | } |
| 107 | switch state { |
| 108 | case shipStateUnknown: |
| 109 | res.Current = pb.StatusResponse_STUCKNESS_UNKNOWN |
| 110 | case shipStateStuck: |
| 111 | res.Current = pb.StatusResponse_STUCKNESS_STUCK |
| 112 | res.Elapsed = time.Since(timeStuck).Nanoseconds() |
| 113 | case shipStateFreed: |
| 114 | res.Current = pb.StatusResponse_STUCKNESS_FREE |
Serge Bazanski | a4ae66b | 2021-03-29 13:26:37 +0000 | [diff] [blame] | 115 | case shipStateTowed: |
| 116 | res.Current = pb.StatusResponse_STUCKNESS_TOWED |
| 117 | res.Elapsed = time.Since(timeStuck).Nanoseconds() |
Serge Bazanski | 3d116b2 | 2021-03-27 15:43:18 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return res, nil |
| 121 | } |
| 122 | |
| 123 | var ( |
| 124 | flagPublicAddress string |
| 125 | ) |
| 126 | |
| 127 | func main() { |
| 128 | flag.StringVar(&flagPublicAddress, "public_address", "127.0.0.1:8080", "Public HTTP/JSON listen address") |
| 129 | flag.Parse() |
| 130 | m := mirko.New() |
| 131 | if err := m.Listen(); err != nil { |
| 132 | glog.Exitf("Listen(): %v", err) |
| 133 | } |
| 134 | |
| 135 | s := &service{} |
| 136 | pb.RegisterShipStuckServer(m.GRPC(), s) |
| 137 | |
| 138 | publicMux := runtime.NewServeMux() |
| 139 | publicSrv := http.Server{ |
| 140 | Addr: flagPublicAddress, |
| 141 | Handler: publicMux, |
| 142 | } |
| 143 | go func() { |
| 144 | glog.Infof("REST listening on %s", flagPublicAddress) |
| 145 | if err := publicSrv.ListenAndServe(); err != nil { |
| 146 | glog.Exitf("public ListenAndServe: %v", err) |
| 147 | } |
| 148 | }() |
| 149 | if err := pb.RegisterShipStuckHandlerServer(m.Context(), publicMux, s); err != nil { |
| 150 | glog.Exitf("RegisterShipStuckHandlerSerever: %v", err) |
| 151 | } |
| 152 | |
| 153 | go s.worker(m.Context()) |
| 154 | |
| 155 | if err := m.Serve(); err != nil { |
| 156 | glog.Exitf("Serve(): %v", err) |
| 157 | } |
| 158 | |
| 159 | <-m.Done() |
| 160 | } |