-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pegboard): add container runner and manager
- Loading branch information
1 parent
458de12
commit 91cd21c
Showing
33 changed files
with
1,549 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[workspace] | ||
members = [ | ||
"container-runner", | ||
"manager", | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/target | ||
**/target | ||
.dockerignore | ||
Dockerfile | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "container-runner" | ||
version = "0.1.0" | ||
edition = "2021" | ||
authors = ["Rivet Gaming, LLC <developer@rivet.gg>"] | ||
license = "Apache-2.0" | ||
|
||
[dependencies] | ||
anyhow = "1.0.79" | ||
portpicker = "0.1.1" | ||
serde = { version = "1.0.195", features = ["derive"] } | ||
serde_json = "1.0.111" | ||
signal-hook = "0.3.17" | ||
|
||
[dev-dependencies] | ||
tempfile = "3.9.0" | ||
uuid = { version = "1.6.1", features = ["v4"] } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM clux/muslrust:1.80.0-stable | ||
|
||
WORKDIR /app | ||
COPY Cargo.toml Cargo.lock . | ||
COPY src/ src/ | ||
RUN cargo build --release | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# container-runner | ||
|
||
This crate is used to run OCI bundles on the job servers themselves. This takes care of trapping signals, log | ||
shipping, rate limiting logs, and more. | ||
|
||
## Deployment | ||
|
||
This gets built & deployed in `infra/tf/infra-artifacts/` then used in `TODO`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
use std::{io::Write, net::TcpStream, sync::mpsc, thread::JoinHandle}; | ||
|
||
use anyhow::*; | ||
use serde::Serialize; | ||
use serde_json; | ||
|
||
use crate::utils::{var, Stakeholder}; | ||
|
||
#[derive(Copy, Clone, Debug)] | ||
#[repr(u8)] | ||
pub enum StreamType { | ||
StdOut = 0, | ||
StdErr = 1, | ||
} | ||
|
||
pub struct ReceivedMessage { | ||
pub stream_type: StreamType, | ||
pub ts: u64, | ||
pub message: String, | ||
} | ||
|
||
/// Sends logs from the container to the Vector agent on the machine. | ||
/// | ||
/// This will run until the `msg_rx` sender is dropped before shutting down. | ||
/// | ||
/// If attempting to reconnect while the runner is shut down, this will exit immediately, dropping | ||
/// all logs in the process. This is to ensure that if Vector becomes unreachable, we don't end up | ||
/// with a lot of lingering runners that refuse to exit. | ||
pub struct LogShipper { | ||
/// Notifies of process shutdown. | ||
pub shutdown_rx: mpsc::Receiver<()>, | ||
|
||
/// Receiver for messages to be shipped. This holds a buffer of messages waiting to be send. | ||
/// | ||
/// If the socket closes or creates back pressure, logs will be dropped on the main thread when | ||
/// trying to send to this channel. | ||
pub msg_rx: mpsc::Receiver<ReceivedMessage>, | ||
|
||
pub stakeholder: Stakeholder, | ||
} | ||
|
||
impl LogShipper { | ||
pub fn spawn(self) -> JoinHandle<()> { | ||
std::thread::spawn(move || self.run()) | ||
} | ||
|
||
fn run(self) { | ||
// Retry loop | ||
loop { | ||
match self.run_inner() { | ||
Result::Ok(()) => { | ||
println!("Exiting log shipper"); | ||
break; | ||
} | ||
Err(err) => { | ||
eprintln!("Log shipper error: {err:?}"); | ||
|
||
// Wait before attempting to reconnect. Wait for disconnect in this time | ||
// period. | ||
match self | ||
.shutdown_rx | ||
.recv_timeout(std::time::Duration::from_secs(15)) | ||
{ | ||
Result::Ok(_) => { | ||
println!("Log shipper received shutdown"); | ||
break; | ||
} | ||
Err(mpsc::RecvTimeoutError::Disconnected) => { | ||
eprintln!("Log shipper shutdown unexpectedly disconnected"); | ||
break; | ||
} | ||
Err(mpsc::RecvTimeoutError::Timeout) => { | ||
// Not shut down, attempt reconnect | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn run_inner(&self) -> Result<()> { | ||
let vector_socket_addr = var("PEGBOARD_META_vector_socket_addr")?; | ||
|
||
println!("Connecting log shipper to Vector at {vector_socket_addr}"); | ||
|
||
let mut stream = TcpStream::connect(vector_socket_addr)?; | ||
|
||
println!("Log shipper connected"); | ||
|
||
while let Result::Ok(message) = self.msg_rx.recv() { | ||
let vector_message = match &self.stakeholder { | ||
Stakeholder::DynamicServer { server_id } => VectorMessage::DynamicServers { | ||
server_id: server_id.as_str(), | ||
task: "main", // Backwards compatibility with logs | ||
stream_type: message.stream_type as u8, | ||
ts: message.ts, | ||
message: message.message.as_str(), | ||
}, | ||
}; | ||
|
||
serde_json::to_writer(&mut stream, &vector_message)?; | ||
stream.write_all(b"\n")?; | ||
} | ||
|
||
println!("Log shipper msg_rx disconnected"); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
/// Vector-compatible message format | ||
#[derive(Serialize)] | ||
#[serde(tag = "source")] | ||
enum VectorMessage<'a> { | ||
#[serde(rename = "dynamic_servers")] | ||
DynamicServers { | ||
server_id: &'a str, | ||
task: &'a str, | ||
stream_type: u8, | ||
ts: u64, | ||
message: &'a str, | ||
}, | ||
} |
Oops, something went wrong.