final String jobId = context.getQueue().poll();
SocketLog.info("master scan and poll jobId=" + jobId + " and run!");
new Thread() {
@Override
public void run() {
JobHistory his = context.getJobHistoryManager().findJobHistory(
context.getGroupManager().getJobStatus(jobId)
.getHistoryId());
TriggerType type = his.getTriggerType();
ScheduleInfoLog.info("JobId:" + jobId + " run start");
his.getLog().appendZeus(
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(new Date()) + " 开始运行");
context.getJobHistoryManager().updateJobHistoryLog(his.getId(),
his.getLog().getContent());
Exception exception = null;
Response resp = null;
try {
Future<Response> f = new MasterExecuteJob().executeJob(
context, w, ExecuteKind.ScheduleKind, his.getId());
resp = f.get();
} catch (Exception e) {
exception = e;
ScheduleInfoLog.error(
String.format("JobId:%s run failed", jobId), e);
}
boolean success = resp.getStatus() == Status.OK ? true : false;
JobStatus jobstatus = context.getGroupManager().getJobStatus(
jobId);
jobstatus
.setStatus(com.taobao.zeus.model.JobStatus.Status.WAIT);
if (success
&& (his.getTriggerType() == TriggerType.SCHEDULE || his
.getTriggerType() == TriggerType.MANUAL_RECOVER)) {
ScheduleInfoLog.info("JobId:" + jobId
+ " clear ready dependency");
jobstatus.setReadyDependency(new HashMap<String, String>());
}
context.getGroupManager().updateJobStatus(jobstatus);
if (!success) {
// 运行失败,更新失败状态,发出失败消息
ZeusJobException jobException = null;
if (exception != null) {
jobException = new ZeusJobException(jobId,
String.format("JobId:%s run failed ", jobId),
exception);
} else {
jobException = new ZeusJobException(jobId,
String.format("JobId:%s run failed ", jobId));
}
ScheduleInfoLog.info("JobId:" + jobId
+ " run fail and dispatch the fail event");
JobFailedEvent jfe = new JobFailedEvent(jobId, type,
context.getJobHistoryManager().findJobHistory(
his.getId()), jobException);
context.getDispatcher().forwardEvent(jfe);
} else {
// 运行成功,发出成功消息
ScheduleInfoLog.info("JobId:" + jobId
+ " run success and dispatch the success event");
JobSuccessEvent jse = new JobSuccessEvent(jobId,
his.getTriggerType(), his.getId());
context.getDispatcher().forwardEvent(jse);
}
}
}.start();
}