Mercurial > templog
annotate rust/src/params.rs @ 638:a9f353f488d0 rust
fix channels
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 09 Nov 2019 11:35:59 +0800 |
parents | 43eb3cfdf769 |
children | 89818a14648b |
rev | line source |
---|---|
592 | 1 use std::time::Duration; |
634 | 2 |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
3 use std::str; |
634 | 4 |
5 | |
6 | |
7 use std::cell::{RefCell}; | |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
8 use std::fs::File; |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
9 use std::io::Read; |
627 | 10 |
633 | 11 use serde::{Serialize,Deserialize}; |
592 | 12 |
634 | 13 use rand::rngs::{OsRng}; |
14 use rand::{RngCore}; | |
15 | |
16 | |
17 use hyper; | |
633 | 18 |
634 | 19 // for try_concat() |
20 use futures::stream::TryStreamExt; | |
21 use futures::executor::block_on; | |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
22 |
633 | 23 use riker::actors::*; |
24 | |
25 use super::types::*; | |
26 use super::config::Config; | |
592 | 27 |
615 | 28 #[derive(Deserialize, Serialize, Debug, Clone)] |
29 pub struct Params { | |
30 pub fridge_setpoint: f32, | |
31 pub fridge_difference: f32, | |
621 | 32 pub overshoot_delay: u64, |
615 | 33 pub overshoot_factor: f32, |
34 pub disabled: bool, | |
35 pub nowort: bool, | |
36 pub fridge_range_lower: f32, | |
37 pub fridge_range_upper: f32, | |
38 } | |
39 | |
40 impl Params { | |
41 pub fn defaults() -> Params { | |
42 Params { | |
43 fridge_setpoint: 16.0, | |
44 fridge_difference: 0.2, | |
45 overshoot_delay: 720, // 12 minutes | |
46 overshoot_factor: 1.0, | |
634 | 47 disabled: true, |
615 | 48 nowort: false, |
49 fridge_range_lower: 3.0, | |
50 fridge_range_upper: 3.0, | |
51 } | |
52 } | |
53 | |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
54 fn try_load(filename: &str) -> Result<Params, TemplogError> { |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
55 let mut s = String::new(); |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
56 File::open(filename)?.read_to_string(&mut s)?; |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
57 Ok(serde_json::from_str(&s)?) |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
58 } |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
59 |
615 | 60 pub fn load(config: &Config) -> Params { |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
61 Self::try_load(&config.params_file) |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
62 .unwrap_or_else(|_| Params::defaults()) |
615 | 63 } |
633 | 64 |
615 | 65 } |
66 | |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
67 pub struct ParamWaiter { |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
68 limitlog: NotTooOften, |
616 | 69 // last_etag is used for long-polling. |
70 last_etag: RefCell<String>, | |
71 epoch: String, | |
638 | 72 notify: ChannelRef<Params>, |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
73 |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
74 config: Config, |
592 | 75 } |
76 | |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
77 const LOG_MINUTES: u64 = 15; |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
78 const MAX_RESPONSE_SIZE: usize = 10000; |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
79 const TIMEOUT_MINUTES: u64 = 5; |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
80 |
593
bf138339d20a
fiddling with timeouts and closures
Matt Johnston <matt@ucc.asn.au>
parents:
592
diff
changeset
|
81 impl ParamWaiter { |
638 | 82 pub fn new_actor((config, notify): (Config, ChannelRef<Params>)) -> Self { |
83 Self::new(config, notify) | |
84 } | |
633 | 85 |
638 | 86 pub fn new(config: Config, notify: ChannelRef<Params>) -> Self { |
633 | 87 let mut b = [0u8; 15]; // 15 bytes -> 20 characters base64 |
88 OsRng.fill_bytes(&mut b); | |
89 let epoch = base64::encode(&b); | |
90 | |
91 ParamWaiter { | |
92 limitlog: NotTooOften::new(LOG_MINUTES*60), | |
93 last_etag: RefCell::new(String::new()), | |
94 epoch: epoch, | |
95 config: config, | |
638 | 96 notify: notify, |
614
e1bab5b36352
using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
613
diff
changeset
|
97 } |
609
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
98 } |
7bda01659426
not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents:
594
diff
changeset
|
99 |
634 | 100 async fn wait_updates(uri: &str, etag: &str) -> Result<(hyper::Chunk, hyper::StatusCode), TemplogError> { |
638 | 101 debug!("wait_updates {}", uri); |
634 | 102 let req = hyper::Request::get(uri) |
103 .header(hyper::header::ETAG, etag)//*self.last_etag.borrow()) | |
104 .body(hyper::Body::from("")).unwrap(); | |
633 | 105 |
634 | 106 // TODO timeout? |
107 let resp = hyper::Client::new().request(req).await?; | |
108 let status = resp.status(); | |
109 let chunk = resp.into_body().try_concat().await?; | |
633 | 110 |
634 | 111 Ok((chunk, status)) |
633 | 112 } |
113 | |
626 | 114 fn handle_response(&self, buf : hyper::Chunk, status: hyper::StatusCode) -> Result<Params, TemplogError> { |
634 | 115 |
633 | 116 #[derive(Deserialize, Debug)] |
117 struct Response { | |
118 // sent as an opaque etag: header. Has format "epoch-nonce", | |
119 // responses where the epoch do not match ParamWaiter::epoch are dropped | |
120 etag: String, | |
121 params: Params, | |
122 } | |
123 | |
626 | 124 match status { |
634 | 125 hyper::StatusCode::OK => { |
613
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
126 // new params |
634 | 127 let r: Response = serde_json::from_slice(&buf)?; |
618
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
128 let mut le = self.last_etag.borrow_mut(); |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
129 *le = r.etag; |
626 | 130 |
131 // update params if the epoch is correct | |
132 if let Some(e) = le.split('-').next() { | |
133 if e == &self.epoch { | |
134 self.write_params(&r.params); | |
135 return Ok(r.params); | |
136 } | |
618
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
137 } |
626 | 138 Err(TemplogError::new(&format!("Bad epoch from server '{}' expected '{}'", *le, self.epoch))) |
613
5c2b0d47bb83
Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents:
611
diff
changeset
|
139 } |
634 | 140 hyper::StatusCode::NOT_MODIFIED => { |
629 | 141 // XXX this isn't really an error. Should handle_response() return |
142 // Result<Option<Params>, TemplogError> instead? | |
618
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
143 Err(TemplogError::new("304 unmodified (long polling timeout at the server)")) |
2d65a9f0bed3
params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents:
616
diff
changeset
|
144 }, |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
145 _ => { |
634 | 146 let text = String::from_utf8_lossy(buf.as_ref()); |
626 | 147 Err(TemplogError::new(&format!("Wrong server response code {}: {}", status.as_u16(), text))) |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
148 }, |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
149 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
150 } |
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
151 |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
152 fn write_params(&self, params: &Params) { |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
153 let p = atomicwrites::AtomicFile::new(&self.config.params_file, atomicwrites::AllowOverwrite); |
638 | 154 let r = p.write(|f| { |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
155 serde_json::to_writer(f, params) |
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
156 }); |
638 | 157 if let Err(e) = r { |
158 // XXX notify? | |
159 error!("Couldn't write to {}: {}", self.config.params_file, e) | |
160 }; | |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
161 } |
634 | 162 |
163 fn do_poll(&mut self, ctx: &Context<<Self as Actor>::Msg>) -> Result<(), TemplogError> { | |
635
4424a8b30f9c
config crate wants everything to be lower case
Matt Johnston <matt@ucc.asn.au>
parents:
634
diff
changeset
|
164 let url = self.config.settings_url.clone(); |
634 | 165 let etag = self.last_etag.borrow().clone(); |
166 let h = ctx.run(async move { | |
167 Self::wait_updates(&url, &etag).await | |
168 }).expect("spawn failed"); // XXX error handling | |
169 let (chunk, stat) = block_on(h)?; | |
170 let new_params = self.handle_response(chunk, stat)?; | |
638 | 171 self.notify.tell(Publish{msg: new_params, topic: "params".into()}, None); |
634 | 172 Ok(()) |
173 } | |
633 | 174 } |
624
2710649ab71e
read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents:
621
diff
changeset
|
175 |
634 | 176 #[derive(Clone,Debug)] |
177 pub struct PollForParams; | |
178 | |
633 | 179 impl Actor for ParamWaiter { |
634 | 180 type Msg = PollForParams; |
611
f3e39e2107fd
still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents:
609
diff
changeset
|
181 |
634 | 182 fn recv(&mut self, |
183 ctx: &Context<Self::Msg>, | |
184 _msg: Self::Msg, | |
185 _sender: Sender) { | |
186 // schedule a retry once this iteration finishes | |
187 ctx.schedule_once(Duration::from_secs(1), ctx.myself(), None, PollForParams); | |
188 | |
189 if let Err(e) = self.do_poll(ctx) { | |
636
43eb3cfdf769
some progress, better error handling
Matt Johnston <matt@ucc.asn.au>
parents:
635
diff
changeset
|
190 self.limitlog.and_then(|| { |
43eb3cfdf769
some progress, better error handling
Matt Johnston <matt@ucc.asn.au>
parents:
635
diff
changeset
|
191 warn!("Problem fetching params: {}", e) |
43eb3cfdf769
some progress, better error handling
Matt Johnston <matt@ucc.asn.au>
parents:
635
diff
changeset
|
192 }); |
634 | 193 } |
194 } | |
195 | |
196 fn pre_start(&mut self, ctx: &Context<Self::Msg>) { | |
197 ctx.schedule_once(Duration::from_secs(1), ctx.myself(), None, PollForParams); | |
592 | 198 } |
199 } | |
200 | |
633 | 201 // pub fn stream(config: Config, handle: Handle) -> Result<(), TemplogError> { |
202 // let rcself = Rc::new(ParamWaiter::new(config, handle)); | |
203 | |
204 // let dur = Duration::from_millis(4000); | |
205 // for _ in Interval::new(dur, &rcself.handle).unwrap() { | |
206 // // fetch params | |
207 // // TODO - skip if inflight. | |
208 // let r = await!(rcself.make_request()).map_err(|e| TemplogError::new_hyper("response", e))?; | |
209 // let status = r.status(); | |
210 // let b = await!(r.body().concat2()).map_err(|e| TemplogError::new_hyper("body", e))?; | |
211 // if let Ok(params) = rcself.handle_response(b, status) { | |
212 // stream_yield!(params); | |
213 // } | |
214 // } | |
215 // Ok(()) | |
216 // } | |
217 |