Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
fix: use Arc to mutate the same Hashmap
Browse files Browse the repository at this point in the history
  • Loading branch information
tippfehlr committed Feb 25, 2024
1 parent 57474f7 commit 78ea4d9
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use std::{collections::HashMap, path::Path, process::Command, sync::Mutex};
use std::{
collections::HashMap,
path::Path,
process::Command,
sync::{Arc, Mutex},
};

use actix_web::{web, App, HttpResponse, HttpServer, Responder};

struct State {
currently_updating: Mutex<HashMap<String, bool>>,
currently_updating: Arc<Mutex<HashMap<String, bool>>>,
}

async fn webhook(path: web::Path<(String, String)>, state: web::Data<State>) -> impl Responder {
Expand All @@ -29,7 +34,6 @@ async fn webhook(path: web::Path<(String, String)>, state: web::Data<State>) ->
.unwrap()
.remove(&format!("{}/{}", &project, &service));
};

println!("Received update request for {}/{}", &project, &service);

let container_id = match Command::new("docker")
Expand Down Expand Up @@ -115,12 +119,14 @@ async fn webhook(path: web::Path<(String, String)>, state: web::Data<State>) ->

#[actix_web::main]
async fn main() -> std::io::Result<()> {
let currently_updating = Arc::new(Mutex::new(HashMap::<String, bool>::new()));

HttpServer::new(move || {
App::new()
.route("/{project}/{container}", web::get().to(webhook))
.route("/{project}/{container}", web::post().to(webhook))
.app_data(web::Data::new(State {
currently_updating: Mutex::new(HashMap::new()),
currently_updating: currently_updating.clone(),
}))
})
.bind(("0.0.0.0", 8537))?
Expand Down

0 comments on commit 78ea4d9

Please sign in to comment.