Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP feat: count recent start events before restart #469

Merged
merged 5 commits into from
Nov 11, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions gateway/src/project.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::convert::Infallible;
use std::net::{IpAddr, SocketAddr};
use std::time::Duration;
Expand All @@ -7,6 +8,7 @@ use bollard::container::{
};
use bollard::errors::Error as DockerError;
use bollard::models::{ContainerConfig, ContainerInspectResponse, ContainerStateStatusEnum};
use bollard::system::EventsOptions;
use futures::prelude::*;
use http::uri::InvalidUri;
use http::Uri;
Expand Down Expand Up @@ -59,7 +61,7 @@ macro_rules! impl_from_variant {
}

const RUNTIME_API_PORT: u16 = 8001;
const MAX_RESTARTS: i64 = 3;
const MAX_RESTARTS: usize = 3;

// Client used for health checks
static CLIENT: Lazy<Client<HttpConnector>> = Lazy::new(Client::new);
Expand Down Expand Up @@ -687,9 +689,33 @@ where
type Next = ProjectStarting;
type Error = ProjectError;

async fn next(self, _ctx: &Ctx) -> Result<Self::Next, Self::Error> {
async fn next(self, ctx: &Ctx) -> Result<Self::Next, Self::Error> {
let container_id = self
oddgrd marked this conversation as resolved.
Show resolved Hide resolved
.container
.id
.as_deref()
.expect("container should have ID");

// Filter and count the `start` events for this project in the last 15 minutes
let start_event_count = ctx
.docker()
.events(Some(EventsOptions::<String> {
since: Some(
(chrono::offset::Utc::now() - chrono::Duration::minutes(15)).to_string(),
),
until: Some(chrono::offset::Utc::now().to_string()),
filters: HashMap::from([
("container".to_string(), vec![container_id.to_string()]),
("event".to_string(), vec!["start".to_string()]),
]),
}))
.count()
oddgrd marked this conversation as resolved.
Show resolved Hide resolved
.await;

debug!("start event count: {start_event_count}");

// If stopped, and has not restarted too much, try to restart
if self.container.restart_count.unwrap_or_default() < MAX_RESTARTS {
if start_event_count < MAX_RESTARTS {
Ok(ProjectStarting {
container: self.container,
})
Expand Down