Skip to content

Commit b0d1c92

Browse files
committed
feat(sherlock): Implemented the Model pulling logic
1 parent b2f07b5 commit b0d1c92

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

sherlock/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ Cargo.lock
1717

1818
# MSVC Windows builds of rustc generate these, which store debugging information
1919
*.pdb
20-
20+
models/*
21+
!models/.gitkeep
2122
# End of https://www.toptal.com/developers/gitignore/api/rust

sherlock/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ edition = "2021"
88
[dependencies]
99
prometheus = "0.13.0"
1010
lazy_static = "1.4.0"
11-
hyper = { version = "0.14", features = ["full"] }
11+
hyper = { version = "0.14.17", features = ["full"] }
1212
tokio = { version = "1", features = ["full"] }
13-
reqwest = { version = "0.11", features = ["blocking", "json"] }
13+
reqwest = { version = "0.11", features = ["blocking", "json"] }
14+
flate2 = "1.0.22"
15+
tar = "0.4.38"

sherlock/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ENV SERVICE_NAME=sample_service
2121
ENV NAMESPACE=default
2222
ENV END_POINT=http://localhost:8501/v1/models/sherlock:predict
2323
ENV POOL_DURATION=60
24-
24+
ENV MODEL_URL=https://static.isala.me/lazy-koala/sherlock/models/sample_service.tar.gz
2525
# Define port
2626
EXPOSE 9898
2727

sherlock/models/.gitkeep

Whitespace-only changes.

sherlock/src/main.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ use std::{env::var, thread, time::Duration};
88
use lazy_static::lazy_static;
99
use prometheus::{labels, opts, register_gauge};
1010
use std::collections::HashMap;
11+
use flate2::read::GzDecoder;
12+
use tar::Archive;
1113

1214
lazy_static! {
1315
static ref SERVICE_NAME: String = var("SERVICE_NAME").unwrap();
1416
static ref NAMESPACE: String = var("NAMESPACE").unwrap();
1517
static ref END_POINT: String = var("END_POINT").unwrap();
1618
static ref POOL_DURATION: String = var("POOL_DURATION").unwrap();
19+
static ref MODEL_URL: String = var("MODEL_URL").unwrap();
1720
static ref ANOMLAY_GAUGE: Gauge = register_gauge!(opts!(
1821
"anomaly_score",
1922
"Reconstruction loss of the autoencoder",
@@ -22,6 +25,15 @@ lazy_static! {
2225
.unwrap();
2326
}
2427

28+
fn download_model()->Result<(), Box<dyn std::error::Error>>{
29+
let resp = reqwest::blocking::get(MODEL_URL.as_str())?;
30+
let tarfile = GzDecoder::new(resp);
31+
let mut archive = Archive::new(tarfile);
32+
archive.unpack("models/")?;
33+
34+
Ok(())
35+
}
36+
2537
async fn serve_req(_req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
2638
let encoder = TextEncoder::new();
2739

@@ -57,9 +69,20 @@ fn poll_anomaly_scores(delay: u64) {
5769
}
5870
}
5971

72+
#[allow(unused_must_use)]
6073
#[tokio::main]
6174
async fn main() {
6275

76+
// Forgive me father for i have sinned 🙏
77+
// I couldn't figure out way to use reqwest's
78+
// async response with GzDecoder 😭
79+
thread::spawn(|| {
80+
if let Err(err) = download_model() {
81+
eprintln!("failed to download the model: {}", err);
82+
std::process::exit(1);
83+
}
84+
}).join();
85+
6386
thread::spawn(|| poll_anomaly_scores(POOL_DURATION.as_str().parse::<u64>().unwrap()));
6487

6588
ANOMLAY_GAUGE.set(0.0);

0 commit comments

Comments
 (0)