Skip to content

Commit 9e993af

Browse files
committed
feat: why not working
1 parent 006cedd commit 9e993af

File tree

6 files changed

+38
-35
lines changed

6 files changed

+38
-35
lines changed

src/endpoint/internal/update/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,24 @@ pub async fn post(Json(payload): Json<PostPayload>) -> impl IntoResponse {
3636
StatusCode::OK
3737
}
3838

39-
async fn post_transaction(cx: QueuedAsyncFrameworkContext, payload: PostPayload) -> State<()> {
39+
async fn post_transaction(cx: QueuedAsyncFrameworkContext, _payload: PostPayload) -> State<()> {
4040
unwrap!(cx.check());
4141

42-
match transactions::docker::pull_image(&payload.image).await {
42+
match transactions::docker::compose_up(&DOCKER_CONTAINER_NAME).await {
4343
Ok(_) => {}
4444
Err(e) => {
45-
tracing::error!("failed to update: {e:?}");
45+
tracing::error!("failed to update {}: {e:?}", &*DOCKER_CONTAINER_NAME);
4646
return State::Retry;
4747
}
4848
}
4949

50-
unwrap!(cx.check());
51-
52-
match transactions::docker::compose_up(&DOCKER_CONTAINER_NAME).await {
50+
match transactions::nginx::reload().await {
5351
Ok(_) => {}
5452
Err(e) => {
55-
tracing::error!("failed to update {}: {e:?}", &*DOCKER_CONTAINER_NAME);
53+
tracing::error!(
54+
"failed to reload nginx after updating {}: {e:?}",
55+
&*DOCKER_CONTAINER_NAME
56+
);
5657
return State::Retry;
5758
}
5859
}

src/endpoint/internal/website/deploy/mod.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,6 @@ pub async fn post(Json(payload): Json<PostPayload>) -> impl IntoResponse {
9797
async fn post_transaction(cx: QueuedAsyncFrameworkContext, payload: PostPayload) -> State<()> {
9898
unwrap!(cx.check());
9999

100-
match transactions::docker::pull_image(&payload.image).await {
101-
Ok(_) => {}
102-
Err(e) => {
103-
tracing::error!("failed to deploy {}: {e:?}", &payload.target);
104-
return State::Retry;
105-
}
106-
}
107-
108-
unwrap!(cx.check());
109-
110100
match transactions::docker::compose_up(payload.target.to_string().as_str()).await {
111101
Ok(_) => {}
112102
Err(e) => {

src/env.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ static_lazy_lock! {
4848
pub DOCKER_CONTAINER_NAME: String = env::var("DOCKER_CONTAINER_NAME").unwrap_or_else(|_| format!("api-{}", clap::crate_name!()));
4949
}
5050

51+
static_lazy_lock! {
52+
/// The name of the Docker container that runs Nginx.
53+
pub DOCKER_CONTAINER_NAME_NGINX: String = env::var("DOCKER_CONTAINER_NAME_NGINX").unwrap_or_else(|_| "nginx".into());
54+
}
55+
5156
static_lazy_lock! {
5257
/// The path to the Docker Compose file.
5358
pub DOCKER_COMPOSE_FILE: PathBuf = parse_env!("DOCKER_COMPOSE_FILE" => |s| Ok(PathBuf::from(s))).unwrap();

src/transactions/docker.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,6 @@ use anyhow::Result;
22

33
use crate::env::DOCKER_COMPOSE_FILE;
44

5-
pub async fn pull_image(image: &str) -> Result<()> {
6-
match tokio::process::Command::new("docker")
7-
.arg("pull")
8-
.arg(image)
9-
.output()
10-
.await
11-
{
12-
Ok(_) => {
13-
tracing::info!("successfully pulled image {}", image);
14-
Ok(())
15-
}
16-
Err(e) => {
17-
tracing::error!("failed to pull image {}: {e:?}", image);
18-
Err(anyhow::anyhow!("failed to pull image"))
19-
}
20-
}
21-
}
22-
235
pub async fn compose_up(container_name: &str) -> Result<()> {
246
match tokio::process::Command::new("docker")
257
.arg("compose")
@@ -28,6 +10,8 @@ pub async fn compose_up(container_name: &str) -> Result<()> {
2810
.arg("up")
2911
.arg("-d")
3012
.arg(container_name)
13+
.arg("--pull")
14+
.arg("always")
3115
.arg("--force-recreate")
3216
.output()
3317
.await

src/transactions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod docker;
2+
pub mod nginx;

src/transactions/nginx.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::env::DOCKER_CONTAINER_NAME_NGINX;
2+
3+
pub async fn reload() -> anyhow::Result<()> {
4+
match tokio::process::Command::new("docker")
5+
.arg("exec")
6+
.arg(&*DOCKER_CONTAINER_NAME_NGINX)
7+
.arg("nginx")
8+
.arg("-s")
9+
.arg("reload")
10+
.output()
11+
.await
12+
{
13+
Ok(_) => {
14+
tracing::info!("successfully reloaded nginx");
15+
Ok(())
16+
}
17+
Err(e) => {
18+
tracing::error!("failed to reload nginx: {e:?}");
19+
Err(anyhow::anyhow!("failed to reload nginx"))
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)