annotate rust/src/params.rs @ 636:43eb3cfdf769 rust

some progress, better error handling
author Matt Johnston <matt@ucc.asn.au>
date Wed, 16 Oct 2019 22:33:06 +0800
parents 4424a8b30f9c
children a9f353f488d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 use std::time::Duration;
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
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
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
4
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
5
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
6
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
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
d5075136442f futures await
Matt Johnston <matt@ucc.asn.au>
parents: 626
diff changeset
10
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
11 use serde::{Serialize,Deserialize};
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
13 use rand::rngs::{OsRng};
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
14 use rand::{RngCore};
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
15
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
16
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
17 use hyper;
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
18
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
19 // for try_concat()
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
20 use futures::stream::TryStreamExt;
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
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
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
23 use riker::actors::*;
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
24
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
25 use super::types::*;
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
26 use super::config::Config;
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
28 #[derive(Deserialize, Serialize, Debug, Clone)]
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
29 pub struct Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
30 pub fridge_setpoint: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
31 pub fridge_difference: f32,
621
8136a6b99866 make it compile again
Matt Johnston <matt@ucc.asn.au>
parents: 618
diff changeset
32 pub overshoot_delay: u64,
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
33 pub overshoot_factor: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
34 pub disabled: bool,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
35 pub nowort: bool,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
36 pub fridge_range_lower: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
37 pub fridge_range_upper: f32,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
38 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
39
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
40 impl Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
41 pub fn defaults() -> Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
42 Params {
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
43 fridge_setpoint: 16.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
44 fridge_difference: 0.2,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
45 overshoot_delay: 720, // 12 minutes
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
46 overshoot_factor: 1.0,
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
47 disabled: true,
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
48 nowort: false,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
49 fridge_range_lower: 3.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
50 fridge_range_upper: 3.0,
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
51 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
52 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
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
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
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
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
63 }
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
64
615
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
65 }
f153aec221be move Params, epoch code
Matt Johnston <matt@ucc.asn.au>
parents: 614
diff changeset
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
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
69 // last_etag is used for long-polling.
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
70 last_etag: RefCell<String>,
a85c0c9bc1fa hide epoch in ParamWaiter
Matt Johnston <matt@ucc.asn.au>
parents: 615
diff changeset
71 epoch: String,
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
72 chan: Option<ChannelRef<Params>>, // TODO: a way to avoid Option?
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
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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 {
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
82
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
83 pub fn new(config: Config) -> Self {
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
84 let mut b = [0u8; 15]; // 15 bytes -> 20 characters base64
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
85 OsRng.fill_bytes(&mut b);
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
86 let epoch = base64::encode(&b);
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
87
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
88 ParamWaiter {
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
89 limitlog: NotTooOften::new(LOG_MINUTES*60),
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
90 last_etag: RefCell::new(String::new()),
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
91 epoch: epoch,
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
92 chan: None,
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
93 config: config,
614
e1bab5b36352 using some refcells for the paramwaiter
Matt Johnston <matt@ucc.asn.au>
parents: 613
diff changeset
94 }
609
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
95 }
7bda01659426 not building, paramwaiter work
Matt Johnston <matt@ucc.asn.au>
parents: 594
diff changeset
96
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
97 async fn wait_updates(uri: &str, etag: &str) -> Result<(hyper::Chunk, hyper::StatusCode), TemplogError> {
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
98 let req = hyper::Request::get(uri)
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
99 .header(hyper::header::ETAG, etag)//*self.last_etag.borrow())
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
100 .body(hyper::Body::from("")).unwrap();
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
101
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
102 // TODO timeout?
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
103 let resp = hyper::Client::new().request(req).await?;
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
104 let status = resp.status();
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
105 let chunk = resp.into_body().try_concat().await?;
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
106
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
107 Ok((chunk, status))
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
108 }
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
109
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
110 fn handle_response(&self, buf : hyper::Chunk, status: hyper::StatusCode) -> Result<Params, TemplogError> {
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
111
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
112 #[derive(Deserialize, Debug)]
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
113 struct Response {
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
114 // sent as an opaque etag: header. Has format "epoch-nonce",
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
115 // responses where the epoch do not match ParamWaiter::epoch are dropped
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
116 etag: String,
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
117 params: Params,
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
118 }
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
119
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
120 match status {
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
121 hyper::StatusCode::OK => {
613
5c2b0d47bb83 Response has epoch_tag and params
Matt Johnston <matt@ucc.asn.au>
parents: 611
diff changeset
122 // new params
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
123 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
124 let mut le = self.last_etag.borrow_mut();
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
125 *le = r.etag;
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
126
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
127 // update params if the epoch is correct
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
128 if let Some(e) = le.split('-').next() {
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
129 if e == &self.epoch {
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
130 self.write_params(&r.params);
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
131 return Ok(r.params);
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
132 }
618
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
133 }
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
134 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
135 }
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
136 hyper::StatusCode::NOT_MODIFIED => {
629
3e5e52d50af5 comment
Matt Johnston <matt@ucc.asn.au>
parents: 627
diff changeset
137 // XXX this isn't really an error. Should handle_response() return
3e5e52d50af5 comment
Matt Johnston <matt@ucc.asn.au>
parents: 627
diff changeset
138 // Result<Option<Params>, TemplogError> instead?
618
2d65a9f0bed3 params returning stream of success
Matt Johnston <matt@ucc.asn.au>
parents: 616
diff changeset
139 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
140 },
611
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
141 _ => {
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
142 let text = String::from_utf8_lossy(buf.as_ref());
626
efcbe0d3afd6 fix to work with hyper
Matt Johnston <matt@ucc.asn.au>
parents: 624
diff changeset
143 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
144 },
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
145 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
146 }
f3e39e2107fd still doesn't compile, improvements to TemplogError and tokio curl though
Matt Johnston <matt@ucc.asn.au>
parents: 609
diff changeset
147
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
148 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
149 let p = atomicwrites::AtomicFile::new(&self.config.params_file, atomicwrites::AllowOverwrite);
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
150 p.write(|f| {
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
151 serde_json::to_writer(f, params)
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
152 });
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
153 }
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
154
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
155 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
156 let url = self.config.settings_url.clone();
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
157 let etag = self.last_etag.borrow().clone();
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
158 let h = ctx.run(async move {
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
159 Self::wait_updates(&url, &etag).await
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
160 }).expect("spawn failed"); // XXX error handling
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
161 let (chunk, stat) = block_on(h)?;
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
162 let new_params = self.handle_response(chunk, stat)?;
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
163 self.chan.as_ref().unwrap().tell(Publish{msg: new_params, topic: "params".into()}, None);
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
164 Ok(())
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
165 }
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
166 }
624
2710649ab71e read/write params local file. untested
Matt Johnston <matt@ucc.asn.au>
parents: 621
diff changeset
167
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
168 #[derive(Clone,Debug)]
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
169 pub struct PollForParams;
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
170
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
171 impl Actor for ParamWaiter {
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
172 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
173
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
174 fn recv(&mut self,
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
175 ctx: &Context<Self::Msg>,
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
176 _msg: Self::Msg,
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
177 _sender: Sender) {
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
178 // schedule a retry once this iteration finishes
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
179 ctx.schedule_once(Duration::from_secs(1), ctx.myself(), None, PollForParams);
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
180
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
181 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
182 self.limitlog.and_then(|| {
43eb3cfdf769 some progress, better error handling
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
183 warn!("Problem fetching params: {}", e)
43eb3cfdf769 some progress, better error handling
Matt Johnston <matt@ucc.asn.au>
parents: 635
diff changeset
184 });
634
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
185 }
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
186 }
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
187
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
188 fn pre_start(&mut self, ctx: &Context<Self::Msg>) {
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
189 self.chan = Some(channel("params", &ctx.system).unwrap());
a5721c02d3ee build succeeds
Matt Johnston <matt@ucc.asn.au>
parents: 633
diff changeset
190 ctx.schedule_once(Duration::from_secs(1), ctx.myself(), None, PollForParams);
592
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 }
03b48ec0bb03 fridge, types, configwaiter
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193
633
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
194 // pub fn stream(config: Config, handle: Handle) -> Result<(), TemplogError> {
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
195 // let rcself = Rc::new(ParamWaiter::new(config, handle));
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
196
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
197 // let dur = Duration::from_millis(4000);
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
198 // for _ in Interval::new(dur, &rcself.handle).unwrap() {
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
199 // // fetch params
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
200 // // TODO - skip if inflight.
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
201 // let r = await!(rcself.make_request()).map_err(|e| TemplogError::new_hyper("response", e))?;
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
202 // let status = r.status();
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
203 // let b = await!(r.body().concat2()).map_err(|e| TemplogError::new_hyper("body", e))?;
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
204 // if let Ok(params) = rcself.handle_response(b, status) {
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
205 // stream_yield!(params);
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
206 // }
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
207 // }
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
208 // Ok(())
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
209 // }
490e9e15b98c move some bits to riker
Matt Johnston <matt@ucc.asn.au>
parents: 632
diff changeset
210