Skip to content

Commit

Permalink
schedule: fix delete fail (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
4t145 authored Jun 20, 2024
1 parent aa24e32 commit 6df321e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 21 deletions.
16 changes: 4 additions & 12 deletions backend/middlewares/schedule/src/dto/schedule_job_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,10 @@ impl TryFrom<KvItemSummaryResp> for KvScheduleJobItemDetailResp {
type Error = TardisError;

fn try_from(resp: KvItemSummaryResp) -> Result<Self, Self::Error> {
let Some(s) = &resp.value.as_str() else {
return Err(TardisError::internal_error("value are expected to be a string", "schedule-409-bad-schedule-job"));
};
let req: ScheduleJobAddOrModifyReq =
tardis::serde_json::from_str(s).map_err(|e| TardisError::internal_error(&format!("can't parse schedule job json body: {e}"), "schedule-409-bad-schedule-job"))?;
let value = ScheduleJobAddOrModifyReq::parse_from_json(&resp.value);
Ok(Self {
key: resp.key.trim_start_matches(KV_KEY_CODE).to_string(),
value: req,
value,
info: resp.info,
create_time: resp.create_time,
update_time: resp.update_time,
Expand All @@ -171,14 +167,10 @@ impl TryFrom<KvItemDetailResp> for KvScheduleJobItemDetailResp {
type Error = TardisError;

fn try_from(resp: KvItemDetailResp) -> Result<Self, Self::Error> {
let Some(s) = &resp.value.as_str() else {
return Err(TardisError::internal_error("value are expected to be a string", "schedule-409-bad-schedule-job"));
};
let req: ScheduleJobAddOrModifyReq =
tardis::serde_json::from_str(s).map_err(|e| TardisError::internal_error(&format!("can't parse schedule job json body: {e}"), "schedule-409-bad-schedule-job"))?;
let value = ScheduleJobAddOrModifyReq::parse_from_json(&resp.value);
Ok(Self {
key: resp.key.trim_start_matches(KV_KEY_CODE).to_string(),
value: req,
value,
info: resp.info,
create_time: resp.create_time,
update_time: resp.update_time,
Expand Down
12 changes: 3 additions & 9 deletions backend/middlewares/schedule/src/serv/schedule_job_serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,6 @@ pub(crate) async fn delete(code: &str, funs: &TardisFunsInst, ctx: &TardisContex
conn.set_ex(&format!("{cache_key_job_changed_info}{code}"), "delete", config.cache_key_job_changed_timer_sec as u64).await?;
// 4. do delete at local scheduler
if service().code_uuid.read().await.get(code).is_some() {
// delete schedual-task from kv cache first
// 先从kv缓存中删除调度任务
let mut conn = funs.cache().cmd().await?;
let config = funs.conf::<ScheduleConfig>();
let cache_key_job_changed_info = &config.cache_key_job_changed_info;
conn.del(&format!("{cache_key_job_changed_info}{code}")).await?;
// delete schedual-task from scheduler
// 从调度器中删除调度任务
ScheduleTaskServ::delete(code).await?;
Expand Down Expand Up @@ -282,7 +276,7 @@ impl OwnedScheduleTaskServ {
if let Ok(job_resp) = self::find_job(None, 1, 9999, &funs, &sync_db_ctx).await {
let jobs = job_resp.records;
for job in jobs {
serv.add(job.create_add_or_mod_req(), &config).await.map_err(|e| error!("fail to delete schedule task: {e}")).unwrap_or_default();
serv.add(job.create_add_or_mod_req(), &config).await.map_err(|e| error!("fail to add schedule task: {e}")).unwrap_or_default();
}
info!("synced all jobs from kv");
break;
Expand Down Expand Up @@ -332,7 +326,7 @@ impl OwnedScheduleTaskServ {
match self::find_one_job(code, &funs, &ctx).await {
Ok(Some(resp)) => {
// if we have this job code in local cache, update or add it
serv.add(resp.value, &config).await.map_err(|e| error!("fail to delete schedule task: {e}")).unwrap_or_default();
serv.add(resp.value, &config).await.map_err(|e| error!("fail to add schedule task: {e}")).unwrap_or_default();
}
Ok(None) => {
// if we don't have this job code in local cache, remove it
Expand Down Expand Up @@ -529,7 +523,7 @@ impl OwnedScheduleTaskServ {
if let Some(tasks) = uuid_cache.get(code) {
for (uuid, _) in tasks {
self.scheduler.remove(uuid).await.map_err(|err| {
let msg = format!("fail to add job: {}", err);
let msg = format!("fail to remove job: {}", err);
TardisError::internal_error(&msg, "500-middlewares-schedual-create-task-failed")
})?;
}
Expand Down

0 comments on commit 6df321e

Please sign in to comment.