Skip to content

Commit

Permalink
refactor: adjust async webhook notification
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Dec 23, 2024
1 parent 4e39e26 commit 252c7c3
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 280 deletions.
102 changes: 51 additions & 51 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,63 +58,63 @@ jobs:
run: |
make release-all
ldd target/release/pingap
windows:
runs-on: windows-latest
name: ${{ matrix.target }}
env:
CFG_RELEASE_CHANNEL: ${{ matrix.cfg_release_channel }}
strategy:
# https://help.github.com/en/actions/getting-started-with-github-actions/about-github-actions#usage-limits
# There's a limit of 60 concurrent jobs across all repos in the rust-lang organization.
# In order to prevent overusing too much of that 60 limit, we throttle the
# number of rustfmt jobs that will run concurrently.
matrix:
target: [x86_64-pc-windows-msvc]
# windows:
# runs-on: windows-latest
# name: ${{ matrix.target }}
# env:
# CFG_RELEASE_CHANNEL: ${{ matrix.cfg_release_channel }}
# strategy:
# # https://help.github.com/en/actions/getting-started-with-github-actions/about-github-actions#usage-limits
# # There's a limit of 60 concurrent jobs across all repos in the rust-lang organization.
# # In order to prevent overusing too much of that 60 limit, we throttle the
# # number of rustfmt jobs that will run concurrently.
# matrix:
# target: [x86_64-pc-windows-msvc]

steps:
# The Windows runners have autocrlf enabled by default
# which causes failures for some of rustfmt's line-ending sensitive tests
- name: disable git eol translation
run: git config --global core.autocrlf false
- uses: actions/checkout@v4
- name: setup node
uses: actions/setup-node@v4
- name: build-web
run: make build-web
# steps:
# # The Windows runners have autocrlf enabled by default
# # which causes failures for some of rustfmt's line-ending sensitive tests
# - name: disable git eol translation
# run: git config --global core.autocrlf false
# - uses: actions/checkout@v4
# - name: setup node
# uses: actions/setup-node@v4
# - name: build-web
# run: make build-web

# Run build
- name: Install Rustup using win.rustup.rs
run: |
# Disable the download progress bar which can cause perf issues
$ProgressPreference = "SilentlyContinue"
Invoke-WebRequest https://win.rustup.rs/ -OutFile rustup-init.exe
.\rustup-init.exe -y --default-host=x86_64-pc-windows-msvc --default-toolchain=none
del rustup-init.exe
rustup target add ${{ matrix.target }}
shell: powershell
# # Run build
# - name: Install Rustup using win.rustup.rs
# run: |
# # Disable the download progress bar which can cause perf issues
# $ProgressPreference = "SilentlyContinue"
# Invoke-WebRequest https://win.rustup.rs/ -OutFile rustup-init.exe
# .\rustup-init.exe -y --default-host=x86_64-pc-windows-msvc --default-toolchain=none
# del rustup-init.exe
# rustup target add ${{ matrix.target }}
# shell: powershell

- name: Add mingw64 to path for x86_64-gnu
run: echo "C:\msys64\mingw64\bin" >> $GITHUB_PATH
if: matrix.target == 'x86_64-pc-windows-gnu' && matrix.channel == 'nightly'
shell: bash
# - name: Add mingw64 to path for x86_64-gnu
# run: echo "C:\msys64\mingw64\bin" >> $GITHUB_PATH
# if: matrix.target == 'x86_64-pc-windows-gnu' && matrix.channel == 'nightly'
# shell: bash

- name: Install Protoc
uses: arduino/setup-protoc@v3
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
# - name: Install Protoc
# uses: arduino/setup-protoc@v3
# with:
# repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Set Perl environment variables
if: runner.os == 'Windows'
run: |
echo "PERL=$((where.exe perl)[0])" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
echo "OPENSSL_SRC_PERL=$((where.exe perl)[0])" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
# - name: Set Perl environment variables
# if: runner.os == 'Windows'
# run: |
# echo "PERL=$((where.exe perl)[0])" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8
# echo "OPENSSL_SRC_PERL=$((where.exe perl)[0])" | Out-File -FilePath $env:GITHUB_ENV -Append -Encoding utf8

- name: Run cargo release
shell: bash
run: |
cargo --version
cargo build --release --features=full
ls -lh target/release
# - name: Run cargo release
# shell: bash
# run: |
# cargo --version
# cargo build --release --features=full
# ls -lh target/release
docker:
runs-on: ubuntu-latest
timeout-minutes: 3600
Expand Down
19 changes: 15 additions & 4 deletions src/acme/lets_encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::config::{
LoadConfigOptions, PingapConf, CATEGORY_CERTIFICATE,
};
use crate::http_extra::HttpResponse;
use crate::proxy::init_certificates;
use crate::proxy::try_update_certificates;
use crate::service::SimpleServiceTaskFuture;
use crate::state::State;
use crate::util;
Expand Down Expand Up @@ -97,13 +97,24 @@ async fn do_update_certificates(
domains = domains.join(","),
"renew certificate success"
);
webhook::send(webhook::SendNotificationParams {
webhook::send_notification(webhook::SendNotificationParams {
category: webhook::NotificationCategory::LetsEncrypt,
msg: "Generate new cert from lets encrypt".to_string(),
remark: Some(format!("Domains: {domains:?}")),
..Default::default()
});
init_certificates(&conf.certificates);
})
.await;
let (_, errors) = try_update_certificates(&conf.certificates);
if !errors.is_empty() {
error!(error = errors, "parse certificate fail");
webhook::send_notification(webhook::SendNotificationParams {
category:
webhook::NotificationCategory::ParseCertificateFail,
level: webhook::NotificationLevel::Error,
msg: errors,
remark: None,
}).await;
}
},
Err(e) => error!(
error = e.to_string(),
Expand Down
5 changes: 3 additions & 2 deletions src/certificate/validity_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ async fn do_validity_check(count: u32) -> Result<(), String> {
if let Err(message) = validity_check(&certificate_info_list, time_offset) {
// certificate will be expired
warn!(category = "validityChecker", message);
webhook::send(webhook::SendNotificationParams {
webhook::send_notification(webhook::SendNotificationParams {
level: webhook::NotificationLevel::Warn,
category: webhook::NotificationCategory::TlsValidity,
msg: message,
..Default::default()
});
})
.await;
}
Ok(())
}
Expand Down
5 changes: 3 additions & 2 deletions src/discovery/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,14 @@ impl ServiceDiscovery for Dns {
),
"dns discover fail"
);
webhook::send(webhook::SendNotificationParams {
webhook::send_notification(webhook::SendNotificationParams {
category:
webhook::NotificationCategory::ServiceDiscoverFail,
level: webhook::NotificationLevel::Warn,
msg: format!("dns discovery {:?}, error: {e}", self.hosts),
remark: None,
});
})
.await;
return Err(e.into());
},
}
Expand Down
5 changes: 3 additions & 2 deletions src/discovery/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl ServiceDiscovery for Docker {
),
"docker discover fail"
);
webhook::send(webhook::SendNotificationParams {
webhook::send_notification(webhook::SendNotificationParams {
category:
webhook::NotificationCategory::ServiceDiscoverFail,
level: webhook::NotificationLevel::Warn,
Expand All @@ -227,7 +227,8 @@ impl ServiceDiscovery for Docker {
self.labels(),
),
remark: None,
});
})
.await;
return Err(e.into());
},
}
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,13 +506,17 @@ fn run() -> Result<(), Box<dyn Error>> {
simple_tasks.push(new_lets_encrypt_service(lets_encrypt_params));
}

let updated_certificates = proxy::init_certificates(&certificates);
let (updated_certificates, errors) =
proxy::try_update_certificates(&certificates);
if !updated_certificates.is_empty() {
info!(
updated_certificates = updated_certificates.join(","),
"init certificates success"
);
}
if !errors.is_empty() {
error!(error = errors, "parse certificate fail");
}

// no server listen 80 and lets encrypt domains is not empty
if !exits_80_server && enabled_lets_encrypt {
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ impl Plugin for AdminServe {
})
.unwrap_or(HttpResponse::unknown_error("Json serde fail".into()))
} else if path == "/restart" && method == Method::POST {
if let Err(e) = restart_now() {
if let Err(e) = restart_now().await {
error!("Restart fail: {e}");
HttpResponse::bad_request(e.to_string().into())
} else {
Expand Down
30 changes: 11 additions & 19 deletions src/proxy/dynamic_certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use crate::certificate::{Certificate, TlsCertificate};
use crate::config::CertificateConf;
use crate::{util, webhook};
use crate::util;
use ahash::AHashMap;
use arc_swap::ArcSwap;
use async_trait::async_trait;
Expand Down Expand Up @@ -78,23 +78,11 @@ fn parse_certificates(
(dynamic_certs, errors)
}

/// Init certificates, which use for global tls callback
pub fn init_certificates(
/// Try update certificates, which use for global tls callback
pub fn try_update_certificates(
certificate_configs: &HashMap<String, CertificateConf>,
) -> Vec<String> {
) -> (Vec<String>, String) {
let (dynamic_certs, errors) = parse_certificates(certificate_configs);
if !errors.is_empty() {
let msg_list: Vec<String> = errors
.iter()
.map(|item| format!("name:{}, error:{}", item.0, item.1))
.collect();
webhook::send(webhook::SendNotificationParams {
category: webhook::NotificationCategory::ParseCertificateFail,
level: webhook::NotificationLevel::Error,
msg: msg_list.join(";"),
remark: None,
});
}
let certs = DYNAMIC_CERTIFICATE_MAP.load();
let mut updated_certificates = vec![];
for (name, cert) in dynamic_certs.iter() {
Expand All @@ -105,8 +93,12 @@ pub fn init_certificates(
}
updated_certificates.push(name.clone());
}
let msg_list: Vec<String> = errors
.iter()
.map(|item| format!("{}({})", item.1, item.0))
.collect();
DYNAMIC_CERTIFICATE_MAP.store(Arc::new(dynamic_certs));
updated_certificates
(updated_certificates, msg_list.join(";"))
}

/// Get certificate info list
Expand Down Expand Up @@ -280,7 +272,7 @@ impl pingora::listeners::TlsAccept for GlobalCertificate {
mod tests {
use super::{GlobalCertificate, TlsSettingParams, DYNAMIC_CERTIFICATE_MAP};
use crate::certificate::TlsCertificate;
use crate::{config::CertificateConf, proxy::init_certificates};
use crate::{config::CertificateConf, proxy::try_update_certificates};
use pretty_assertions::assert_eq;
use std::collections::HashMap;

Expand Down Expand Up @@ -390,7 +382,7 @@ aqcrKJfS+xaKWxXPiNlpBMG5

let mut map = HashMap::new();
map.insert("pingap".to_string(), cert_info);
init_certificates(&map);
try_update_certificates(&map);

let cert = DYNAMIC_CERTIFICATE_MAP
.load()
Expand Down
4 changes: 3 additions & 1 deletion src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ mod upstream;
#[allow(unused_imports)]
pub use location::Location;

pub use dynamic_certificate::{get_certificate_info_list, init_certificates};
pub use dynamic_certificate::{
get_certificate_info_list, try_update_certificates,
};
pub use location::try_init_locations;
pub use logger::Parser;
pub use server::*;
Expand Down
Loading

0 comments on commit 252c7c3

Please sign in to comment.