Skip to content

Commit

Permalink
Merge pull request #766 from 4t145/schedule-fix-log-param
Browse files Browse the repository at this point in the history
schedule: fix log api's parameters
  • Loading branch information
4t145 authored Jun 7, 2024
2 parents 8274133 + ff5a4e4 commit c8385f3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
16 changes: 15 additions & 1 deletion backend/middlewares/schedule/src/dto/schedule_job_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub(crate) struct KvItemSummaryResp {
pub update_time: DateTime<Utc>,
}

#[derive(poem_openapi::Object, Serialize, Deserialize, Clone, Debug, Default)]
#[derive(poem_openapi::Object, Serialize, Deserialize, Clone, Debug)]
pub struct ScheduleJobAddOrModifyReq {
#[oai(validator(min_length = "2"))]
pub code: TrimString,
Expand All @@ -49,6 +49,20 @@ pub struct ScheduleJobAddOrModifyReq {
#[serde(default)]
pub disable_time: Option<DateTime<Utc>>,
}
impl Default for ScheduleJobAddOrModifyReq {
fn default() -> Self {
Self {
code: Default::default(),
cron: Default::default(),
callback_url: Default::default(),
callback_headers: Default::default(),
callback_method: "GET".to_string(),
callback_body: Default::default(),
enable_time: Default::default(),
disable_time: Default::default(),
}
}
}

impl ScheduleJobAddOrModifyReq {
pub fn parse_time_from_json_value(value: &Value) -> Option<DateTime<Utc>> {
Expand Down
35 changes: 26 additions & 9 deletions backend/middlewares/schedule/src/serv/schedule_job_serv.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::future::Future;
use std::net::SocketAddr;
use std::pin::Pin;
use std::sync::{Arc, OnceLock};
use std::time::Duration;
Expand All @@ -19,7 +20,7 @@ use tardis::log::{error, info, trace, warn};
use tardis::tokio::sync::RwLock;
use tardis::tokio::time;
use tardis::web::web_resp::{TardisPage, TardisResp};
use tardis::{TardisFuns, TardisFunsInst};
use tardis::{serde_json, TardisFuns, TardisFunsInst};
use tokio_cron_scheduler::{Job, JobScheduler};

use crate::dto::schedule_job_dto::{KvScheduleJobItemDetailResp, ScheduleJobAddOrModifyReq, ScheduleJobInfoResp, ScheduleJobKvSummaryResp, ScheduleTaskInfoResp};
Expand Down Expand Up @@ -56,10 +57,10 @@ pub(crate) async fn add_or_modify(add_or_modify: ScheduleJobAddOrModifyReq, funs
"schedule_job",
"add job",
None,
Some(code.to_string()),
None,
Some(code.to_string()),
Some("add".to_string()),
Some(tardis::chrono::Utc::now().to_rfc3339()),
None,
Some(Utc::now().to_rfc3339()),
None,
None,
Expand Down Expand Up @@ -88,10 +89,10 @@ pub(crate) async fn delete(code: &str, funs: &TardisFunsInst, ctx: &TardisContex
"schedule_job",
"delete job",
None,
Some(code.to_string()),
None,
Some(code.to_string()),
Some("delete".to_string()),
Some(tardis::chrono::Utc::now().to_rfc3339()),
None,
Some(Utc::now().to_rfc3339()),
None,
None,
Expand Down Expand Up @@ -437,17 +438,33 @@ impl OwnedScheduleTaskServ {
// 2. request webhook
match TardisFuns::web_client().raw().execute(callback_req).await {
Ok(resp) => {
let code = resp.status();
let status_code = resp.status();
let remote_addr = resp.remote_addr().as_ref().map(SocketAddr::to_string);
let response_header: HashMap<String, String> = resp
.headers()
.into_iter()
.filter_map(|(k, v)| {
let v = v.to_str().ok()?.to_string();
Some((k.to_string(), v))
})
.collect();
let ext = serde_json::json! {
{
"remote_addr": remote_addr,
"status_code": status_code.to_string(),
"headers": response_header
}
};
let content = resp.text().await.unwrap_or_default();
// 3.1. write log exec end
let Ok(_) = SpiLogClient::add(
"schedule_task",
&content,
None,
Some(ext),
None,
Some(code.to_string()),
Some("exec-end".to_string()),
Some(tardis::chrono::Utc::now().to_rfc3339()),
None,
Some(Utc::now().to_rfc3339()),
None,
None,
Expand All @@ -468,7 +485,7 @@ impl OwnedScheduleTaskServ {
None,
Some(code.to_string()),
Some("exec-fail".to_string()),
Some(tardis::chrono::Utc::now().to_rfc3339()),
None,
Some(Utc::now().to_rfc3339()),
None,
None,
Expand Down

0 comments on commit c8385f3

Please sign in to comment.