1、更新服务重启后,任务下一次执行时间

This commit is contained in:
td_zhangyu 2019-12-24 12:01:44 +08:00
parent 767959adca
commit fb9b587830
4 changed files with 22 additions and 13 deletions

View File

@ -5,12 +5,9 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.spiderflow.context.SpiderContext;
import org.spiderflow.context.SpiderContextHolder;
import org.spiderflow.core.Spider;

View File

@ -15,6 +15,8 @@ import org.spiderflow.core.model.SpiderFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 爬虫定时执行管理
* @author Administrator
@ -51,7 +53,7 @@ public class SpiderJobManager {
* @param spiderFlow 爬虫流程图
* @return boolean true/false
*/
public boolean addJob(SpiderFlow spiderFlow){
public Date addJob(SpiderFlow spiderFlow){
try {
JobDetail job = JobBuilder.newJob(SpiderJob.class).withIdentity(getJobKey(spiderFlow.getId())).build();
job.getJobDataMap().put(JOB_PARAM_NAME, spiderFlow);
@ -60,12 +62,10 @@ public class SpiderJobManager {
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(getTriggerKey(spiderFlow.getId())).withSchedule(cronScheduleBuilder).build();
scheduler.scheduleJob(job,trigger);
return true;
return scheduler.scheduleJob(job,trigger);
} catch (SchedulerException e) {
logger.error("创建定时任务出错",e);
return false;
return null;
}
}

View File

@ -54,6 +54,9 @@ public interface SpiderFlowMapper extends BaseMapper<SpiderFlow>{
@Update("update sp_flow set enabled = #{enabled} where id = #{id}")
int resetSpiderStatus(@Param("id") String id, @Param("enabled") String enabled);
@Select("update sp_flow set next_execute_time = null where id = #{id}")
int resetNextExecuteTime(@Param("id") String id);
@Select("select id,name from sp_flow")
List<SpiderFlow> selectFlows();

View File

@ -43,10 +43,14 @@ public class SpiderFlowService extends ServiceImpl<SpiderFlowMapper, SpiderFlow>
@PostConstruct
private void initJobs(){
List<SpiderFlow> spiderFlows = sfMapper.selectList(new QueryWrapper<SpiderFlow>().eq("enabled", "1"));
if(spiderFlows != null){
if(spiderFlows != null && !spiderFlows.isEmpty()){
for (SpiderFlow sf : spiderFlows) {
if(StringUtils.isNotEmpty(sf.getCron())){
spiderJobManager.addJob(sf);
Date nextExecuteTimt = spiderJobManager.addJob(sf);
if (nextExecuteTimt != null) {
sf.setNextExecuteTime(nextExecuteTimt);
sfMapper.updateById(sf);
}
}
}
}
@ -110,14 +114,19 @@ public class SpiderFlowService extends ServiceImpl<SpiderFlowMapper, SpiderFlow>
public void stop(String id){
sfMapper.resetSpiderStatus(id,"0");
sfMapper.resetNextExecuteTime(id);
spiderJobManager.remove(id);
}
public void start(String id){
spiderJobManager.remove(id);
SpiderFlow spiderFlow = getById(id);
spiderJobManager.addJob(spiderFlow);
sfMapper.resetSpiderStatus(id,"1");
Date nextExecuteTime = spiderJobManager.addJob(spiderFlow);
if (nextExecuteTime != null) {
spiderFlow.setNextExecuteTime(nextExecuteTime);
sfMapper.updateById(spiderFlow);
sfMapper.resetSpiderStatus(id, "1");
}
}
public void run(String id){