Skip to content

Commit

Permalink
Add dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
max-lt committed Mar 23, 2024
1 parent a441c8d commit a4b2276
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[env]
RUNTIME_SNAPSHOT_PATH = { value = "/tmp/openworkers-runtime-snapshot.bin", relative = false }
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.vscode
.git

target

.env

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"cSpell.words": ["actix", "nats", "oneshot", "sqlx"]
"cSpell.words": ["actix", "dotenv", "nats", "oneshot", "sqlx"]
}
19 changes: 17 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "openworkers-runner"
version = "0.1.4"
version = "0.1.5"
edition = "2021"
default-run = "openworkers-runner"

Expand All @@ -14,11 +14,13 @@ tokio = "1.36.0"
env_logger = "0.11.2"
http_v02 = { package = "http", version = "0.2.9" }
sqlx = { version = "0.7", features = [ "runtime-tokio", "postgres", "uuid", "bigdecimal", "rust_decimal" ] }
# openworkers-runtime ={ git = "https://github.com/openworkers/openworkers-runtime", branch = "master" }
openworkers-runtime = { path = "../openworkers-runtime" }
openworkers-runtime ={ git = "https://github.com/openworkers/openworkers-runtime", tag = "v0.1.5"}
# openworkers-runtime = { path = "../openworkers-runtime" }
nats = "0.24.1"
serde_json = "1.0.114"
serde = { version = "1.0.197", features = ["derive"] }
dotenv = "0.15.0"
base64 = "0.22.0"

# https://doc.rust-lang.org/cargo/reference/profiles.html
# https://github.com/johnthagen/min-sized-rust?tab=readme-ov-file#minimizing-rust-binary-size
Expand Down
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM rust:1.77 as builder

RUN mkdir -p /build/openworkers-runner

ENV RUST_BACKTRACE=1
ENV RUNTIME_SNAPSHOT_PATH=/build/snapshot.bin

WORKDIR /build/openworkers-runner

COPY . /build/openworkers-runner

RUN touch $RUNTIME_SNAPSHOT_PATH

RUN --mount=type=cache,target=~/.cargo \
--mount=type=cache,target=/build/openworkers-runner/target \
cargo run --release --bin snapshot && \
cargo build --release && cp /build/openworkers-runner/target/release/openworkers-runner /build/output

FROM debian:bookworm-slim

COPY --from=builder /build/output /usr/local/bin/openworkers-runner

CMD ["/usr/local/bin/openworkers-runner"]

EXPOSE 8080
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ export RUST_LOG=openworkers_runtime=debug,openworkers_runner=debug # Optional

cargo run
```


### Install sqlx-cli (optional - only for development)

```bash
cargo install sqlx-cli --no-default-features --features rustls,postgres
```

#### Prepare the database
```bash
cargo sqlx prepare
```
5 changes: 3 additions & 2 deletions bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ async fn handle_request(data: Data<AppState>, req: HttpRequest) -> HttpResponse

#[actix_web::main]
async fn main() -> std::io::Result<()> {
dotenv::dotenv().ok();

if !std::env::var("RUST_LOG").is_ok() {
std::env::set_var("RUST_LOG", "info");
}
Expand All @@ -187,8 +189,7 @@ async fn main() -> std::io::Result<()> {

debug!("start main");

let db_url = std::env::var("DATABASE_URL")
.unwrap_or("postgres://admin:[email protected]/swap_dev".to_string());
let db_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let pool = PgPoolOptions::new()
.max_connections(4)
.connect(&db_url)
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod scheduled;
pub mod store;
pub mod log;
pub mod nats;
2 changes: 1 addition & 1 deletion src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn create_log_handler(worker_id: String) -> std::sync::mpsc::Sender<LogEvent
let (tx, rx) = std::sync::mpsc::channel::<LogEvent>();

std::thread::spawn(move || {
let nc = nats::connect("nats://127.0.0.1:4222").expect("failed to connect to nats");
let nc = crate::nats::nats_connect();

for event in rx {
log::debug!("{:?}", event);
Expand Down
21 changes: 21 additions & 0 deletions src/nats.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use base64::{engine::general_purpose::STANDARD, Engine as _};

pub fn nats_connect() -> nats::Connection {
let nats_servers = std::env::var("NATS_SERVERS").expect("NATS_SERVERS must be set");

log::debug!("connecting to nats: {nats_servers}");

let conn = match std::env::var("NATS_CREDS") {
Ok(credentials) => {
// Decode the base64 encoded credentials
let credentials: Vec<u8> = STANDARD.decode(credentials).expect("failed to decode credentials");
let credentials = String::from_utf8(credentials).expect("failed to convert credentials to string");
let options = nats::Options::with_static_credentials(&credentials).expect("failed to create nats options");

options.connect(nats_servers)
},
Err(_) => nats::connect(nats_servers),
};

conn.expect("failed to connect to nats")
}
2 changes: 1 addition & 1 deletion src/scheduled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn handle_scheduled(db: store::Database) {
.unwrap();

let handle = local.spawn_local(async move {
let nc = nats::connect("nats://127.0.0.1:4222").expect("failed to connect to nats");
let nc = crate::nats::nats_connect();
let sub = nc
.queue_subscribe("scheduled", "runner")
.expect("failed to subscribe to scheduled");
Expand Down

0 comments on commit a4b2276

Please sign in to comment.