feat:初始化调度
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package com.xiang.xservice.fwd.schedule;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.xiang.xservice.basic.utils.DateUtils;
|
||||
import com.xiang.xservice.basic.utils.PrimaryKeyUtils;
|
||||
import com.xiang.xservice.basic.xservice.dingTalk.service.DingTalkService;
|
||||
@@ -21,15 +23,19 @@ import com.xiang.xservice.schedule.service.IDynamicTaskSchedulerService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -54,6 +60,24 @@ public class FwdImportantMsgJob {
|
||||
@Value("${DingTalk.chatId}")
|
||||
private String chatId;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
log.info("xs-fwd任务调度系统初始化!");
|
||||
ScheduledTaskEntity scheduledTaskEntity = new ScheduledTaskEntity();
|
||||
scheduledTaskEntity.setTaskGroup(TaskGroupEnum.SERVICE_FWD_SCHEDULE.getCode());
|
||||
scheduledTaskEntity.setStatus(TaskStatusEnum.UN_START.getCode());
|
||||
List<ScheduledTaskEntity> taskList = dynamicTaskSchedulerService.getTaskList(scheduledTaskEntity);
|
||||
for (ScheduledTaskEntity taskEntity : taskList) {
|
||||
HashMap params = com.alibaba.fastjson2.JSON.parseObject(taskEntity.getParameters(), HashMap.class);
|
||||
FwdOrderTaskParam param = MapUtils.isEmpty(params) ? new FwdOrderTaskParam(null, taskEntity.getId()) : new FwdOrderTaskParam((Long) params.get("projectId"), taskEntity.getId());
|
||||
LocalDateTime runTime = taskEntity.getRunTime().isBefore(LocalDateTime.now()) ? LocalDateTime.now().plusMinutes(1) : taskEntity.getRunTime();
|
||||
dynamicTaskScheduler.schedule(
|
||||
new TaskConfig(taskEntity.getId(), taskEntity.getTaskName(), taskEntity.getTaskGroup(), runTime, params),
|
||||
new TicketGrabTask(fwdUserConfigMapper, fwdAudienceConfigMapper, iPerformService, param, dynamicTaskSchedulerService)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0 0 8 1/1 * ?")
|
||||
@PostMapping("/preSaleReminderJob")
|
||||
public void preSaleReminderJob() throws Exception {
|
||||
@@ -85,26 +109,28 @@ public class FwdImportantMsgJob {
|
||||
for (FPerformProjectInfo data : saleTodayData) {
|
||||
msg.append("演唱会名称:").append(data.getProjectName()).append("预售时间:").append(DateUtils.getDateTimeStr(data.getPreSaleTime())).append("\n");
|
||||
long taskId = PrimaryKeyUtils.snowflakeId();
|
||||
Map<String, Object> params = Maps.newHashMap();
|
||||
params.put("projectId", data.getProjectId());
|
||||
dynamicTaskScheduler.schedule(
|
||||
new TaskConfig(taskId, "芬玩岛演唱会抢票-【" + data.getProjectName() + "】", TaskGroupEnum.SERVICE_FWD_SCHEDULE.getCode(),
|
||||
data.getPreSaleTime(), null),
|
||||
data.getPreSaleTime(), params),
|
||||
new TicketGrabTask(fwdUserConfigMapper, fwdAudienceConfigMapper, iPerformService,
|
||||
new FwdOrderTaskParam(data.getProjectId(), taskId), dynamicTaskSchedulerService));
|
||||
savaTask(data, taskId);
|
||||
savaTask(data, taskId, params);
|
||||
}
|
||||
msg.append("请注意进行数据库配置的更改!");
|
||||
dingTalkService.sendChatMessage(chatId, msg.toString());
|
||||
log.info("【芬玩岛】演唱会预售定时任务结束!time:{}", System.currentTimeMillis());
|
||||
}
|
||||
|
||||
private void savaTask(FPerformProjectInfo data, long taskId) {
|
||||
private void savaTask(FPerformProjectInfo data, long taskId, Map<String, Object> params) {
|
||||
ScheduledTaskEntity entity = new ScheduledTaskEntity();
|
||||
entity.setId(taskId);
|
||||
entity.setTaskName("芬玩岛演唱会抢票-【" + data.getProjectName() + "】");
|
||||
entity.setTaskGroup(TaskGroupEnum.SERVICE_FWD_SCHEDULE.getCode());
|
||||
entity.setRunTime(data.getPreSaleTime());
|
||||
entity.setStatus(TaskStatusEnum.UN_START.getCode());
|
||||
entity.setParameters(null);
|
||||
entity.setParameters(JSON.toJSONString(params));
|
||||
entity.setCreatedTime(LocalDateTime.now());
|
||||
entity.setUpdatedTime(LocalDateTime.now());
|
||||
dynamicTaskSchedulerService.saveTask(entity);
|
||||
|
||||
@@ -35,6 +35,7 @@ public class TicketGrabTask implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
log.info("【TicketGrabTask】 run start. 获取到参数:{}", JSON.toJSONString(param));
|
||||
dynamicTaskSchedulerService.updateProcess(param.getTaskId());
|
||||
List<FUserConfig> availableUser = userConfigMapper.getAvailableUser();
|
||||
@@ -46,10 +47,17 @@ public class TicketGrabTask implements Runnable {
|
||||
try {
|
||||
if (performService.createProjectOrder(param.getProjectId(), audiences.stream().map(FAudienceConfig::getFrequentId).collect(Collectors.toList()))) {
|
||||
dynamicTaskSchedulerService.finishTask(param.getTaskId());
|
||||
} else {
|
||||
dynamicTaskSchedulerService.finishTask(param.getTaskId());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("创建订单失败", e);
|
||||
}
|
||||
dynamicTaskSchedulerService.errTask(param.getTaskId());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("任务调度运行失败", e);
|
||||
dynamicTaskSchedulerService.errTask(param.getTaskId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.xiang.xservice.xb.schedule.jntyzx;
|
||||
|
||||
import com.xiang.xservice.jntyzx.service.JntyzxService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Author: xiang
|
||||
* @Date: 2025-05-14 15:34
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class QueryVenueJob {
|
||||
private final JntyzxService jntyzxService;
|
||||
|
||||
// @Scheduled(cron = "0 0 14 1/1 * ?")
|
||||
public void query() throws Exception{
|
||||
log.info("[查询场地] 查询当天场地定时任务启动!");
|
||||
jntyzxService.queryAvailable();
|
||||
log.info("[查询场地] 查询当天场地定时任务结束!");
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://172.28.159.213:3306/xservice-script?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
|
||||
url: jdbc:mysql://120.27.153.87:3306/xservice-script-test?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
|
||||
username: root
|
||||
password: 123456
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
|
||||
Reference in New Issue
Block a user