2222import org .apache .dolphinscheduler .common .enums .TaskDependType ;
2323import org .apache .dolphinscheduler .dao .entity .Schedule ;
2424import org .apache .dolphinscheduler .dao .entity .WorkflowDefinition ;
25+ import org .apache .dolphinscheduler .dao .repository .ScheduleDao ;
26+ import org .apache .dolphinscheduler .dao .repository .WorkflowDefinitionDao ;
2527import org .apache .dolphinscheduler .dao .utils .WorkerGroupUtils ;
2628import org .apache .dolphinscheduler .extract .master .IWorkflowControlClient ;
2729import org .apache .dolphinscheduler .extract .master .transportor .workflow .WorkflowScheduleTriggerRequest ;
28- import org .apache .dolphinscheduler .service .process .ProcessService ;
2930
3031import java .util .Date ;
32+ import java .util .Optional ;
3133
3234import lombok .extern .slf4j .Slf4j ;
3335
4446public class ProcessScheduleTask extends QuartzJobBean {
4547
4648 @ Autowired
47- private ProcessService processService ;
49+ private ScheduleDao scheduleDao ;
50+
51+ @ Autowired
52+ private WorkflowDefinitionDao workflowDefinitionDao ;
4853
4954 @ Autowired
5055 private IWorkflowControlClient workflowInstanceController ;
@@ -53,34 +58,48 @@ public class ProcessScheduleTask extends QuartzJobBean {
5358 @ Timed (value = "ds.master.quartz.job.execution.time" , percentiles = {0.5 , 0.75 , 0.95 , 0.99 }, histogram = true )
5459 @ Override
5560 protected void executeInternal (JobExecutionContext context ) {
56- QuartzJobData quartzJobData = QuartzJobData .of (context .getJobDetail ().getJobDataMap ());
57- int projectId = quartzJobData .getProjectId ();
58- int scheduleId = quartzJobData .getScheduleId ();
61+ final QuartzJobData quartzJobData = QuartzJobData .of (context .getJobDetail ().getJobDataMap ());
62+ final int projectId = quartzJobData .getProjectId ();
63+ final int scheduleId = quartzJobData .getScheduleId ();
5964
60- Date scheduledFireTime = context .getScheduledFireTime ();
65+ final Date scheduledFireTime = context .getScheduledFireTime ();
66+ final Date fireTime = context .getFireTime ();
6167
62- Date fireTime = context .getFireTime ();
68+ log .info ("Scheduler: {} fired expect fire time is {}, actual fire time is {}" ,
69+ scheduleId ,
70+ scheduledFireTime ,
71+ fireTime );
6372
64- log .info ("scheduled fire time :{}, fire time :{}, scheduleId :{}" , scheduledFireTime , fireTime , scheduleId );
65-
66- // query schedule
67- Schedule schedule = processService .querySchedule (scheduleId );
73+ // If the schedule does not exist or offline, then delete the corn job
74+ final Schedule schedule = scheduleDao .queryById (scheduleId );
6875 if (schedule == null || ReleaseState .OFFLINE == schedule .getReleaseState ()) {
76+ log .warn ("Scheduler: {} does not exist in db,will delete job in quartz" , scheduleId );
77+ deleteJob (context , projectId , scheduleId );
78+ return ;
79+ }
80+
81+ final Optional <WorkflowDefinition > workflowDefinitionOptional =
82+ workflowDefinitionDao .queryByCode (schedule .getWorkflowDefinitionCode ());
83+ if (!workflowDefinitionOptional .isPresent ()) {
6984 log .warn (
70- "process schedule does not exist in db or process schedule offline,delete schedule job in quartz, projectId:{}, scheduleId:{}" ,
71- projectId , scheduleId );
85+ "Scheduler: {} bind workflow: {} does not exist in db,will delete the schedule and delete schedule job in quartz" ,
86+ scheduleId ,
87+ schedule .getWorkflowDefinitionCode ());
88+ scheduleDao .deleteById (scheduleId );
7289 deleteJob (context , projectId , scheduleId );
7390 return ;
7491 }
7592
76- WorkflowDefinition workflowDefinition =
77- processService .findWorkflowDefinitionByCode (schedule .getWorkflowDefinitionCode ());
78- // release state : online/offline
79- ReleaseState releaseState = workflowDefinition .getReleaseState ();
80- if (releaseState == ReleaseState .OFFLINE ) {
93+ final WorkflowDefinition workflowDefinition = workflowDefinitionOptional .get ();
94+ if (workflowDefinition .getReleaseState () == ReleaseState .OFFLINE ) {
8195 log .warn (
82- "process definition does not exist in db or offline,need not to create command, projectId:{}, processDefinitionId:{}" ,
83- projectId , workflowDefinition .getId ());
96+ "Scheduler: {} bind workflow: {} state is OFFLINE,will update the schedule status to OFFLINE and delete schedule job in quartz" ,
97+ scheduleId ,
98+ schedule .getWorkflowDefinitionCode ());
99+ schedule .setReleaseState (ReleaseState .OFFLINE );
100+ schedule .setUpdateTime (new Date ());
101+ scheduleDao .updateById (schedule );
102+ deleteJob (context , projectId , scheduleId );
84103 return ;
85104 }
86105
@@ -106,14 +125,14 @@ protected void executeInternal(JobExecutionContext context) {
106125
107126 private void deleteJob (JobExecutionContext context , int projectId , int scheduleId ) {
108127 final Scheduler scheduler = context .getScheduler ();
109- JobKey jobKey = QuartzJobKey .of (projectId , scheduleId ).toJobKey ();
128+ final JobKey jobKey = QuartzJobKey .of (projectId , scheduleId ).toJobKey ();
110129 try {
111130 if (scheduler .checkExists (jobKey )) {
112- log .info ("Try to delete job: {}, projectId: {}, schedulerId" , projectId , scheduleId );
131+ log .info ("Try to delete job: {}, projectId: {}, schedulerId: {}" , jobKey , projectId , scheduleId );
113132 scheduler .deleteJob (jobKey );
114133 }
115134 } catch (Exception e ) {
116- log .error ("Failed to delete job: {}" , jobKey );
135+ log .error ("Failed to delete job: {}" , jobKey , e );
117136 }
118137 }
119138}
0 commit comments